@azure/msal-common 14.1.0 → 14.3.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 (149) hide show
  1. package/dist/account/AuthToken.mjs +1 -1
  2. package/dist/account/CcsCredential.mjs +1 -1
  3. package/dist/account/ClientInfo.mjs +1 -1
  4. package/dist/authority/Authority.d.ts +25 -11
  5. package/dist/authority/Authority.d.ts.map +1 -1
  6. package/dist/authority/Authority.mjs +122 -71
  7. package/dist/authority/Authority.mjs.map +1 -1
  8. package/dist/authority/AuthorityFactory.d.ts.map +1 -1
  9. package/dist/authority/AuthorityFactory.mjs +3 -3
  10. package/dist/authority/AuthorityMetadata.d.ts +42 -162
  11. package/dist/authority/AuthorityMetadata.d.ts.map +1 -1
  12. package/dist/authority/AuthorityMetadata.mjs +106 -384
  13. package/dist/authority/AuthorityMetadata.mjs.map +1 -1
  14. package/dist/authority/AuthorityOptions.d.ts +6 -0
  15. package/dist/authority/AuthorityOptions.d.ts.map +1 -1
  16. package/dist/authority/AuthorityOptions.mjs +1 -1
  17. package/dist/authority/AuthorityOptions.mjs.map +1 -1
  18. package/dist/authority/AuthorityType.mjs +1 -1
  19. package/dist/authority/CloudInstanceDiscoveryErrorResponse.mjs +1 -1
  20. package/dist/authority/CloudInstanceDiscoveryResponse.mjs +1 -1
  21. package/dist/authority/OpenIdConfigResponse.mjs +1 -1
  22. package/dist/authority/ProtocolMode.mjs +1 -1
  23. package/dist/authority/RegionDiscovery.mjs +1 -1
  24. package/dist/cache/CacheManager.d.ts +17 -1
  25. package/dist/cache/CacheManager.d.ts.map +1 -1
  26. package/dist/cache/CacheManager.mjs +79 -37
  27. package/dist/cache/CacheManager.mjs.map +1 -1
  28. package/dist/cache/entities/AccessTokenEntity.d.ts +12 -43
  29. package/dist/cache/entities/AccessTokenEntity.d.ts.map +1 -1
  30. package/dist/cache/entities/AccountEntity.d.ts.map +1 -1
  31. package/dist/cache/entities/AccountEntity.mjs +1 -3
  32. package/dist/cache/entities/AccountEntity.mjs.map +1 -1
  33. package/dist/cache/entities/AppMetadataEntity.mjs +1 -1
  34. package/dist/cache/entities/AuthorityMetadataEntity.mjs +1 -1
  35. package/dist/cache/entities/CacheRecord.mjs +1 -1
  36. package/dist/cache/entities/CredentialEntity.d.ts +15 -73
  37. package/dist/cache/entities/CredentialEntity.d.ts.map +1 -1
  38. package/dist/cache/entities/IdTokenEntity.d.ts +4 -30
  39. package/dist/cache/entities/IdTokenEntity.d.ts.map +1 -1
  40. package/dist/cache/entities/RefreshTokenEntity.d.ts +2 -33
  41. package/dist/cache/entities/RefreshTokenEntity.d.ts.map +1 -1
  42. package/dist/cache/entities/ServerTelemetryEntity.mjs +1 -1
  43. package/dist/cache/entities/ThrottlingEntity.mjs +1 -1
  44. package/dist/cache/persistence/TokenCacheContext.mjs +1 -1
  45. package/dist/cache/utils/CacheHelpers.d.ts +59 -0
  46. package/dist/cache/utils/CacheHelpers.d.ts.map +1 -0
  47. package/dist/cache/utils/CacheHelpers.mjs +226 -0
  48. package/dist/cache/utils/CacheHelpers.mjs.map +1 -0
  49. package/dist/cache/utils/CacheTypes.d.ts +1 -0
  50. package/dist/cache/utils/CacheTypes.d.ts.map +1 -1
  51. package/dist/client/AuthorizationCodeClient.d.ts.map +1 -1
  52. package/dist/client/AuthorizationCodeClient.mjs +3 -3
  53. package/dist/client/AuthorizationCodeClient.mjs.map +1 -1
  54. package/dist/client/BaseClient.d.ts +2 -2
  55. package/dist/client/BaseClient.d.ts.map +1 -1
  56. package/dist/client/BaseClient.mjs +11 -10
  57. package/dist/client/BaseClient.mjs.map +1 -1
  58. package/dist/client/RefreshTokenClient.d.ts.map +1 -1
  59. package/dist/client/RefreshTokenClient.mjs +4 -4
  60. package/dist/client/RefreshTokenClient.mjs.map +1 -1
  61. package/dist/client/SilentFlowClient.d.ts +1 -0
  62. package/dist/client/SilentFlowClient.d.ts.map +1 -1
  63. package/dist/client/SilentFlowClient.mjs +20 -27
  64. package/dist/client/SilentFlowClient.mjs.map +1 -1
  65. package/dist/config/ClientConfiguration.mjs +1 -1
  66. package/dist/crypto/ICrypto.mjs +1 -1
  67. package/dist/crypto/JoseHeader.mjs +1 -1
  68. package/dist/crypto/PopTokenGenerator.mjs +1 -1
  69. package/dist/error/AuthError.mjs +1 -1
  70. package/dist/error/AuthErrorCodes.mjs +1 -1
  71. package/dist/error/ClientAuthError.d.ts +5 -0
  72. package/dist/error/ClientAuthError.d.ts.map +1 -1
  73. package/dist/error/ClientAuthError.mjs +7 -2
  74. package/dist/error/ClientAuthError.mjs.map +1 -1
  75. package/dist/error/ClientAuthErrorCodes.d.ts +1 -0
  76. package/dist/error/ClientAuthErrorCodes.d.ts.map +1 -1
  77. package/dist/error/ClientAuthErrorCodes.mjs +3 -2
  78. package/dist/error/ClientAuthErrorCodes.mjs.map +1 -1
  79. package/dist/error/ClientConfigurationError.mjs +1 -1
  80. package/dist/error/ClientConfigurationErrorCodes.mjs +1 -1
  81. package/dist/error/InteractionRequiredAuthError.mjs +1 -1
  82. package/dist/error/InteractionRequiredAuthErrorCodes.mjs +1 -1
  83. package/dist/error/JoseHeaderError.mjs +1 -1
  84. package/dist/error/JoseHeaderErrorCodes.mjs +1 -1
  85. package/dist/error/ServerError.mjs +1 -1
  86. package/dist/index.cjs +5157 -5476
  87. package/dist/index.cjs.map +1 -1
  88. package/dist/index.d.ts +3 -2
  89. package/dist/index.d.ts.map +1 -1
  90. package/dist/index.mjs +4 -6
  91. package/dist/index.mjs.map +1 -1
  92. package/dist/logger/Logger.mjs +1 -1
  93. package/dist/network/INetworkModule.mjs +1 -1
  94. package/dist/network/NetworkManager.mjs +1 -1
  95. package/dist/network/ThrottlingUtils.mjs +1 -1
  96. package/dist/packageMetadata.d.ts +1 -1
  97. package/dist/packageMetadata.mjs +2 -2
  98. package/dist/request/AuthenticationHeaderParser.mjs +1 -1
  99. package/dist/request/RequestParameterBuilder.mjs +1 -1
  100. package/dist/request/RequestValidator.mjs +1 -1
  101. package/dist/request/ScopeSet.mjs +1 -1
  102. package/dist/response/ResponseHandler.d.ts +2 -2
  103. package/dist/response/ResponseHandler.d.ts.map +1 -1
  104. package/dist/response/ResponseHandler.mjs +30 -32
  105. package/dist/response/ResponseHandler.mjs.map +1 -1
  106. package/dist/telemetry/performance/PerformanceClient.mjs +1 -1
  107. package/dist/telemetry/performance/PerformanceEvent.d.ts +17 -1
  108. package/dist/telemetry/performance/PerformanceEvent.d.ts.map +1 -1
  109. package/dist/telemetry/performance/PerformanceEvent.mjs +9 -2
  110. package/dist/telemetry/performance/PerformanceEvent.mjs.map +1 -1
  111. package/dist/telemetry/performance/StubPerformanceClient.mjs +1 -1
  112. package/dist/telemetry/server/ServerTelemetryManager.mjs +1 -1
  113. package/dist/url/UrlString.mjs +1 -1
  114. package/dist/utils/Constants.mjs +1 -1
  115. package/dist/utils/FunctionWrappers.mjs +1 -1
  116. package/dist/utils/ProtocolUtils.mjs +1 -1
  117. package/dist/utils/StringUtils.mjs +1 -1
  118. package/dist/utils/TimeUtils.mjs +1 -1
  119. package/package.json +1 -1
  120. package/src/authority/Authority.ts +205 -127
  121. package/src/authority/AuthorityFactory.ts +4 -4
  122. package/src/authority/AuthorityMetadata.ts +151 -390
  123. package/src/authority/AuthorityOptions.ts +9 -0
  124. package/src/cache/CacheManager.ts +115 -56
  125. package/src/cache/entities/AccessTokenEntity.ts +13 -149
  126. package/src/cache/entities/AccountEntity.ts +0 -3
  127. package/src/cache/entities/CredentialEntity.ts +16 -200
  128. package/src/cache/entities/IdTokenEntity.ts +4 -64
  129. package/src/cache/entities/RefreshTokenEntity.ts +2 -69
  130. package/src/cache/utils/CacheHelpers.ts +306 -0
  131. package/src/cache/utils/CacheTypes.ts +1 -0
  132. package/src/client/AuthorizationCodeClient.ts +10 -4
  133. package/src/client/BaseClient.ts +29 -15
  134. package/src/client/RefreshTokenClient.ts +17 -7
  135. package/src/client/SilentFlowClient.ts +49 -49
  136. package/src/error/ClientAuthError.ts +8 -0
  137. package/src/error/ClientAuthErrorCodes.ts +1 -0
  138. package/src/index.ts +7 -1
  139. package/src/packageMetadata.ts +1 -1
  140. package/src/response/ResponseHandler.ts +45 -44
  141. package/src/telemetry/performance/PerformanceEvent.ts +24 -2
  142. package/dist/cache/entities/AccessTokenEntity.mjs +0 -121
  143. package/dist/cache/entities/AccessTokenEntity.mjs.map +0 -1
  144. package/dist/cache/entities/CredentialEntity.mjs +0 -145
  145. package/dist/cache/entities/CredentialEntity.mjs.map +0 -1
  146. package/dist/cache/entities/IdTokenEntity.mjs +0 -64
  147. package/dist/cache/entities/IdTokenEntity.mjs.map +0 -1
  148. package/dist/cache/entities/RefreshTokenEntity.mjs +0 -67
  149. package/dist/cache/entities/RefreshTokenEntity.mjs.map +0 -1
