-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
tokenCredential.ts
92 lines (85 loc) · 2.69 KB
/
tokenCredential.ts
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
81
82
83
84
85
86
87
88
89
90
91
92
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
import { AbortSignalLike } from "@azure/abort-controller";
import { TracingContext } from "./tracing";
/**
* Represents a credential capable of providing an authentication token.
*/
export interface TokenCredential {
/**
* Gets the token provided by this credential.
*
* This method is called automatically by Azure SDK client libraries. You may call this method
* directly, but you must also handle token caching and token refreshing.
*
* @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?: {
/**
* Tracing Context for the current request.
*/
tracingContext?: TracingContext;
};
/**
* Allows specifying a tenantId. Useful to handle challenges that provide tenant Id hints.
*/
tenantId?: string;
}
/**
* 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: unknown): 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.
const castCredential = credential as {
getToken: unknown;
signRequest: unknown;
};
return (
castCredential &&
typeof castCredential.getToken === "function" &&
(castCredential.signRequest === undefined || castCredential.getToken.length > 0)
);
}