@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,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
|
+
}
|