@@ -3,218 +3,34 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
- import {
7
- Separators,
8
- CredentialType,
9
- CacheType,
10
- Constants,
11
- AuthenticationScheme,
12
- } from "../../utils/Constants";
13
- import {
14
- ClientAuthErrorCodes,
15
- createClientAuthError,
16
- } from "../../error/ClientAuthError";
6
+ import { CredentialType, AuthenticationScheme } from "../../utils/Constants";
17
7
 
18
8
  /**
19
- * Base type for credentials to be stored in the cache: eg: ACCESS_TOKEN, ID_TOKEN etc
20
- *
21
- * Key:Value Schema:
22
- *
23
- * Key: <home_account_id*>-<environment>-<credential_type>-<client_id>-<realm*>-<target*>-<requestedClaims*>-<scheme*>
24
- *
25
- * Value Schema:
26
- * {
27
- * homeAccountId: home account identifier for the auth scheme,
28
- * environment: entity that issued the token, represented as a full host
29
- * credentialType: Type of credential as a string, can be one of the following: RefreshToken, AccessToken, IdToken, Password, Cookie, Certificate, Other
30
- * clientId: client ID of the application
31
- * secret: Actual credential as a string
32
- * familyId: Family ID identifier, usually only used for refresh tokens
33
- * realm: Full tenant or organizational identifier that the account belongs to
34
- * target: Permissions that are included in the token, or for refresh tokens, the resource identifier.
35
- * tokenType: Matches the authentication scheme for which the token was issued (i.e. Bearer or pop)
36
- * requestedClaimsHash: Matches the SHA 256 hash of the claims object included in the token request
37
- * userAssertionHash: Matches the SHA 256 hash of the obo_assertion for the OBO flow
38
- * }
9
+ * Credential Cache Type
39
10
  */
