@azure/msal-node 2.0.2 → 2.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +3 -1
- package/dist/cache/NodeStorage.cjs +1 -1
- package/dist/cache/NodeStorage.cjs.map +1 -1
- package/dist/cache/NodeStorage.d.ts +1 -1
- package/dist/cache/NodeStorage.d.ts.map +1 -1
- package/dist/cache/NodeStorage.mjs +1 -1
- package/dist/cache/NodeStorage.mjs.map +1 -1
- package/dist/cache/TokenCache.cjs +5 -9
- package/dist/cache/TokenCache.cjs.map +1 -1
- package/dist/cache/TokenCache.d.ts +3 -3
- package/dist/cache/TokenCache.d.ts.map +1 -1
- package/dist/cache/TokenCache.mjs +6 -10
- package/dist/cache/TokenCache.mjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.d.ts +2 -2
- package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
- package/dist/cache/serializer/Deserializer.cjs +2 -4
- package/dist/cache/serializer/Deserializer.cjs.map +1 -1
- package/dist/cache/serializer/Deserializer.d.ts +1 -1
- package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
- package/dist/cache/serializer/Deserializer.mjs +3 -5
- package/dist/cache/serializer/Deserializer.mjs.map +1 -1
- package/dist/cache/serializer/Serializer.cjs +1 -1
- package/dist/cache/serializer/Serializer.cjs.map +1 -1
- package/dist/cache/serializer/Serializer.d.ts +1 -1
- package/dist/cache/serializer/Serializer.d.ts.map +1 -1
- package/dist/cache/serializer/Serializer.mjs +1 -1
- package/dist/cache/serializer/Serializer.mjs.map +1 -1
- package/dist/client/ClientApplication.cjs +2 -2
- package/dist/client/ClientApplication.cjs.map +1 -1
- package/dist/client/ClientApplication.d.ts +10 -10
- package/dist/client/ClientApplication.d.ts.map +1 -1
- package/dist/client/ClientApplication.mjs +2 -2
- package/dist/client/ClientApplication.mjs.map +1 -1
- package/dist/client/ClientAssertion.cjs +1 -1
- package/dist/client/ClientAssertion.cjs.map +1 -1
- package/dist/client/ClientAssertion.d.ts +1 -1
- package/dist/client/ClientAssertion.d.ts.map +1 -1
- package/dist/client/ClientAssertion.mjs +1 -1
- package/dist/client/ClientAssertion.mjs.map +1 -1
- package/dist/client/ClientCredentialClient.cjs +26 -4
- package/dist/client/ClientCredentialClient.cjs.map +1 -1
- package/dist/client/ClientCredentialClient.d.ts +1 -0
- package/dist/client/ClientCredentialClient.d.ts.map +1 -1
- package/dist/client/ClientCredentialClient.mjs +27 -5
- package/dist/client/ClientCredentialClient.mjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.cjs +4 -5
- package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.d.ts +5 -5
- package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/ConfidentialClientApplication.mjs +5 -6
- package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
- package/dist/client/DeviceCodeClient.cjs +3 -3
- package/dist/client/DeviceCodeClient.cjs.map +1 -1
- package/dist/client/DeviceCodeClient.d.ts.map +1 -1
- package/dist/client/DeviceCodeClient.mjs +4 -4
- package/dist/client/DeviceCodeClient.mjs.map +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts +8 -8
- package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/IPublicClientApplication.d.ts +9 -9
- package/dist/client/IPublicClientApplication.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.cjs +7 -8
- package/dist/client/OnBehalfOfClient.cjs.map +1 -1
- package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.mjs +7 -8
- package/dist/client/OnBehalfOfClient.mjs.map +1 -1
- package/dist/client/PublicClientApplication.cjs +36 -29
- package/dist/client/PublicClientApplication.cjs.map +1 -1
- package/dist/client/PublicClientApplication.d.ts +7 -7
- package/dist/client/PublicClientApplication.d.ts.map +1 -1
- package/dist/client/PublicClientApplication.mjs +40 -33
- package/dist/client/PublicClientApplication.mjs.map +1 -1
- package/dist/client/UsernamePasswordClient.cjs +2 -2
- package/dist/client/UsernamePasswordClient.cjs.map +1 -1
- package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
- package/dist/client/UsernamePasswordClient.mjs +2 -2
- package/dist/client/UsernamePasswordClient.mjs.map +1 -1
- package/dist/config/Configuration.cjs +1 -1
- package/dist/config/Configuration.cjs.map +1 -1
- package/dist/config/Configuration.mjs +1 -1
- package/dist/config/Configuration.mjs.map +1 -1
- package/dist/crypto/CryptoProvider.cjs +1 -1
- package/dist/crypto/CryptoProvider.cjs.map +1 -1
- package/dist/crypto/CryptoProvider.mjs +1 -1
- package/dist/crypto/CryptoProvider.mjs.map +1 -1
- package/dist/crypto/GuidGenerator.cjs +1 -1
- package/dist/crypto/GuidGenerator.cjs.map +1 -1
- package/dist/crypto/GuidGenerator.mjs +1 -1
- package/dist/crypto/GuidGenerator.mjs.map +1 -1
- package/dist/crypto/HashUtils.cjs +1 -1
- package/dist/crypto/HashUtils.cjs.map +1 -1
- package/dist/crypto/HashUtils.mjs +1 -1
- package/dist/crypto/HashUtils.mjs.map +1 -1
- package/dist/crypto/PkceGenerator.cjs +1 -1
- package/dist/crypto/PkceGenerator.cjs.map +1 -1
- package/dist/crypto/PkceGenerator.mjs +1 -1
- package/dist/crypto/PkceGenerator.mjs.map +1 -1
- package/dist/error/NodeAuthError.cjs +1 -1
- package/dist/error/NodeAuthError.cjs.map +1 -1
- package/dist/error/NodeAuthError.mjs +1 -1
- package/dist/error/NodeAuthError.mjs.map +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.ts +32 -32
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +1 -1
- package/dist/internals.cjs +1 -1
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.d.ts +2 -2
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.mjs +1 -1
- package/dist/internals.mjs.map +1 -1
- package/dist/network/HttpClient.cjs +10 -9
- package/dist/network/HttpClient.cjs.map +1 -1
- package/dist/network/HttpClient.d.ts.map +1 -1
- package/dist/network/HttpClient.mjs +3 -2
- package/dist/network/HttpClient.mjs.map +1 -1
- package/dist/network/LoopbackClient.cjs +2 -2
- package/dist/network/LoopbackClient.cjs.map +1 -1
- package/dist/network/LoopbackClient.d.ts +1 -1
- package/dist/network/LoopbackClient.d.ts.map +1 -1
- package/dist/network/LoopbackClient.mjs +3 -3
- package/dist/network/LoopbackClient.mjs.map +1 -1
- package/dist/packageMetadata.cjs +2 -2
- package/dist/packageMetadata.cjs.map +1 -1
- package/dist/packageMetadata.d.ts +1 -1
- package/dist/packageMetadata.mjs +2 -2
- package/dist/packageMetadata.mjs.map +1 -1
- package/dist/request/InteractiveRequest.d.ts +2 -2
- package/dist/request/InteractiveRequest.d.ts.map +1 -1
- package/dist/utils/Constants.cjs +1 -11
- package/dist/utils/Constants.cjs.map +1 -1
- package/dist/utils/Constants.d.ts +0 -10
- package/dist/utils/Constants.d.ts.map +1 -1
- package/dist/utils/Constants.mjs +2 -11
- package/dist/utils/Constants.mjs.map +1 -1
- package/dist/utils/EncodingUtils.cjs +1 -1
- package/dist/utils/EncodingUtils.cjs.map +1 -1
- package/dist/utils/EncodingUtils.mjs +1 -1
- package/dist/utils/EncodingUtils.mjs.map +1 -1
- package/dist/utils/NetworkUtils.cjs +1 -1
- package/dist/utils/NetworkUtils.cjs.map +1 -1
- package/dist/utils/NetworkUtils.mjs +1 -1
- package/dist/utils/NetworkUtils.mjs.map +1 -1
- package/package.json +8 -5
- package/src/cache/ITokenCache.ts +24 -0
- package/src/cache/NodeStorage.ts +533 -0
- package/src/cache/TokenCache.ts +358 -0
- package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
- package/src/cache/distributed/ICacheClient.ts +23 -0
- package/src/cache/distributed/IPartitionManager.ts +35 -0
- package/src/cache/serializer/Deserializer.ts +218 -0
- package/src/cache/serializer/Serializer.ts +173 -0
- package/src/cache/serializer/SerializerTypes.ts +121 -0
- package/src/client/ClientApplication.ts +595 -0
- package/src/client/ClientAssertion.ts +160 -0
- package/src/client/ClientCredentialClient.ts +348 -0
- package/src/client/ConfidentialClientApplication.ts +261 -0
- package/src/client/DeviceCodeClient.ts +360 -0
- package/src/client/IConfidentialClientApplication.ts +72 -0
- package/src/client/IPublicClientApplication.ts +72 -0
- package/src/client/OnBehalfOfClient.ts +361 -0
- package/src/client/PublicClientApplication.ts +283 -0
- package/src/client/UsernamePasswordClient.ts +180 -0
- package/src/config/Configuration.ts +201 -0
- package/src/crypto/CryptoProvider.ts +98 -0
- package/src/crypto/GuidGenerator.ts +28 -0
- package/src/crypto/HashUtils.ts +17 -0
- package/src/crypto/PkceGenerator.ts +63 -0
- package/src/error/NodeAuthError.ts +117 -0
- package/src/index.ts +121 -0
- package/src/internals.ts +12 -0
- package/src/network/HttpClient.ts +425 -0
- package/src/network/ILoopbackClient.ts +19 -0
- package/src/network/LoopbackClient.ts +126 -0
- package/src/packageMetadata.ts +3 -0
- package/src/request/AuthorizationCodeRequest.ts +39 -0
- package/src/request/AuthorizationUrlRequest.ts +50 -0
- package/src/request/ClientCredentialRequest.ts +29 -0
- package/src/request/DeviceCodeRequest.ts +34 -0
- package/src/request/InteractiveRequest.ts +39 -0
- package/src/request/OnBehalfOfRequest.ts +30 -0
- package/src/request/RefreshTokenRequest.ts +34 -0
- package/src/request/SignOutRequest.ts +11 -0
- package/src/request/SilentFlowRequest.ts +32 -0
- package/src/request/UsernamePasswordRequest.ts +36 -0
- package/src/utils/Constants.ts +103 -0
- package/src/utils/EncodingUtils.ts +50 -0
- package/src/utils/NetworkUtils.ts +49 -0
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import jwt from "jsonwebtoken";
|
|
7
|
+
import { TimeUtils, ClientAuthError, Constants } from "@azure/msal-common";
|
|
8
|
+
import { CryptoProvider } from "../crypto/CryptoProvider.js";
|
|
9
|
+
import { EncodingUtils } from "../utils/EncodingUtils.js";
|
|
10
|
+
import { JwtConstants } from "../utils/Constants.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Client assertion of type jwt-bearer used in confidential client flows
|
|
14
|
+
* @public
|
|
15
|
+
*/
|
|
16
|
+
export class ClientAssertion {
|
|
17
|
+
private jwt: string;
|
|
18
|
+
private privateKey: string;
|
|
19
|
+
private thumbprint: string;
|
|
20
|
+
private expirationTime: number;
|
|
21
|
+
private issuer: string;
|
|
22
|
+
private jwtAudience: string;
|
|
23
|
+
private publicCertificate: Array<string>;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Initialize the ClientAssertion class from the clientAssertion passed by the user
|
|
27
|
+
* @param assertion - refer https://tools.ietf.org/html/rfc7521
|
|
28
|
+
*/
|
|
29
|
+
public static fromAssertion(assertion: string): ClientAssertion {
|
|
30
|
+
const clientAssertion = new ClientAssertion();
|
|
31
|
+
clientAssertion.jwt = assertion;
|
|
32
|
+
return clientAssertion;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Initialize the ClientAssertion class from the certificate passed by the user
|
|
37
|
+
* @param thumbprint - identifier of a certificate
|
|
38
|
+
* @param privateKey - secret key
|
|
39
|
+
* @param publicCertificate - electronic document provided to prove the ownership of the public key
|
|
40
|
+
*/
|
|
41
|
+
public static fromCertificate(
|
|
42
|
+
thumbprint: string,
|
|
43
|
+
privateKey: string,
|
|
44
|
+
publicCertificate?: string
|
|
45
|
+
): ClientAssertion {
|
|
46
|
+
const clientAssertion = new ClientAssertion();
|
|
47
|
+
clientAssertion.privateKey = privateKey;
|
|
48
|
+
clientAssertion.thumbprint = thumbprint;
|
|
49
|
+
if (publicCertificate) {
|
|
50
|
+
clientAssertion.publicCertificate =
|
|
51
|
+
this.parseCertificate(publicCertificate);
|
|
52
|
+
}
|
|
53
|
+
return clientAssertion;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Update JWT for certificate based clientAssertion, if passed by the user, uses it as is
|
|
58
|
+
* @param cryptoProvider - library's crypto helper
|
|
59
|
+
* @param issuer - iss claim
|
|
60
|
+
* @param jwtAudience - aud claim
|
|
61
|
+
*/
|
|
62
|
+
public getJwt(
|
|
63
|
+
cryptoProvider: CryptoProvider,
|
|
64
|
+
issuer: string,
|
|
65
|
+
jwtAudience: string
|
|
66
|
+
): string {
|
|
67
|
+
// if assertion was created from certificate, check if jwt is expired and create new one.
|
|
68
|
+
if (this.privateKey && this.thumbprint) {
|
|
69
|
+
if (
|
|
70
|
+
this.jwt &&
|
|
71
|
+
!this.isExpired() &&
|
|
72
|
+
issuer === this.issuer &&
|
|
73
|
+
jwtAudience === this.jwtAudience
|
|
74
|
+
) {
|
|
75
|
+
return this.jwt;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
return this.createJwt(cryptoProvider, issuer, jwtAudience);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/*
|
|
82
|
+
* if assertion was created by caller, then we just append it. It is up to the caller to
|
|
83
|
+
* ensure that it contains necessary claims and that it is not expired.
|
|
84
|
+
*/
|
|
85
|
+
if (this.jwt) {
|
|
86
|
+
return this.jwt;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
throw ClientAuthError.createInvalidAssertionError();
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* JWT format and required claims specified: https://tools.ietf.org/html/rfc7523#section-3
|
|
94
|
+
*/
|
|
95
|
+
private createJwt(
|
|
96
|
+
cryptoProvider: CryptoProvider,
|
|
97
|
+
issuer: string,
|
|
98
|
+
jwtAudience: string
|
|
99
|
+
): string {
|
|
100
|
+
this.issuer = issuer;
|
|
101
|
+
this.jwtAudience = jwtAudience;
|
|
102
|
+
const issuedAt = TimeUtils.nowSeconds();
|
|
103
|
+
this.expirationTime = issuedAt + 600;
|
|
104
|
+
|
|
105
|
+
const header: jwt.JwtHeader = {
|
|
106
|
+
alg: JwtConstants.RSA_256,
|
|
107
|
+
x5t: EncodingUtils.base64EncodeUrl(this.thumbprint, "hex"),
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
if (this.publicCertificate) {
|
|
111
|
+
Object.assign(header, {
|
|
112
|
+
x5c: this.publicCertificate,
|
|
113
|
+
} as Partial<jwt.JwtHeader>);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const payload = {
|
|
117
|
+
[JwtConstants.AUDIENCE]: this.jwtAudience,
|
|
118
|
+
[JwtConstants.EXPIRATION_TIME]: this.expirationTime,
|
|
119
|
+
[JwtConstants.ISSUER]: this.issuer,
|
|
120
|
+
[JwtConstants.SUBJECT]: this.issuer,
|
|
121
|
+
[JwtConstants.NOT_BEFORE]: issuedAt,
|
|
122
|
+
[JwtConstants.JWT_ID]: cryptoProvider.createNewGuid(),
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
this.jwt = jwt.sign(payload, this.privateKey, { header });
|
|
126
|
+
return this.jwt;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Utility API to check expiration
|
|
131
|
+
*/
|
|
132
|
+
private isExpired(): boolean {
|
|
133
|
+
return this.expirationTime < TimeUtils.nowSeconds();
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Extracts the raw certs from a given certificate string and returns them in an array.
|
|
138
|
+
* @param publicCertificate - electronic document provided to prove the ownership of the public key
|
|
139
|
+
*/
|
|
140
|
+
public static parseCertificate(publicCertificate: string): Array<string> {
|
|
141
|
+
/**
|
|
142
|
+
* This is regex to identify the certs in a given certificate string.
|
|
143
|
+
* We want to look for the contents between the BEGIN and END certificate strings, without the associated newlines.
|
|
144
|
+
* The information in parens "(.+?)" is the capture group to represent the cert we want isolated.
|
|
145
|
+
* "." means any string character, "+" means match 1 or more times, and "?" means the shortest match.
|
|
146
|
+
* The "g" at the end of the regex means search the string globally, and the "s" enables the "." to match newlines.
|
|
147
|
+
*/
|
|
148
|
+
const regexToFindCerts =
|
|
149
|
+
/-----BEGIN CERTIFICATE-----\r*\n(.+?)\r*\n-----END CERTIFICATE-----/gs;
|
|
150
|
+
const certs: string[] = [];
|
|
151
|
+
|
|
152
|
+
let matches;
|
|
153
|
+
while ((matches = regexToFindCerts.exec(publicCertificate)) !== null) {
|
|
154
|
+
// matches[1] represents the first parens capture group in the regex.
|
|
155
|
+
certs.push(matches[1].replace(/\r*\n/g, Constants.EMPTY_STRING));
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
return certs;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
AccessTokenEntity,
|
|
8
|
+
AuthenticationResult,
|
|
9
|
+
AuthenticationScheme,
|
|
10
|
+
Authority,
|
|
11
|
+
BaseClient,
|
|
12
|
+
CacheOutcome,
|
|
13
|
+
ClientAuthError,
|
|
14
|
+
ClientConfiguration,
|
|
15
|
+
CommonClientCredentialRequest,
|
|
16
|
+
Constants,
|
|
17
|
+
CredentialFilter,
|
|
18
|
+
CredentialType,
|
|
19
|
+
GrantType,
|
|
20
|
+
IAppTokenProvider,
|
|
21
|
+
RequestParameterBuilder,
|
|
22
|
+
RequestThumbprint,
|
|
23
|
+
ResponseHandler,
|
|
24
|
+
ScopeSet,
|
|
25
|
+
ServerAuthorizationTokenResponse,
|
|
26
|
+
StringUtils,
|
|
27
|
+
TimeUtils,
|
|
28
|
+
TokenCacheContext,
|
|
29
|
+
UrlString,
|
|
30
|
+
} from "@azure/msal-common";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* OAuth2.0 client credential grant
|
|
34
|
+
*/
|
|
35
|
+
export class ClientCredentialClient extends BaseClient {
|
|
36
|
+
private scopeSet: ScopeSet;
|
|
37
|
+
private readonly appTokenProvider?: IAppTokenProvider;
|
|
38
|
+
private lastCacheOutcome: CacheOutcome;
|
|
39
|
+
|
|
40
|
+
constructor(
|
|
41
|
+
configuration: ClientConfiguration,
|
|
42
|
+
appTokenProvider?: IAppTokenProvider
|
|
43
|
+
) {
|
|
44
|
+
super(configuration);
|
|
45
|
+
this.appTokenProvider = appTokenProvider;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Public API to acquire a token with ClientCredential Flow for Confidential clients
|
|
50
|
+
* @param request
|
|
51
|
+
*/
|
|
52
|
+
public async acquireToken(
|
|
53
|
+
request: CommonClientCredentialRequest
|
|
54
|
+
): Promise<AuthenticationResult | null> {
|
|
55
|
+
this.scopeSet = new ScopeSet(request.scopes || []);
|
|
56
|
+
|
|
57
|
+
if (request.skipCache) {
|
|
58
|
+
return await this.executeTokenRequest(request, this.authority);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const cachedAuthenticationResult =
|
|
62
|
+
await this.getCachedAuthenticationResult(request);
|
|
63
|
+
|
|
64
|
+
if (cachedAuthenticationResult) {
|
|
65
|
+
// if the token is not expired but must be refreshed; get a new one in the background
|
|
66
|
+
if (
|
|
67
|
+
this.lastCacheOutcome ===
|
|
68
|
+
CacheOutcome.REFRESH_CACHED_ACCESS_TOKEN
|
|
69
|
+
) {
|
|
70
|
+
this.logger.info(
|
|
71
|
+
"ClientCredentialClient:getCachedAuthenticationResult - Cached access token's refreshOn property has been exceeded'. It's not expired, but must be refreshed."
|
|
72
|
+
);
|
|
73
|
+
|
|
74
|
+
// return the cached token, don't wait for the result of this request
|
|
75
|
+
const refreshAccessToken = true;
|
|
76
|
+
await this.executeTokenRequest(
|
|
77
|
+
request,
|
|
78
|
+
this.authority,
|
|
79
|
+
refreshAccessToken
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
// reset the last cache outcome
|
|
84
|
+
this.lastCacheOutcome = CacheOutcome.NO_CACHE_HIT;
|
|
85
|
+
|
|
86
|
+
// otherwise, the token is not expired and does not need to be refreshed
|
|
87
|
+
return cachedAuthenticationResult;
|
|
88
|
+
} else {
|
|
89
|
+
return await this.executeTokenRequest(request, this.authority);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* looks up cache if the tokens are cached already
|
|
95
|
+
*/
|
|
96
|
+
private async getCachedAuthenticationResult(
|
|
97
|
+
request: CommonClientCredentialRequest
|
|
98
|
+
): Promise<AuthenticationResult | null> {
|
|
99
|
+
// read the user-supplied cache into memory, if applicable
|
|
100
|
+
let cacheContext;
|
|
101
|
+
if (this.config.serializableCache && this.config.persistencePlugin) {
|
|
102
|
+
cacheContext = new TokenCacheContext(
|
|
103
|
+
this.config.serializableCache,
|
|
104
|
+
false
|
|
105
|
+
);
|
|
106
|
+
await this.config.persistencePlugin.beforeCacheAccess(cacheContext);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const cachedAccessToken = this.readAccessTokenFromCache();
|
|
110
|
+
|
|
111
|
+
if (
|
|
112
|
+
this.config.serializableCache &&
|
|
113
|
+
this.config.persistencePlugin &&
|
|
114
|
+
cacheContext
|
|
115
|
+
) {
|
|
116
|
+
await this.config.persistencePlugin.afterCacheAccess(cacheContext);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// must refresh due to non-existent access_token
|
|
120
|
+
if (!cachedAccessToken) {
|
|
121
|
+
this.lastCacheOutcome = CacheOutcome.NO_CACHED_ACCESS_TOKEN;
|
|
122
|
+
this.serverTelemetryManager?.setCacheOutcome(
|
|
123
|
+
CacheOutcome.NO_CACHED_ACCESS_TOKEN
|
|
124
|
+
);
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// must refresh due to the expires_in value
|
|
129
|
+
if (
|
|
130
|
+
TimeUtils.isTokenExpired(
|
|
131
|
+
cachedAccessToken.expiresOn,
|
|
132
|
+
this.config.systemOptions.tokenRenewalOffsetSeconds
|
|
133
|
+
)
|
|
134
|
+
) {
|
|
135
|
+
this.lastCacheOutcome = CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED;
|
|
136
|
+
this.serverTelemetryManager?.setCacheOutcome(
|
|
137
|
+
CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// must refresh (in the background) due to the refresh_in value
|
|
144
|
+
if (
|
|
145
|
+
cachedAccessToken.refreshOn &&
|
|
146
|
+
TimeUtils.isTokenExpired(cachedAccessToken.refreshOn.toString(), 0)
|
|
147
|
+
) {
|
|
148
|
+
this.lastCacheOutcome = CacheOutcome.REFRESH_CACHED_ACCESS_TOKEN;
|
|
149
|
+
this.serverTelemetryManager?.setCacheOutcome(
|
|
150
|
+
CacheOutcome.REFRESH_CACHED_ACCESS_TOKEN
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
return await ResponseHandler.generateAuthenticationResult(
|
|
155
|
+
this.cryptoUtils,
|
|
156
|
+
this.authority,
|
|
157
|
+
{
|
|
158
|
+
account: null,
|
|
159
|
+
idToken: null,
|
|
160
|
+
accessToken: cachedAccessToken,
|
|
161
|
+
refreshToken: null,
|
|
162
|
+
appMetadata: null,
|
|
163
|
+
},
|
|
164
|
+
true,
|
|
165
|
+
request
|
|
166
|
+
);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/**
|
|
170
|
+
* Reads access token from the cache
|
|
171
|
+
*/
|
|
172
|
+
private readAccessTokenFromCache(): AccessTokenEntity | null {
|
|
173
|
+
const accessTokenFilter: CredentialFilter = {
|
|
174
|
+
homeAccountId: Constants.EMPTY_STRING,
|
|
175
|
+
environment:
|
|
176
|
+
this.authority.canonicalAuthorityUrlComponents.HostNameAndPort,
|
|
177
|
+
credentialType: CredentialType.ACCESS_TOKEN,
|
|
178
|
+
clientId: this.config.authOptions.clientId,
|
|
179
|
+
realm: this.authority.tenant,
|
|
180
|
+
target: ScopeSet.createSearchScopes(this.scopeSet.asArray()),
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const accessTokens =
|
|
184
|
+
this.cacheManager.getAccessTokensByFilter(accessTokenFilter);
|
|
185
|
+
if (accessTokens.length < 1) {
|
|
186
|
+
return null;
|
|
187
|
+
} else if (accessTokens.length > 1) {
|
|
188
|
+
throw ClientAuthError.createMultipleMatchingTokensInCacheError();
|
|
189
|
+
}
|
|
190
|
+
return accessTokens[0] as AccessTokenEntity;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Makes a network call to request the token from the service
|
|
195
|
+
* @param request
|
|
196
|
+
* @param authority
|
|
197
|
+
*/
|
|
198
|
+
private async executeTokenRequest(
|
|
199
|
+
request: CommonClientCredentialRequest,
|
|
200
|
+
authority: Authority,
|
|
201
|
+
refreshAccessToken?: boolean
|
|
202
|
+
): Promise<AuthenticationResult | null> {
|
|
203
|
+
let serverTokenResponse: ServerAuthorizationTokenResponse;
|
|
204
|
+
let reqTimestamp: number;
|
|
205
|
+
|
|
206
|
+
if (this.appTokenProvider) {
|
|
207
|
+
this.logger.info("Using appTokenProvider extensibility.");
|
|
208
|
+
|
|
209
|
+
const appTokenPropviderParameters = {
|
|
210
|
+
correlationId: request.correlationId,
|
|
211
|
+
tenantId: this.config.authOptions.authority.tenant,
|
|
212
|
+
scopes: request.scopes,
|
|
213
|
+
claims: request.claims,
|
|
214
|
+
};
|
|
215
|
+
|
|
216
|
+
reqTimestamp = TimeUtils.nowSeconds();
|
|
217
|
+
const appTokenProviderResult = await this.appTokenProvider(
|
|
218
|
+
appTokenPropviderParameters
|
|
219
|
+
);
|
|
220
|
+
|
|
221
|
+
serverTokenResponse = {
|
|
222
|
+
access_token: appTokenProviderResult.accessToken,
|
|
223
|
+
expires_in: appTokenProviderResult.expiresInSeconds,
|
|
224
|
+
refresh_in: appTokenProviderResult.refreshInSeconds,
|
|
225
|
+
token_type: AuthenticationScheme.BEARER,
|
|
226
|
+
};
|
|
227
|
+
} else {
|
|
228
|
+
const queryParametersString =
|
|
229
|
+
this.createTokenQueryParameters(request);
|
|
230
|
+
const endpoint = UrlString.appendQueryString(
|
|
231
|
+
authority.tokenEndpoint,
|
|
232
|
+
queryParametersString
|
|
233
|
+
);
|
|
234
|
+
const requestBody = this.createTokenRequestBody(request);
|
|
235
|
+
const headers: Record<string, string> =
|
|
236
|
+
this.createTokenRequestHeaders();
|
|
237
|
+
const thumbprint: RequestThumbprint = {
|
|
238
|
+
clientId: this.config.authOptions.clientId,
|
|
239
|
+
authority: request.authority,
|
|
240
|
+
scopes: request.scopes,
|
|
241
|
+
claims: request.claims,
|
|
242
|
+
authenticationScheme: request.authenticationScheme,
|
|
243
|
+
resourceRequestMethod: request.resourceRequestMethod,
|
|
244
|
+
resourceRequestUri: request.resourceRequestUri,
|
|
245
|
+
shrClaims: request.shrClaims,
|
|
246
|
+
sshKid: request.sshKid,
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
reqTimestamp = TimeUtils.nowSeconds();
|
|
250
|
+
const response = await this.executePostToTokenEndpoint(
|
|
251
|
+
endpoint,
|
|
252
|
+
requestBody,
|
|
253
|
+
headers,
|
|
254
|
+
thumbprint,
|
|
255
|
+
request.correlationId
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
serverTokenResponse = response.body;
|
|
259
|
+
serverTokenResponse.status = response.status;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
const responseHandler = new ResponseHandler(
|
|
263
|
+
this.config.authOptions.clientId,
|
|
264
|
+
this.cacheManager,
|
|
265
|
+
this.cryptoUtils,
|
|
266
|
+
this.logger,
|
|
267
|
+
this.config.serializableCache,
|
|
268
|
+
this.config.persistencePlugin
|
|
269
|
+
);
|
|
270
|
+
|
|
271
|
+
responseHandler.validateTokenResponse(
|
|
272
|
+
serverTokenResponse,
|
|
273
|
+
refreshAccessToken
|
|
274
|
+
);
|
|
275
|
+
|
|
276
|
+
const tokenResponse = await responseHandler.handleServerTokenResponse(
|
|
277
|
+
serverTokenResponse,
|
|
278
|
+
this.authority,
|
|
279
|
+
reqTimestamp,
|
|
280
|
+
request
|
|
281
|
+
);
|
|
282
|
+
|
|
283
|
+
return tokenResponse;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* generate the request to the server in the acceptable format
|
|
288
|
+
* @param request
|
|
289
|
+
*/
|
|
290
|
+
private createTokenRequestBody(
|
|
291
|
+
request: CommonClientCredentialRequest
|
|
292
|
+
): string {
|
|
293
|
+
const parameterBuilder = new RequestParameterBuilder();
|
|
294
|
+
|
|
295
|
+
parameterBuilder.addClientId(this.config.authOptions.clientId);
|
|
296
|
+
|
|
297
|
+
parameterBuilder.addScopes(request.scopes, false);
|
|
298
|
+
|
|
299
|
+
parameterBuilder.addGrantType(GrantType.CLIENT_CREDENTIALS_GRANT);
|
|
300
|
+
|
|
301
|
+
parameterBuilder.addLibraryInfo(this.config.libraryInfo);
|
|
302
|
+
parameterBuilder.addApplicationTelemetry(
|
|
303
|
+
this.config.telemetry.application
|
|
304
|
+
);
|
|
305
|
+
|
|
306
|
+
parameterBuilder.addThrottling();
|
|
307
|
+
|
|
308
|
+
if (this.serverTelemetryManager) {
|
|
309
|
+
parameterBuilder.addServerTelemetry(this.serverTelemetryManager);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
const correlationId =
|
|
313
|
+
request.correlationId ||
|
|
314
|
+
this.config.cryptoInterface.createNewGuid();
|
|
315
|
+
parameterBuilder.addCorrelationId(correlationId);
|
|
316
|
+
|
|
317
|
+
if (this.config.clientCredentials.clientSecret) {
|
|
318
|
+
parameterBuilder.addClientSecret(
|
|
319
|
+
this.config.clientCredentials.clientSecret
|
|
320
|
+
);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
// Use clientAssertion from request, fallback to client assertion in base configuration
|
|
324
|
+
const clientAssertion =
|
|
325
|
+
request.clientAssertion ||
|
|
326
|
+
this.config.clientCredentials.clientAssertion;
|
|
327
|
+
|
|
328
|
+
if (clientAssertion) {
|
|
329
|
+
parameterBuilder.addClientAssertion(clientAssertion.assertion);
|
|
330
|
+
parameterBuilder.addClientAssertionType(
|
|
331
|
+
clientAssertion.assertionType
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (
|
|
336
|
+
!StringUtils.isEmptyObj(request.claims) ||
|
|
337
|
+
(this.config.authOptions.clientCapabilities &&
|
|
338
|
+
this.config.authOptions.clientCapabilities.length > 0)
|
|
339
|
+
) {
|
|
340
|
+
parameterBuilder.addClaims(
|
|
341
|
+
request.claims,
|
|
342
|
+
this.config.authOptions.clientCapabilities
|
|
343
|
+
);
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
return parameterBuilder.createQueryString();
|
|
347
|
+
}
|
|
348
|
+
}
|