forked from Azure/azure-sdk-for-js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtokenCredential.ts
More file actions
80 lines (74 loc) · 2.31 KB
/
Copy pathtokenCredential.ts
File metadata and controls
80 lines (74 loc) · 2.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
import { AbortSignalLike } from "@azure/abort-controller";
import { SpanOptions } from "@azure/core-tracing";
/**
* Represents a credential capable of providing an authentication token.
*/
export interface TokenCredential {
/**
* Gets the token provided by this credential.
*
* @param scopes The list of scopes for which the token will have access.
* @param options The options used to configure any requests this
* TokenCredential implementation might make.
*/
getToken(scopes: string | string[], options?: GetTokenOptions): Promise<AccessToken | null>;
}
/**
* Defines options for TokenCredential.getToken.
*/
export interface GetTokenOptions {
/**
* The signal which can be used to abort requests.
*/
abortSignal?: AbortSignalLike;
/**
* Options used when creating and sending HTTP requests for this operation.
*/
requestOptions?: {
/**
* The number of milliseconds a request can take before automatically being terminated.
*/
timeout?: number;
};
/**
* Options used when tracing is enabled.
*/
tracingOptions?: {
/**
* OpenTelemetry SpanOptions used to create a span when tracing is enabled.
*/
spanOptions?: SpanOptions;
};
}
/**
* Represents an access token with an expiration time.
*/
export interface AccessToken {
/**
* The access token returned by the authentication service.
*/
token: string;
/**
* The access token's expiration timestamp in milliseconds, UNIX epoch time.
*/
expiresOnTimestamp: number;
}
/**
* Tests an object to determine whether it implements TokenCredential.
*
* @param credential The assumed TokenCredential to be tested.
*/
export function isTokenCredential(credential: any): credential is TokenCredential {
// Check for an object with a 'getToken' function and possibly with
// a 'signRequest' function. We do this check to make sure that
// a ServiceClientCredentials implementor (like TokenClientCredentials
// in ms-rest-nodeauth) doesn't get mistaken for a TokenCredential if
// it doesn't actually implement TokenCredential also.
return (
credential &&
typeof credential.getToken === "function" &&
(credential.signRequest === undefined || credential.getToken.length > 0)
);
}