40
- export class CredentialEntity {
11
+ export type CredentialEntity = {
12
+ /** Identifier for the user in their home tenant*/
41
13
  homeAccountId: string;
14
+ /** Entity that issued the token, represented as a full host */
42
15
  environment: string;
16
+ /** Type of credential */
43
17
  credentialType: CredentialType;
18
+ /** Client ID of the application */
44
19
  clientId: string;
20
+ /** Actual credential as a string */
45
21
  secret: string;
22
+ /** Family ID identifier, usually only used for refresh tokens */
46
23
  familyId?: string;
24
+ /** Full tenant or organizational identifier that the account belongs to */
47
25
  realm?: string;
26
+ /** Permissions that are included in the token, or for refresh tokens, the resource identifier. */
48
27
  target?: string;
28
+ /** Matches the SHA 256 hash of the obo_assertion for the OBO flow */
49
29
  userAssertionHash?: string;
30
+ /** Matches the authentication scheme for which the token was issued (i.e. Bearer or pop) */
50
31
  tokenType?: AuthenticationScheme;
32
+ /** KeyId for PoP and SSH tokens stored in the kid claim */
51
33
  keyId?: string;
34
+ /** Matches the SHA 256 hash of the claims object included in the token request */
52
35
  requestedClaimsHash?: string;
53
-
54
- /**
55
- * Generate Account Id key component as per the schema: <home_account_id>-<environment>
56
- */
57
- generateAccountId(): string {
58
- return CredentialEntity.generateAccountIdForCacheKey(
59
- this.homeAccountId,
60
- this.environment
61
- );
62
- }
63
-
64
- /**
65
- * Generate Credential Id key component as per the schema: <credential_type>-<client_id>-<realm>
66
- */
67
- generateCredentialId(): string {
68
- return CredentialEntity.generateCredentialIdForCacheKey(
69
- this.credentialType,
70
- this.clientId,
71
- this.realm,
72
- this.familyId
73
- );
74
- }
75
-
76
- /**
77
- * Generate target key component as per schema: <target>
78
- */
79
- generateTarget(): string {
80
- return CredentialEntity.generateTargetForCacheKey(this.target);
81
- }
82
-
83
- /**
84
- * generates credential key
85
- */
86
- generateCredentialKey(): string {
87
- return CredentialEntity.generateCredentialCacheKey(
88
- this.homeAccountId,
89
- this.environment,
90
- this.credentialType,
91
- this.clientId,
92
- this.realm,
93
- this.target,
94
- this.familyId,
95
- this.tokenType,
96
- this.requestedClaimsHash
97
- );
98
- }
99
-
100
- /**
101
- * returns the type of the cache (in this case credential)
102
- */
103
- generateType(): number {
104
- switch (this.credentialType) {
105
- case CredentialType.ID_TOKEN:
106
- return CacheType.ID_TOKEN;
107
- case CredentialType.ACCESS_TOKEN:
108
- case CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME:
109
- return CacheType.ACCESS_TOKEN;
110
- case CredentialType.REFRESH_TOKEN:
111
- return CacheType.REFRESH_TOKEN;
112
- default: {
113
- throw createClientAuthError(
114
- ClientAuthErrorCodes.unexpectedCredentialType
115
- );
116
- }
117
- }
118
- }
119
-
120
- /**
121
- * generates credential key
122
- * <home_account_id*>-\<environment>-<credential_type>-<client_id>-<realm\*>-<target\*>-<scheme\*>
123
- */
124
- static generateCredentialCacheKey(
125
- homeAccountId: string,
126
- environment: string,
127
- credentialType: CredentialType,
128
- clientId: string,
129
- realm?: string,
130
- target?: string,
131
- familyId?: string,
132
- tokenType?: AuthenticationScheme,
133
- requestedClaimsHash?: string
134
- ): string {
135
- const credentialKey = [
136
- this.generateAccountIdForCacheKey(homeAccountId, environment),
137
- this.generateCredentialIdForCacheKey(
138
- credentialType,
139
- clientId,
140
- realm,
141
- familyId
142
- ),
143
- this.generateTargetForCacheKey(target),
144
- this.generateClaimsHashForCacheKey(requestedClaimsHash),
145
- this.generateSchemeForCacheKey(tokenType),
146
- ];
147
-
148
- return credentialKey.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
149
- }
150
-
151
- /**
152
- * generates Account Id for keys
153
- * @param homeAccountId
154
- * @param environment
155
- */
156
- private static generateAccountIdForCacheKey(
157
- homeAccountId: string,
158
- environment: string
159
- ): string {
160
- const accountId: Array<string> = [homeAccountId, environment];
161
- return accountId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
162
- }
163
-
164
- /**
165
- * Generates Credential Id for keys
166
- * @param credentialType
167
- * @param realm
168
- * @param clientId
169
- * @param familyId
170
- */
171
- private static generateCredentialIdForCacheKey(
172
- credentialType: CredentialType,
173
- clientId: string,
174
- realm?: string,
175
- familyId?: string
176
- ): string {
177
- const clientOrFamilyId =
178
- credentialType === CredentialType.REFRESH_TOKEN
179
- ? familyId || clientId
180
- : clientId;
181
- const credentialId: Array<string> = [
182
- credentialType,
183
- clientOrFamilyId,
184
- realm || Constants.EMPTY_STRING,
185
- ];
186
-
187
- return credentialId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
188
- }
189
-
190
- /**
191
- * Generate target key component as per schema: <target>
192
- */
193
- private static generateTargetForCacheKey(scopes?: string): string {
194
- return (scopes || Constants.EMPTY_STRING).toLowerCase();
195
- }
196
-
197
- /**
198
- * Generate requested claims key component as per schema: <requestedClaims>
199
- */
200
- private static generateClaimsHashForCacheKey(
201
- requestedClaimsHash?: string
202
- ): string {
203
- return (requestedClaimsHash || Constants.EMPTY_STRING).toLowerCase();
204
- }
205
-
206
- /**
207
- * Generate scheme key componenet as per schema: <scheme>
208
- */
209
- private static generateSchemeForCacheKey(tokenType?: string): string {
210
- /*
211
- * PoP Tokens and SSH certs include scheme in cache key
212
- * Cast to lowercase to handle "bearer" from ADFS
213
- */
214
- return tokenType &&
215
- tokenType.toLowerCase() !==
216
- AuthenticationScheme.BEARER.toLowerCase()
217
- ? tokenType.toLowerCase()
218
- : Constants.EMPTY_STRING;
219
- }
220
- }
36
+ };
@@ -4,71 +4,11 @@
4
4
  */
