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