@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.
Files changed (191) hide show
  1. package/README.md +3 -1
  2. package/dist/cache/NodeStorage.cjs +1 -1
  3. package/dist/cache/NodeStorage.cjs.map +1 -1
  4. package/dist/cache/NodeStorage.d.ts +1 -1
  5. package/dist/cache/NodeStorage.d.ts.map +1 -1
  6. package/dist/cache/NodeStorage.mjs +1 -1
  7. package/dist/cache/NodeStorage.mjs.map +1 -1
  8. package/dist/cache/TokenCache.cjs +5 -9
  9. package/dist/cache/TokenCache.cjs.map +1 -1
  10. package/dist/cache/TokenCache.d.ts +3 -3
  11. package/dist/cache/TokenCache.d.ts.map +1 -1
  12. package/dist/cache/TokenCache.mjs +6 -10
  13. package/dist/cache/TokenCache.mjs.map +1 -1
  14. package/dist/cache/distributed/DistributedCachePlugin.cjs +1 -1
  15. package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
  16. package/dist/cache/distributed/DistributedCachePlugin.d.ts +2 -2
  17. package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
  18. package/dist/cache/distributed/DistributedCachePlugin.mjs +1 -1
  19. package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
  20. package/dist/cache/serializer/Deserializer.cjs +2 -4
  21. package/dist/cache/serializer/Deserializer.cjs.map +1 -1
  22. package/dist/cache/serializer/Deserializer.d.ts +1 -1
  23. package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
  24. package/dist/cache/serializer/Deserializer.mjs +3 -5
  25. package/dist/cache/serializer/Deserializer.mjs.map +1 -1
  26. package/dist/cache/serializer/Serializer.cjs +1 -1
  27. package/dist/cache/serializer/Serializer.cjs.map +1 -1
  28. package/dist/cache/serializer/Serializer.d.ts +1 -1
  29. package/dist/cache/serializer/Serializer.d.ts.map +1 -1
  30. package/dist/cache/serializer/Serializer.mjs +1 -1
  31. package/dist/cache/serializer/Serializer.mjs.map +1 -1
  32. package/dist/client/ClientApplication.cjs +2 -2
  33. package/dist/client/ClientApplication.cjs.map +1 -1
  34. package/dist/client/ClientApplication.d.ts +10 -10
  35. package/dist/client/ClientApplication.d.ts.map +1 -1
  36. package/dist/client/ClientApplication.mjs +2 -2
  37. package/dist/client/ClientApplication.mjs.map +1 -1
  38. package/dist/client/ClientAssertion.cjs +1 -1
  39. package/dist/client/ClientAssertion.cjs.map +1 -1
  40. package/dist/client/ClientAssertion.d.ts +1 -1
  41. package/dist/client/ClientAssertion.d.ts.map +1 -1
  42. package/dist/client/ClientAssertion.mjs +1 -1
  43. package/dist/client/ClientAssertion.mjs.map +1 -1
  44. package/dist/client/ClientCredentialClient.cjs +26 -4
  45. package/dist/client/ClientCredentialClient.cjs.map +1 -1
  46. package/dist/client/ClientCredentialClient.d.ts +1 -0
  47. package/dist/client/ClientCredentialClient.d.ts.map +1 -1
  48. package/dist/client/ClientCredentialClient.mjs +27 -5
  49. package/dist/client/ClientCredentialClient.mjs.map +1 -1
  50. package/dist/client/ConfidentialClientApplication.cjs +4 -5
  51. package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
  52. package/dist/client/ConfidentialClientApplication.d.ts +5 -5
  53. package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
  54. package/dist/client/ConfidentialClientApplication.mjs +5 -6
  55. package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
  56. package/dist/client/DeviceCodeClient.cjs +3 -3
  57. package/dist/client/DeviceCodeClient.cjs.map +1 -1
  58. package/dist/client/DeviceCodeClient.d.ts.map +1 -1
  59. package/dist/client/DeviceCodeClient.mjs +4 -4
  60. package/dist/client/DeviceCodeClient.mjs.map +1 -1
  61. package/dist/client/IConfidentialClientApplication.d.ts +8 -8
  62. package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
  63. package/dist/client/IPublicClientApplication.d.ts +9 -9
  64. package/dist/client/IPublicClientApplication.d.ts.map +1 -1
  65. package/dist/client/OnBehalfOfClient.cjs +7 -8
  66. package/dist/client/OnBehalfOfClient.cjs.map +1 -1
  67. package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
  68. package/dist/client/OnBehalfOfClient.mjs +7 -8
  69. package/dist/client/OnBehalfOfClient.mjs.map +1 -1
  70. package/dist/client/PublicClientApplication.cjs +36 -29
  71. package/dist/client/PublicClientApplication.cjs.map +1 -1
  72. package/dist/client/PublicClientApplication.d.ts +7 -7
  73. package/dist/client/PublicClientApplication.d.ts.map +1 -1
  74. package/dist/client/PublicClientApplication.mjs +40 -33
  75. package/dist/client/PublicClientApplication.mjs.map +1 -1
  76. package/dist/client/UsernamePasswordClient.cjs +2 -2
  77. package/dist/client/UsernamePasswordClient.cjs.map +1 -1
  78. package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
  79. package/dist/client/UsernamePasswordClient.mjs +2 -2
  80. package/dist/client/UsernamePasswordClient.mjs.map +1 -1
  81. package/dist/config/Configuration.cjs +1 -1
  82. package/dist/config/Configuration.cjs.map +1 -1
  83. package/dist/config/Configuration.mjs +1 -1
  84. package/dist/config/Configuration.mjs.map +1 -1
  85. package/dist/crypto/CryptoProvider.cjs +1 -1
  86. package/dist/crypto/CryptoProvider.cjs.map +1 -1
  87. package/dist/crypto/CryptoProvider.mjs +1 -1
  88. package/dist/crypto/CryptoProvider.mjs.map +1 -1
  89. package/dist/crypto/GuidGenerator.cjs +1 -1
  90. package/dist/crypto/GuidGenerator.cjs.map +1 -1
  91. package/dist/crypto/GuidGenerator.mjs +1 -1
  92. package/dist/crypto/GuidGenerator.mjs.map +1 -1
  93. package/dist/crypto/HashUtils.cjs +1 -1
  94. package/dist/crypto/HashUtils.cjs.map +1 -1
  95. package/dist/crypto/HashUtils.mjs +1 -1
  96. package/dist/crypto/HashUtils.mjs.map +1 -1
  97. package/dist/crypto/PkceGenerator.cjs +1 -1
  98. package/dist/crypto/PkceGenerator.cjs.map +1 -1
  99. package/dist/crypto/PkceGenerator.mjs +1 -1
  100. package/dist/crypto/PkceGenerator.mjs.map +1 -1
  101. package/dist/error/NodeAuthError.cjs +1 -1
  102. package/dist/error/NodeAuthError.cjs.map +1 -1
  103. package/dist/error/NodeAuthError.mjs +1 -1
  104. package/dist/error/NodeAuthError.mjs.map +1 -1
  105. package/dist/index.cjs +1 -1
  106. package/dist/index.d.ts +32 -32
  107. package/dist/index.d.ts.map +1 -1
  108. package/dist/index.mjs +1 -1
  109. package/dist/internals.cjs +1 -1
  110. package/dist/internals.cjs.map +1 -1
  111. package/dist/internals.d.ts +2 -2
  112. package/dist/internals.d.ts.map +1 -1
  113. package/dist/internals.mjs +1 -1
  114. package/dist/internals.mjs.map +1 -1
  115. package/dist/network/HttpClient.cjs +10 -9
  116. package/dist/network/HttpClient.cjs.map +1 -1
  117. package/dist/network/HttpClient.d.ts.map +1 -1
  118. package/dist/network/HttpClient.mjs +3 -2
  119. package/dist/network/HttpClient.mjs.map +1 -1
  120. package/dist/network/LoopbackClient.cjs +2 -2
  121. package/dist/network/LoopbackClient.cjs.map +1 -1
  122. package/dist/network/LoopbackClient.d.ts +1 -1
  123. package/dist/network/LoopbackClient.d.ts.map +1 -1
  124. package/dist/network/LoopbackClient.mjs +3 -3
  125. package/dist/network/LoopbackClient.mjs.map +1 -1
  126. package/dist/packageMetadata.cjs +2 -2
  127. package/dist/packageMetadata.cjs.map +1 -1
  128. package/dist/packageMetadata.d.ts +1 -1
  129. package/dist/packageMetadata.mjs +2 -2
  130. package/dist/packageMetadata.mjs.map +1 -1
  131. package/dist/request/InteractiveRequest.d.ts +2 -2
  132. package/dist/request/InteractiveRequest.d.ts.map +1 -1
  133. package/dist/utils/Constants.cjs +1 -11
  134. package/dist/utils/Constants.cjs.map +1 -1
  135. package/dist/utils/Constants.d.ts +0 -10
  136. package/dist/utils/Constants.d.ts.map +1 -1
  137. package/dist/utils/Constants.mjs +2 -11
  138. package/dist/utils/Constants.mjs.map +1 -1
  139. package/dist/utils/EncodingUtils.cjs +1 -1
  140. package/dist/utils/EncodingUtils.cjs.map +1 -1
  141. package/dist/utils/EncodingUtils.mjs +1 -1
  142. package/dist/utils/EncodingUtils.mjs.map +1 -1
  143. package/dist/utils/NetworkUtils.cjs +1 -1
  144. package/dist/utils/NetworkUtils.cjs.map +1 -1
  145. package/dist/utils/NetworkUtils.mjs +1 -1
  146. package/dist/utils/NetworkUtils.mjs.map +1 -1
  147. package/package.json +8 -5
  148. package/src/cache/ITokenCache.ts +24 -0
  149. package/src/cache/NodeStorage.ts +533 -0
  150. package/src/cache/TokenCache.ts +358 -0
  151. package/src/cache/distributed/DistributedCachePlugin.ts +56 -0
  152. package/src/cache/distributed/ICacheClient.ts +23 -0
  153. package/src/cache/distributed/IPartitionManager.ts +35 -0
  154. package/src/cache/serializer/Deserializer.ts +218 -0
  155. package/src/cache/serializer/Serializer.ts +173 -0
  156. package/src/cache/serializer/SerializerTypes.ts +121 -0
  157. package/src/client/ClientApplication.ts +595 -0
  158. package/src/client/ClientAssertion.ts +160 -0
  159. package/src/client/ClientCredentialClient.ts +348 -0
  160. package/src/client/ConfidentialClientApplication.ts +261 -0
  161. package/src/client/DeviceCodeClient.ts +360 -0
  162. package/src/client/IConfidentialClientApplication.ts +72 -0
  163. package/src/client/IPublicClientApplication.ts +72 -0
  164. package/src/client/OnBehalfOfClient.ts +361 -0
  165. package/src/client/PublicClientApplication.ts +283 -0
  166. package/src/client/UsernamePasswordClient.ts +180 -0
  167. package/src/config/Configuration.ts +201 -0
  168. package/src/crypto/CryptoProvider.ts +98 -0
  169. package/src/crypto/GuidGenerator.ts +28 -0
  170. package/src/crypto/HashUtils.ts +17 -0
  171. package/src/crypto/PkceGenerator.ts +63 -0
  172. package/src/error/NodeAuthError.ts +117 -0
  173. package/src/index.ts +121 -0
  174. package/src/internals.ts +12 -0
  175. package/src/network/HttpClient.ts +425 -0
  176. package/src/network/ILoopbackClient.ts +19 -0
  177. package/src/network/LoopbackClient.ts +126 -0
  178. package/src/packageMetadata.ts +3 -0
  179. package/src/request/AuthorizationCodeRequest.ts +39 -0
  180. package/src/request/AuthorizationUrlRequest.ts +50 -0
  181. package/src/request/ClientCredentialRequest.ts +29 -0
  182. package/src/request/DeviceCodeRequest.ts +34 -0
  183. package/src/request/InteractiveRequest.ts +39 -0
  184. package/src/request/OnBehalfOfRequest.ts +30 -0
  185. package/src/request/RefreshTokenRequest.ts +34 -0
  186. package/src/request/SignOutRequest.ts +11 -0
  187. package/src/request/SilentFlowRequest.ts +32 -0
  188. package/src/request/UsernamePasswordRequest.ts +36 -0
  189. package/src/utils/Constants.ts +103 -0
  190. package/src/utils/EncodingUtils.ts +50 -0
  191. package/src/utils/NetworkUtils.ts +49 -0