5
5
 
6
6
  import { CredentialEntity } from "./CredentialEntity";
7
- import { CredentialType } from "../../utils/Constants";
8
7
 
9
8
  /**
10
- * ID_TOKEN Cache
11
- *
12
- * Key:Value Schema:
13
- *
14
- * Key Example: uid.utid-login.microsoftonline.com-idtoken-clientId-contoso.com-
15
- *
16
- * Value Schema:
17
- * {
18
- * homeAccountId: home account identifier for the auth scheme,
19
- * environment: entity that issued the token, represented as a full host
20
- * credentialType: Type of credential as a string, can be one of the following: RefreshToken, AccessToken, IdToken, Password, Cookie, Certificate, Other
21
- * clientId: client ID of the application
22
- * secret: Actual credential as a string
23
- * realm: Full tenant or organizational identifier that the account belongs to
24
- * }
9
+ * Id Token Cache Type
25
10
  */
26
- export class IdTokenEntity extends CredentialEntity {
11
+ export type IdTokenEntity = CredentialEntity & {
12
+ /** Full tenant or organizational identifier that the account belongs to */
27
13
  realm: string;
28
-
29
- /**
30
- * Create IdTokenEntity
31
- * @param homeAccountId
32
- * @param authenticationResult
33
- * @param clientId
34
- * @param authority
35
- */
36
- static createIdTokenEntity(
37
- homeAccountId: string,
38
- environment: string,
39
- idToken: string,
40
- clientId: string,
41
- tenantId: string
42
- ): IdTokenEntity {
43
- const idTokenEntity = new IdTokenEntity();
44
-
45
- idTokenEntity.credentialType = CredentialType.ID_TOKEN;
46
- idTokenEntity.homeAccountId = homeAccountId;
47
- idTokenEntity.environment = environment;
48
- idTokenEntity.clientId = clientId;
49
- idTokenEntity.secret = idToken;
50
- idTokenEntity.realm = tenantId;
51
-
52
- return idTokenEntity;
53
- }
54
-
55
- /**
56
- * Validates an entity: checks for all expected params
57
- * @param entity
58
- */
59
- static isIdTokenEntity(entity: object): boolean {
60
- if (!entity) {
61
- return false;
62
- }
63
-
64
- return (
65
- entity.hasOwnProperty("homeAccountId") &&
66
- entity.hasOwnProperty("environment") &&
67
- entity.hasOwnProperty("credentialType") &&
68
- entity.hasOwnProperty("realm") &&
69
- entity.hasOwnProperty("clientId") &&
70
- entity.hasOwnProperty("secret") &&
71
- entity["credentialType"] === CredentialType.ID_TOKEN
72
- );
73
- }
74
- }
14
+ };
@@ -4,75 +4,8 @@
4
4
  */
5
5
 
6
6
  import { CredentialEntity } from "./CredentialEntity";
7
- import { CredentialType } from "../../utils/Constants";
8
7
 
9
8
  /**
10
- * REFRESH_TOKEN Cache
11
- *
12
- * Key:Value Schema:
13
- *
14
- * Key Example: uid.utid-login.microsoftonline.com-refreshtoken-clientId--
15
- *
16
- * Value:
17
- * {
18
- * homeAccountId: home account identifier for the auth scheme,
19
- * environment: entity that issued the token, represented as a full host
20
- * credentialType: Type of credential as a string, can be one of the following: RefreshToken, AccessToken, IdToken, Password, Cookie, Certificate, Other
21
- * clientId: client ID of the application
22
- * secret: Actual credential as a string
23
- * familyId: Family ID identifier, '1' represents Microsoft Family
24
- * realm: Full tenant or organizational identifier that the account belongs to
25
- * target: Permissions that are included in the token, or for refresh tokens, the resource identifier.
26
- * }
9
+ * Refresh Token Cache Type
27
10
  */
28
- export class RefreshTokenEntity extends CredentialEntity {
29
- familyId?: string;
30
-
31
- /**
32
- * Create RefreshTokenEntity
33
- * @param homeAccountId
34
- * @param authenticationResult
35
- * @param clientId
36
- * @param authority
37
- */
38
- static createRefreshTokenEntity(
39
- homeAccountId: string,
40
- environment: string,
41
- refreshToken: string,
42
- clientId: string,
43
- familyId?: string,
44
- userAssertionHash?: string
45
- ): RefreshTokenEntity {
46
- const rtEntity = new RefreshTokenEntity();
47
-
48
- rtEntity.clientId = clientId;
49
- rtEntity.credentialType = CredentialType.REFRESH_TOKEN;
50
- rtEntity.environment = environment;
51
- rtEntity.homeAccountId = homeAccountId;
52
- rtEntity.secret = refreshToken;
53
- rtEntity.userAssertionHash = userAssertionHash;
54
-
55
- if (familyId) rtEntity.familyId = familyId;
56
-
57
- return rtEntity;
58
- }
59
-
60
- /**
61
- * Validates an entity: checks for all expected params
62
- * @param entity
63
- */
64
- static isRefreshTokenEntity(entity: object): boolean {
65
- if (!entity) {
66
- return false;
67
- }
68
-
69
- return (
70
- entity.hasOwnProperty("homeAccountId") &&
71
- entity.hasOwnProperty("environment") &&
72
- entity.hasOwnProperty("credentialType") &&
73
- entity.hasOwnProperty("clientId") &&
74
- entity.hasOwnProperty("secret") &&
75
- entity["credentialType"] === CredentialType.REFRESH_TOKEN
76
- );
77
- }
78
- }
11
+ export type RefreshTokenEntity = CredentialEntity;
@@ -0,0 +1,306 @@
1
+ /*
2
+ * Copyright (c) Microsoft Corporation. All rights reserved.
3
+ * Licensed under the MIT License.
4
+ */
5
+
6
+ import { extractTokenClaims } from "../../account/AuthToken";
7
+ import { TokenClaims } from "../../account/TokenClaims";
8
+ import {
9
+ ClientAuthErrorCodes,
10
+ createClientAuthError,
11
+ } from "../../error/ClientAuthError";
12
+ import {
13
+ AuthenticationScheme,
14
+ CredentialType,
15
+ Separators,
16
+ } from "../../utils/Constants";
17
+ import { TimeUtils } from "../../utils/TimeUtils";
18
+ import { AccessTokenEntity } from "../entities/AccessTokenEntity";
19
+ import { CredentialEntity } from "../entities/CredentialEntity";
20
+ import { IdTokenEntity } from "../entities/IdTokenEntity";
21
+ import { RefreshTokenEntity } from "../entities/RefreshTokenEntity";
22
+
23
+ /**
24
+ * Cache Key: <home_account_id>-<environment>-<credential_type>-<client_id or familyId>-<realm>-<scopes>-<claims hash>-<scheme>
25
+ * IdToken Example: uid.utid-login.microsoftonline.com-idtoken-app_client_id-contoso.com
26
+ * AccessToken Example: uid.utid-login.microsoftonline.com-accesstoken-app_client_id-contoso.com-scope1 scope2--pop
27
+ * RefreshToken Example: uid.utid-login.microsoftonline.com-refreshtoken-1-contoso.com
28
+ * @param credentialEntity
29
+ * @returns
30
+ */
31
+ export function generateCredentialKey(
32
+ credentialEntity: CredentialEntity
33
+ ): string {
34
+ const credentialKey = [
35
+ generateAccountId(credentialEntity),
36
+ generateCredentialId(credentialEntity),
37
+ generateTarget(credentialEntity),
38
+ generateClaimsHash(credentialEntity),
39
+ generateScheme(credentialEntity),
40
+ ];
41
+
42
+ return credentialKey.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
43
+ }
44
+
45
+ /**
46
+ * Create IdTokenEntity
47
+ * @param homeAccountId
48
+ * @param authenticationResult
49
+ * @param clientId
50
+ * @param authority
51
+ */
52
+ export function createIdTokenEntity(
53
+ homeAccountId: string,
54
+ environment: string,
55
+ idToken: string,
56
+ clientId: string,
57
+ tenantId: string
58
+ ): IdTokenEntity {
59
+ const idTokenEntity: IdTokenEntity = {
60
+ credentialType: CredentialType.ID_TOKEN,
61
+ homeAccountId: homeAccountId,
62
+ environment: environment,
63
+ clientId: clientId,
64
+ secret: idToken,
65
+ realm: tenantId,
66
+ };
67
+
68
+ return idTokenEntity;
69
+ }
70
+
71
+ /**
72
+ * Create AccessTokenEntity
73
+ * @param homeAccountId
74
+ * @param environment
75
+ * @param accessToken
76
+ * @param clientId
77
+ * @param tenantId
78
+ * @param scopes
79
+ * @param expiresOn
80
+ * @param extExpiresOn
81
+ */
82
+ export function createAccessTokenEntity(
83
+ homeAccountId: string,
84
+ environment: string,
85
+ accessToken: string,
86
+ clientId: string,
87
+ tenantId: string,
88
+ scopes: string,
89
+ expiresOn: number,
90
+ extExpiresOn: number,
91
+ base64Decode: (input: string) => string,
92
+ refreshOn?: number,
93
+ tokenType?: AuthenticationScheme,
94
+ userAssertionHash?: string,
95
+ keyId?: string,
96
+ requestedClaims?: string,
97
+ requestedClaimsHash?: string
98
+ ): AccessTokenEntity {
99
+ const atEntity: AccessTokenEntity = {
100
+ homeAccountId: homeAccountId,
101
+ credentialType: CredentialType.ACCESS_TOKEN,
102
+ secret: accessToken,
103
+ cachedAt: TimeUtils.nowSeconds().toString(),
104
+ expiresOn: expiresOn.toString(),
105
+ extendedExpiresOn: extExpiresOn.toString(),
106
+ environment: environment,
107
+ clientId: clientId,
108
+ realm: tenantId,
109
+ target: scopes,
110
+ tokenType: tokenType || AuthenticationScheme.BEARER,
111
+ };
112
+
113
+ if (userAssertionHash) {
114
+ atEntity.userAssertionHash = userAssertionHash;
115
+ }
116
+
117
+ if (refreshOn) {
118
+ atEntity.refreshOn = refreshOn.toString();
119
+ }
120
+
121
+ if (requestedClaims) {
122
+ atEntity.requestedClaims = requestedClaims;
123
+ atEntity.requestedClaimsHash = requestedClaimsHash;
124
+ }
125
+
126
+ /*
127
+ * Create Access Token With Auth Scheme instead of regular access token
128
+ * Cast to lower to handle "bearer" from ADFS
129
+ */
130
+ if (
131
+ atEntity.tokenType?.toLowerCase() !==
132
+ AuthenticationScheme.BEARER.toLowerCase()
133
+ ) {
134
+ atEntity.credentialType = CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME;
135
+ switch (atEntity.tokenType) {
136
+ case AuthenticationScheme.POP:
137
+ // Make sure keyId is present and add it to credential
138
+ const tokenClaims: TokenClaims | null = extractTokenClaims(
139
+ accessToken,
140
+ base64Decode
141
+ );
142
+ if (!tokenClaims?.cnf?.kid) {
143
+ throw createClientAuthError(
144
+ ClientAuthErrorCodes.tokenClaimsCnfRequiredForSignedJwt
145
+ );
146
+ }
147
+ atEntity.keyId = tokenClaims.cnf.kid;
148
+ break;
149
+ case AuthenticationScheme.SSH:
150
+ atEntity.keyId = keyId;
151
+ }
152
+ }
153
+
154
+ return atEntity;
155
+ }
156
+
157
+ /**
158
+ * Create RefreshTokenEntity
159
+ * @param homeAccountId
160
+ * @param authenticationResult
161
+ * @param clientId
162
+ * @param authority
163
+ */
164
+ export function createRefreshTokenEntity(
165
+ homeAccountId: string,
166
+ environment: string,
167
+ refreshToken: string,
168
+ clientId: string,
169
+ familyId?: string,
170
+ userAssertionHash?: string
171
+ ): RefreshTokenEntity {
172
+ const rtEntity: RefreshTokenEntity = {
173
+ credentialType: CredentialType.REFRESH_TOKEN,
174
+ homeAccountId: homeAccountId,
175
+ environment: environment,
176
+ clientId: clientId,
177
+ secret: refreshToken,
178
+ };
179
+
180
+ if (userAssertionHash) {
181
+ rtEntity.userAssertionHash = userAssertionHash;
182
+ }
183
+
184
+ if (familyId) {
185
+ rtEntity.familyId = familyId;
186
+ }
187
+
188
+ return rtEntity;
189
+ }
190
+
191
+ export function isCredentialEntity(entity: object): boolean {
192
+ return (
193
+ entity.hasOwnProperty("homeAccountId") &&
194
+ entity.hasOwnProperty("environment") &&
195
+ entity.hasOwnProperty("credentialType") &&
196
+ entity.hasOwnProperty("clientId") &&
197
+ entity.hasOwnProperty("secret")
198
+ );
199
+ }
200
+
201
+ /**
202
+ * Validates an entity: checks for all expected params
203
+ * @param entity
204
+ */
205
+ export function isAccessTokenEntity(entity: object): boolean {
206
+ if (!entity) {
207
+ return false;
208
+ }
209
+
210
+ return (
211
+ isCredentialEntity(entity) &&
212
+ entity.hasOwnProperty("realm") &&
213
+ entity.hasOwnProperty("target") &&
214
+ (entity["credentialType"] === CredentialType.ACCESS_TOKEN ||
215
+ entity["credentialType"] ===
216
+ CredentialType.ACCESS_TOKEN_WITH_AUTH_SCHEME)
217
+ );
218
+ }
219
+
220
+ /**
221
+ * Validates an entity: checks for all expected params
222
+ * @param entity
223
+ */
224
+ export function isIdTokenEntity(entity: object): boolean {
225
+ if (!entity) {
226
+ return false;
227
+ }
228
+
229
+ return (
230
+ isCredentialEntity(entity) &&
231
+ entity.hasOwnProperty("realm") &&
232
+ entity["credentialType"] === CredentialType.ID_TOKEN
233
+ );
234
+ }
235
+
236
+ /**
237
+ * Validates an entity: checks for all expected params
238
+ * @param entity
239
+ */
240
+ export function isRefreshTokenEntity(entity: object): boolean {
241
+ if (!entity) {
242
+ return false;
243
+ }
244
+
245
+ return (
246
+ isCredentialEntity(entity) &&
247
+ entity["credentialType"] === CredentialType.REFRESH_TOKEN
248
+ );
249
+ }
250
+
251
+ /**
252
+ * Generate Account Id key component as per the schema: <home_account_id>-<environment>
253
+ */
254
+ function generateAccountId(credentialEntity: CredentialEntity): string {
255
+ const accountId: Array<string> = [
256
+ credentialEntity.homeAccountId,
257
+ credentialEntity.environment,
258
+ ];
259
+ return accountId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
260
+ }
261
+
262
+ /**
263
+ * Generate Credential Id key component as per the schema: <credential_type>-<client_id>-<realm>
264
+ */
265
+ function generateCredentialId(credentialEntity: CredentialEntity): string {
266
+ const clientOrFamilyId =
267
+ credentialEntity.credentialType === CredentialType.REFRESH_TOKEN
268
+ ? credentialEntity.familyId || credentialEntity.clientId
269
+ : credentialEntity.clientId;
270
+ const credentialId: Array<string> = [
271
+ credentialEntity.credentialType,
272
+ clientOrFamilyId,
273
+ credentialEntity.realm || "",
274
+ ];
275
+
276
+ return credentialId.join(Separators.CACHE_KEY_SEPARATOR).toLowerCase();
277
+ }
278
+
279
+ /**
280
+ * Generate target key component as per schema: <target>
281
+ */
282
+ function generateTarget(credentialEntity: CredentialEntity): string {
283
+ return (credentialEntity.target || "").toLowerCase();
284
+ }
285
+
286
+ /**
287
+ * Generate requested claims key component as per schema: <requestedClaims>
288
+ */
289
+ function generateClaimsHash(credentialEntity: CredentialEntity): string {
290
+ return (credentialEntity.requestedClaimsHash || "").toLowerCase();
291
+ }
292
+
293
+ /**
294
+ * Generate scheme key componenet as per schema: <scheme>
295
+ */
296
+ function generateScheme(credentialEntity: CredentialEntity): string {
297
+ /*
298
+ * PoP Tokens and SSH certs include scheme in cache key
299
+ * Cast to lowercase to handle "bearer" from ADFS
300
+ */
301
+ return credentialEntity.tokenType &&
302
+ credentialEntity.tokenType.toLowerCase() !==
303
+ AuthenticationScheme.BEARER.toLowerCase()
304
+ ? credentialEntity.tokenType.toLowerCase()
305
+ : "";
306
+ }
@@ -59,6 +59,7 @@ export type AccountFilter = Omit<
59
59
  > & {
60
60
  realm?: string;
61
61
  loginHint?: string;
62
+ sid?: string;
62
63
  };
63
64
 
64
65
  /**