@@ -0,0 +1,72 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { AccountInfo, AuthenticationResult, Logger } from "@azure/msal-common";
7
+ import { AuthorizationCodeRequest } from "../request/AuthorizationCodeRequest.js";
8
+ import { AuthorizationUrlRequest } from "../request/AuthorizationUrlRequest.js";
9
+ import { DeviceCodeRequest } from "../request/DeviceCodeRequest.js";
10
+ import { RefreshTokenRequest } from "../request/RefreshTokenRequest.js";
11
+ import { SilentFlowRequest } from "../request/SilentFlowRequest.js";
12
+ import { UsernamePasswordRequest } from "../request/UsernamePasswordRequest.js";
13
+ import { TokenCache } from "../cache/TokenCache.js";
14
+ import { InteractiveRequest } from "../request/InteractiveRequest.js";
15
+ import { SignOutRequest } from "../request/SignOutRequest.js";
16
+
17
+ /**
18
+ * Interface for the PublicClientApplication class defining the public API signatures
19
+ * @public
20
+ */
21
+ export interface IPublicClientApplication {
22
+ /** Creates the URL of the authorization request */
23
+ getAuthCodeUrl(request: AuthorizationUrlRequest): Promise<string>;
24
+
25
+ /** Acquires a token by exchanging the authorization code received from the first step of OAuth 2.0 Authorization Code Flow */
26
+ acquireTokenByCode(
27
+ request: AuthorizationCodeRequest
28
+ ): Promise<AuthenticationResult>;
29
+
30
+ /** Acquires a token interactively */
31
+ acquireTokenInteractive(
32
+ request: InteractiveRequest
33
+ ): Promise<AuthenticationResult>;
34
+
35
+ /** Acquires a token silently when a user specifies the account the token is requested for */
36
+ acquireTokenSilent(
37
+ request: SilentFlowRequest
38
+ ): Promise<AuthenticationResult>;
39
+
40
+ /** Acquires a token by exchanging the refresh token provided for a new set of tokens */
41
+ acquireTokenByRefreshToken(
42
+ request: RefreshTokenRequest
43
+ ): Promise<AuthenticationResult | null>;
44
+
45
+ /** Acquires a token from the authority using OAuth2.0 device code flow */
46
+ acquireTokenByDeviceCode(
47
+ request: DeviceCodeRequest
48
+ ): Promise<AuthenticationResult | null>;
49
+
50
+ /** Acquires tokens with password grant by exchanging client applications username and password for credentials */
51
+ acquireTokenByUsernamePassword(
52
+ request: UsernamePasswordRequest
53
+ ): Promise<AuthenticationResult | null>;
54
+
55
+ /** Gets the token cache for the application */
56
+ getTokenCache(): TokenCache;
57
+
58
+ /** Returns the logger instance */
59
+ getLogger(): Logger;
60
+
61
+ /** Replaces the default logger set in configurations with new Logger with new configurations */
62
+ setLogger(logger: Logger): void;
63
+
64
+ /** Clear the cache */
65
+ clearCache(): void;
66
+
67
+ /** Gets all cached accounts */
68
+ getAllAccounts(): Promise<AccountInfo[]>;
69
+
70
+ /** Removes cache artifacts associated with the given account */
71
+ signOut(request: SignOutRequest): Promise<void>;
72
+ }
@@ -0,0 +1,361 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import {
7
+ AADServerParamKeys,
8
+ AccessTokenEntity,
9
+ AccountEntity,
10
+ AccountInfo,
11
+ AuthenticationResult,
12
+ AuthenticationScheme,
13
+ Authority,
14
+ AuthToken,
15
+ BaseClient,
16
+ CacheOutcome,
17
+ ClientAuthError,
18
+ ClientConfiguration,
19
+ CommonOnBehalfOfRequest,
20
+ Constants,
21
+ CredentialFilter,
22
+ CredentialType,
23
+ GrantType,
24
+ IdTokenEntity,
25
+ RequestParameterBuilder,
26
+ RequestThumbprint,
27
+ ResponseHandler,
28
+ ScopeSet,
29
+ TimeUtils,
30
+ TokenClaims,
31
+ UrlString,
32
+ } from "@azure/msal-common";
33
+ import { EncodingUtils } from "../utils/EncodingUtils";
34
+
35
+ /**
36
+ * On-Behalf-Of client
37
+ */
38
+ export class OnBehalfOfClient extends BaseClient {
39
+ private scopeSet: ScopeSet;
40
+ private userAssertionHash: string;
41
+
42
+ constructor(configuration: ClientConfiguration) {
43
+ super(configuration);
44
+ }
45
+
46
+ /**
47
+ * Public API to acquire tokens with on behalf of flow
48
+ * @param request
49
+ */
50
+ public async acquireToken(
51
+ request: CommonOnBehalfOfRequest
52
+ ): Promise<AuthenticationResult | null> {
53
+ this.scopeSet = new ScopeSet(request.scopes || []);
54
+
55
+ // generate the user_assertion_hash for OBOAssertion
56
+ this.userAssertionHash = await this.cryptoUtils.hashString(
57
+ request.oboAssertion
58
+ );
59
+
60
+ if (request.skipCache) {
61
+ return await this.executeTokenRequest(
62
+ request,
63
+ this.authority,
64
+ this.userAssertionHash
65
+ );
66
+ }
67
+
68
+ try {
69
+ return await this.getCachedAuthenticationResult(request);
70
+ } catch (e) {
71
+ // Any failure falls back to interactive request, once we implement distributed cache, we plan to handle `createRefreshRequiredError` to refresh using the RT
72
+ return await this.executeTokenRequest(
73
+ request,
74
+ this.authority,
75
+ this.userAssertionHash
76
+ );
77
+ }
78
+ }
79
+
80
+ /**
81
+ * look up cache for tokens
82
+ * Find idtoken in the cache
83
+ * Find accessToken based on user assertion and account info in the cache
84
+ * Please note we are not yet supported OBO tokens refreshed with long lived RT. User will have to send a new assertion if the current access token expires
85
+ * This is to prevent security issues when the assertion changes over time, however, longlived RT helps retaining the session
86
+ * @param request
87
+ */
88
+ private async getCachedAuthenticationResult(
89
+ request: CommonOnBehalfOfRequest
90
+ ): Promise<AuthenticationResult | null> {
91
+ // look in the cache for the access_token which matches the incoming_assertion
92
+ const cachedAccessToken = this.readAccessTokenFromCacheForOBO(
93
+ this.config.authOptions.clientId,
94
+ request
95
+ );
96
+ if (!cachedAccessToken) {
97
+ // Must refresh due to non-existent access_token.
98
+ this.serverTelemetryManager?.setCacheOutcome(
99
+ CacheOutcome.NO_CACHED_ACCESS_TOKEN
100
+ );
101
+ this.logger.info(
102
+ "SilentFlowClient:acquireCachedToken - No access token found in cache for the given properties."
103
+ );
104
+ throw ClientAuthError.createRefreshRequiredError();
105
+ } else if (
106
+ TimeUtils.isTokenExpired(
107
+ cachedAccessToken.expiresOn,
108
+ this.config.systemOptions.tokenRenewalOffsetSeconds
109
+ )
110
+ ) {
111
+ // Access token expired, will need to renewed
112
+ this.serverTelemetryManager?.setCacheOutcome(
113
+ CacheOutcome.CACHED_ACCESS_TOKEN_EXPIRED
114
+ );
115
+ this.logger.info(
116
+ `OnbehalfofFlow:getCachedAuthenticationResult - Cached access token is expired or will expire within ${this.config.systemOptions.tokenRenewalOffsetSeconds} seconds.`
117
+ );
118
+ throw ClientAuthError.createRefreshRequiredError();
119
+ }
120
+
121
+ // fetch the idToken from cache
122
+ const cachedIdToken = this.readIdTokenFromCacheForOBO(
123
+ cachedAccessToken.homeAccountId
124
+ );
125
+ let idTokenClaims: TokenClaims | undefined;
126
+ let cachedAccount: AccountEntity | null = null;
127
+ if (cachedIdToken) {
128
+ idTokenClaims = AuthToken.extractTokenClaims(
129
+ cachedIdToken.secret,
130
+ EncodingUtils.base64Decode
131
+ );
132
+ const localAccountId = idTokenClaims.oid || idTokenClaims.sub;
133
+ const accountInfo: AccountInfo = {
134
+ homeAccountId: cachedIdToken.homeAccountId,
135
+ environment: cachedIdToken.environment,
136
+ tenantId: cachedIdToken.realm,
137
+ username: Constants.EMPTY_STRING,
138
+ localAccountId: localAccountId || Constants.EMPTY_STRING,
139
+ };
140
+
141
+ cachedAccount = this.cacheManager.readAccountFromCache(accountInfo);
142
+ }
143
+
144
+ // increment telemetry cache hit counter
145
+ if (this.config.serverTelemetryManager) {
146
+ this.config.serverTelemetryManager.incrementCacheHits();
147
+ }
148
+
149
+ return await ResponseHandler.generateAuthenticationResult(
150
+ this.cryptoUtils,
151
+ this.authority,
152
+ {
153
+ account: cachedAccount,
154
+ accessToken: cachedAccessToken,
155
+ idToken: cachedIdToken,
156
+ refreshToken: null,
157
+ appMetadata: null,
158
+ },
159
+ true,
160
+ request,
161
+ idTokenClaims
162
+ );
163
+ }
164
+
165
+ /**
166
+ * read idtoken from cache, this is a specific implementation for OBO as the requirements differ from a generic lookup in the cacheManager
167
+ * Certain use cases of OBO flow do not expect an idToken in the cache/or from the service
168
+ * @param atHomeAccountId {string}
169
+ */
170
+ private readIdTokenFromCacheForOBO(
171
+ atHomeAccountId: string
172
+ ): IdTokenEntity | null {
173
+ const idTokenFilter: CredentialFilter = {
174
+ homeAccountId: atHomeAccountId,
175
+ environment:
176
+ this.authority.canonicalAuthorityUrlComponents.HostNameAndPort,
177
+ credentialType: CredentialType.ID_TOKEN,
178
+ clientId: this.config.authOptions.clientId,
179
+ realm: this.authority.tenant,
180
+ };
181
+
182
+ const idTokens: IdTokenEntity[] =
183
+ this.cacheManager.getIdTokensByFilter(idTokenFilter);
184
+
185
+ // When acquiring a token on behalf of an application, there might not be an id token in the cache
186
+ if (idTokens.length < 1) {
187
+ return null;
188
+ }
189
+ return idTokens[0] as IdTokenEntity;
190
+ }
191
+
192
+ /**
193
+ * Fetches the cached access token based on incoming assertion
194
+ * @param clientId
195
+ * @param request
196
+ * @param userAssertionHash
197
+ */
198
+ private readAccessTokenFromCacheForOBO(
199
+ clientId: string,
200
+ request: CommonOnBehalfOfRequest
201
+ ) {
202
+ const authScheme =
203
+ request.authenticationScheme || AuthenticationScheme.BEARER;
204
+ /*
205
+ * Distinguish between Bearer and PoP/SSH token cache types
206
+ * Cast to lowercase to handle "bearer" from ADFS
207
+ */
208
+ const credentialType =
209
+ authScheme &&
210
+ authScheme.toLowerCase() !==
211
+ AuthenticationScheme.BEARER.toLowerCase()
212
+ ? CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME
213
+ : CredentialType.ACCESS_TOKEN;
214
+
215
+ const accessTokenFilter: CredentialFilter = {
216
+ credentialType: credentialType,
217
+ clientId,
218
+ target: ScopeSet.createSearchScopes(this.scopeSet.asArray()),
219
+ tokenType: authScheme,
220
+ keyId: request.sshKid,
221
+ requestedClaimsHash: request.requestedClaimsHash,
222
+ userAssertionHash: this.userAssertionHash,
223
+ };
224
+
225
+ const accessTokens =
226
+ this.cacheManager.getAccessTokensByFilter(accessTokenFilter);
227
+
228
+ const numAccessTokens = accessTokens.length;
229
+ if (numAccessTokens < 1) {
230
+ return null;
231
+ } else if (numAccessTokens > 1) {
232
+ throw ClientAuthError.createMultipleMatchingTokensInCacheError();
233
+ }
234
+
235
+ return accessTokens[0] as AccessTokenEntity;
236
+ }
237
+
238
+ /**
239
+ * Make a network call to the server requesting credentials
240
+ * @param request
241
+ * @param authority
242
+ */
243
+ private async executeTokenRequest(
244
+ request: CommonOnBehalfOfRequest,
245
+ authority: Authority,
246
+ userAssertionHash: string
247
+ ): Promise<AuthenticationResult | null> {
248
+ const queryParametersString = this.createTokenQueryParameters(request);
249
+ const endpoint = UrlString.appendQueryString(
250
+ authority.tokenEndpoint,
251
+ queryParametersString
252
+ );
253
+ const requestBody = this.createTokenRequestBody(request);
254
+ const headers: Record<string, string> =
255
+ this.createTokenRequestHeaders();
256
+ const thumbprint: RequestThumbprint = {
257
+ clientId: this.config.authOptions.clientId,
258
+ authority: request.authority,
259
+ scopes: request.scopes,
260
+ claims: request.claims,
261
+ authenticationScheme: request.authenticationScheme,
262
+ resourceRequestMethod: request.resourceRequestMethod,
263
+ resourceRequestUri: request.resourceRequestUri,
264
+ shrClaims: request.shrClaims,
265
+ sshKid: request.sshKid,
266
+ };
267
+
268
+ const reqTimestamp = TimeUtils.nowSeconds();
269
+ const response = await this.executePostToTokenEndpoint(
270
+ endpoint,
271
+ requestBody,
272
+ headers,
273
+ thumbprint,
274
+ request.correlationId
275
+ );
276
+
277
+ const responseHandler = new ResponseHandler(
278
+ this.config.authOptions.clientId,
279
+ this.cacheManager,
280
+ this.cryptoUtils,
281
+ this.logger,
282
+ this.config.serializableCache,
283
+ this.config.persistencePlugin
284
+ );
285
+
286
+ responseHandler.validateTokenResponse(response.body);
287
+ const tokenResponse = await responseHandler.handleServerTokenResponse(
288
+ response.body,
289
+ this.authority,
290
+ reqTimestamp,
291
+ request,
292
+ undefined,
293
+ userAssertionHash
294
+ );
295
+
296
+ return tokenResponse;
297
+ }
298
+
299
+ /**
300
+ * generate a server request in accepable format
301
+ * @param request
302
+ */
303
+ private createTokenRequestBody(request: CommonOnBehalfOfRequest): string {
304
+ const parameterBuilder = new RequestParameterBuilder();
305
+
306
+ parameterBuilder.addClientId(this.config.authOptions.clientId);
307
+
308
+ parameterBuilder.addScopes(request.scopes);
309
+
310
+ parameterBuilder.addGrantType(GrantType.JWT_BEARER);
311
+
312
+ parameterBuilder.addClientInfo();
313
+
314
+ parameterBuilder.addLibraryInfo(this.config.libraryInfo);
315
+ parameterBuilder.addApplicationTelemetry(
316
+ this.config.telemetry.application
317
+ );
318
+ parameterBuilder.addThrottling();
319
+
320
+ if (this.serverTelemetryManager) {
321
+ parameterBuilder.addServerTelemetry(this.serverTelemetryManager);
322
+ }
323
+
324
+ const correlationId =
325
+ request.correlationId ||
326
+ this.config.cryptoInterface.createNewGuid();
327
+ parameterBuilder.addCorrelationId(correlationId);
328
+
329
+ parameterBuilder.addRequestTokenUse(AADServerParamKeys.ON_BEHALF_OF);
330
+
331
+ parameterBuilder.addOboAssertion(request.oboAssertion);
332
+
333
+ if (this.config.clientCredentials.clientSecret) {
334
+ parameterBuilder.addClientSecret(
335
+ this.config.clientCredentials.clientSecret
336
+ );
337
+ }
338
+
339
+ if (this.config.clientCredentials.clientAssertion) {
340
+ const clientAssertion =
341
+ this.config.clientCredentials.clientAssertion;
342
+ parameterBuilder.addClientAssertion(clientAssertion.assertion);
343
+ parameterBuilder.addClientAssertionType(
344
+ clientAssertion.assertionType
345
+ );
346
+ }
347
+
348
+ if (
349
+ request.claims ||
350
+ (this.config.authOptions.clientCapabilities &&
351
+ this.config.authOptions.clientCapabilities.length > 0)
352
+ ) {
353
+ parameterBuilder.addClaims(
354
+ request.claims,
355
+ this.config.authOptions.clientCapabilities
356
+ );
357
+ }
358
+
359
+ return parameterBuilder.createQueryString();
360
+ }
361
+ }