@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.
- package/README.md +3 -1
- package/dist/cache/ITokenCache.d.ts +15 -15
- package/dist/cache/NodeStorage.cjs +401 -401
- package/dist/cache/NodeStorage.cjs.map +1 -1
- package/dist/cache/NodeStorage.d.ts +182 -182
- package/dist/cache/NodeStorage.d.ts.map +1 -1
- package/dist/cache/NodeStorage.mjs +401 -401
- package/dist/cache/NodeStorage.mjs.map +1 -1
- package/dist/cache/TokenCache.cjs +265 -269
- package/dist/cache/TokenCache.cjs.map +1 -1
- package/dist/cache/TokenCache.d.ts +90 -90
- package/dist/cache/TokenCache.d.ts.map +1 -1
- package/dist/cache/TokenCache.mjs +266 -270
- package/dist/cache/TokenCache.mjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.cjs +26 -26
- package/dist/cache/distributed/DistributedCachePlugin.cjs.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.d.ts +10 -10
- package/dist/cache/distributed/DistributedCachePlugin.d.ts.map +1 -1
- package/dist/cache/distributed/DistributedCachePlugin.mjs +26 -26
- package/dist/cache/distributed/DistributedCachePlugin.mjs.map +1 -1
- package/dist/cache/distributed/ICacheClient.d.ts +17 -17
- package/dist/cache/distributed/IPartitionManager.d.ts +28 -28
- package/dist/cache/serializer/Deserializer.cjs +173 -175
- package/dist/cache/serializer/Deserializer.cjs.map +1 -1
- package/dist/cache/serializer/Deserializer.d.ts +42 -42
- package/dist/cache/serializer/Deserializer.d.ts.map +1 -1
- package/dist/cache/serializer/Deserializer.mjs +174 -176
- package/dist/cache/serializer/Deserializer.mjs.map +1 -1
- package/dist/cache/serializer/Serializer.cjs +134 -134
- package/dist/cache/serializer/Serializer.cjs.map +1 -1
- package/dist/cache/serializer/Serializer.d.ts +39 -39
- package/dist/cache/serializer/Serializer.d.ts.map +1 -1
- package/dist/cache/serializer/Serializer.mjs +134 -134
- package/dist/cache/serializer/Serializer.mjs.map +1 -1
- package/dist/cache/serializer/SerializerTypes.d.ts +101 -101
- package/dist/client/ClientApplication.cjs +333 -333
- package/dist/client/ClientApplication.cjs.map +1 -1
- package/dist/client/ClientApplication.d.ts +142 -142
- package/dist/client/ClientApplication.d.ts.map +1 -1
- package/dist/client/ClientApplication.mjs +334 -334
- package/dist/client/ClientApplication.mjs.map +1 -1
- package/dist/client/ClientAssertion.cjs +116 -116
- package/dist/client/ClientAssertion.cjs.map +1 -1
- package/dist/client/ClientAssertion.d.ts +46 -46
- package/dist/client/ClientAssertion.d.ts.map +1 -1
- package/dist/client/ClientAssertion.mjs +117 -117
- package/dist/client/ClientAssertion.mjs.map +1 -1
- package/dist/client/ClientCredentialClient.cjs +189 -168
- package/dist/client/ClientCredentialClient.cjs.map +1 -1
- package/dist/client/ClientCredentialClient.d.ts +33 -33
- package/dist/client/ClientCredentialClient.d.ts.map +1 -1
- package/dist/client/ClientCredentialClient.mjs +190 -169
- package/dist/client/ClientCredentialClient.mjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.cjs +163 -164
- package/dist/client/ConfidentialClientApplication.cjs.map +1 -1
- package/dist/client/ConfidentialClientApplication.d.ts +58 -58
- package/dist/client/ConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/ConfidentialClientApplication.mjs +164 -165
- package/dist/client/ConfidentialClientApplication.mjs.map +1 -1
- package/dist/client/DeviceCodeClient.cjs +207 -207
- package/dist/client/DeviceCodeClient.cjs.map +1 -1
- package/dist/client/DeviceCodeClient.d.ts +53 -53
- package/dist/client/DeviceCodeClient.d.ts.map +1 -1
- package/dist/client/DeviceCodeClient.mjs +208 -208
- package/dist/client/DeviceCodeClient.mjs.map +1 -1
- package/dist/client/IConfidentialClientApplication.d.ts +39 -39
- package/dist/client/IConfidentialClientApplication.d.ts.map +1 -1
- package/dist/client/IPublicClientApplication.d.ts +42 -42
- package/dist/client/IPublicClientApplication.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.cjs +203 -204
- package/dist/client/OnBehalfOfClient.cjs.map +1 -1
- package/dist/client/OnBehalfOfClient.d.ts +47 -47
- package/dist/client/OnBehalfOfClient.d.ts.map +1 -1
- package/dist/client/OnBehalfOfClient.mjs +204 -205
- package/dist/client/OnBehalfOfClient.mjs.map +1 -1
- package/dist/client/PublicClientApplication.cjs +229 -175
- package/dist/client/PublicClientApplication.cjs.map +1 -1
- package/dist/client/PublicClientApplication.d.ts +71 -65
- package/dist/client/PublicClientApplication.d.ts.map +1 -1
- package/dist/client/PublicClientApplication.mjs +232 -178
- package/dist/client/PublicClientApplication.mjs.map +1 -1
- package/dist/client/UsernamePasswordClient.cjs +95 -95
- package/dist/client/UsernamePasswordClient.cjs.map +1 -1
- package/dist/client/UsernamePasswordClient.d.ts +25 -25
- package/dist/client/UsernamePasswordClient.d.ts.map +1 -1
- package/dist/client/UsernamePasswordClient.mjs +95 -95
- package/dist/client/UsernamePasswordClient.mjs.map +1 -1
- package/dist/config/Configuration.cjs +72 -72
- package/dist/config/Configuration.cjs.map +1 -1
- package/dist/config/Configuration.d.ts +107 -107
- package/dist/config/Configuration.mjs +72 -72
- package/dist/config/Configuration.mjs.map +1 -1
- package/dist/crypto/CryptoProvider.cjs +75 -75
- package/dist/crypto/CryptoProvider.cjs.map +1 -1
- package/dist/crypto/CryptoProvider.d.ts +52 -52
- package/dist/crypto/CryptoProvider.mjs +75 -75
- package/dist/crypto/CryptoProvider.mjs.map +1 -1
- package/dist/crypto/GuidGenerator.cjs +22 -22
- package/dist/crypto/GuidGenerator.cjs.map +1 -1
- package/dist/crypto/GuidGenerator.d.ts +14 -14
- package/dist/crypto/GuidGenerator.mjs +22 -22
- package/dist/crypto/GuidGenerator.mjs.map +1 -1
- package/dist/crypto/HashUtils.cjs +13 -13
- package/dist/crypto/HashUtils.cjs.map +1 -1
- package/dist/crypto/HashUtils.d.ts +8 -8
- package/dist/crypto/HashUtils.mjs +13 -13
- package/dist/crypto/HashUtils.mjs.map +1 -1
- package/dist/crypto/PkceGenerator.cjs +49 -49
- package/dist/crypto/PkceGenerator.cjs.map +1 -1
- package/dist/crypto/PkceGenerator.d.ts +22 -22
- package/dist/crypto/PkceGenerator.mjs +49 -49
- package/dist/crypto/PkceGenerator.mjs.map +1 -1
- package/dist/error/NodeAuthError.cjs +85 -85
- package/dist/error/NodeAuthError.cjs.map +1 -1
- package/dist/error/NodeAuthError.d.ts +65 -65
- package/dist/error/NodeAuthError.mjs +85 -85
- package/dist/error/NodeAuthError.mjs.map +1 -1
- package/dist/index.cjs +17 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +42 -42
- package/dist/index.d.ts.map +1 -1
- package/dist/index.mjs +2 -2
- package/dist/internals.cjs +8 -8
- package/dist/internals.cjs.map +1 -1
- package/dist/internals.d.ts +6 -6
- package/dist/internals.d.ts.map +1 -1
- package/dist/internals.mjs +8 -8
- package/dist/internals.mjs.map +1 -1
- package/dist/network/HttpClient.cjs +277 -276
- package/dist/network/HttpClient.cjs.map +1 -1
- package/dist/network/HttpClient.d.ts +25 -25
- package/dist/network/HttpClient.d.ts.map +1 -1
- package/dist/network/HttpClient.mjs +278 -277
- package/dist/network/HttpClient.mjs.map +1 -1
- package/dist/network/ILoopbackClient.d.ts +10 -10
- package/dist/network/LoopbackClient.cjs +75 -84
- package/dist/network/LoopbackClient.cjs.map +1 -1
- package/dist/network/LoopbackClient.d.ts +21 -21
- package/dist/network/LoopbackClient.d.ts.map +1 -1
- package/dist/network/LoopbackClient.mjs +77 -86
- package/dist/network/LoopbackClient.mjs.map +1 -1
- package/dist/packageMetadata.cjs +4 -4
- package/dist/packageMetadata.cjs.map +1 -1
- package/dist/packageMetadata.d.ts +2 -2
- package/dist/packageMetadata.mjs +4 -4
- package/dist/packageMetadata.mjs.map +1 -1
- package/dist/request/AuthorizationCodeRequest.d.ts +21 -21
- package/dist/request/AuthorizationUrlRequest.d.ts +33 -33
- package/dist/request/ClientCredentialRequest.d.ts +14 -14
- package/dist/request/DeviceCodeRequest.d.ts +15 -15
- package/dist/request/InteractiveRequest.d.ts +22 -22
- package/dist/request/InteractiveRequest.d.ts.map +1 -1
- package/dist/request/OnBehalfOfRequest.d.ts +14 -14
- package/dist/request/RefreshTokenRequest.d.ts +17 -17
- package/dist/request/SignOutRequest.d.ts +5 -5
- package/dist/request/SilentFlowRequest.d.ts +16 -16
- package/dist/request/UsernamePasswordRequest.d.ts +19 -19
- package/dist/utils/Constants.cjs +80 -90
- package/dist/utils/Constants.cjs.map +1 -1
- package/dist/utils/Constants.d.ts +86 -96
- package/dist/utils/Constants.d.ts.map +1 -1
- package/dist/utils/Constants.mjs +81 -90
- package/dist/utils/Constants.mjs.map +1 -1
- package/dist/utils/EncodingUtils.cjs +44 -44
- package/dist/utils/EncodingUtils.cjs.map +1 -1
- package/dist/utils/EncodingUtils.d.ts +26 -26
- package/dist/utils/EncodingUtils.mjs +44 -44
- package/dist/utils/EncodingUtils.mjs.map +1 -1
- package/dist/utils/NetworkUtils.cjs +38 -38
- package/dist/utils/NetworkUtils.cjs.map +1 -1
- package/dist/utils/NetworkUtils.d.ts +5 -5
- package/dist/utils/NetworkUtils.mjs +38 -38
- 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 +596 -0
- package/src/client/ClientAssertion.ts +165 -0
- package/src/client/ClientCredentialClient.ts +346 -0
- package/src/client/ConfidentialClientApplication.ts +268 -0
- package/src/client/DeviceCodeClient.ts +369 -0
- package/src/client/IConfidentialClientApplication.ts +72 -0
- package/src/client/IPublicClientApplication.ts +72 -0
- package/src/client/OnBehalfOfClient.ts +368 -0
- package/src/client/PublicClientApplication.ts +339 -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 +125 -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 +110 -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,339 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
ApiId,
|
|
8
|
+
Constants,
|
|
9
|
+
LOOPBACK_SERVER_CONSTANTS,
|
|
10
|
+
} from "../utils/Constants.js";
|
|
11
|
+
import {
|
|
12
|
+
AuthenticationResult,
|
|
13
|
+
CommonDeviceCodeRequest,
|
|
14
|
+
AuthError,
|
|
15
|
+
ResponseMode,
|
|
16
|
+
OIDC_DEFAULT_SCOPES,
|
|
17
|
+
CodeChallengeMethodValues,
|
|
18
|
+
Constants as CommonConstants,
|
|
19
|
+
ServerError,
|
|
20
|
+
NativeRequest,
|
|
21
|
+
NativeSignOutRequest,
|
|
22
|
+
AccountInfo,
|
|
23
|
+
INativeBrokerPlugin,
|
|
24
|
+
ServerAuthorizationCodeResponse,
|
|
25
|
+
} from "@azure/msal-common";
|
|
26
|
+
import { Configuration } from "../config/Configuration.js";
|
|
27
|
+
import { ClientApplication } from "./ClientApplication.js";
|
|
28
|
+
import { IPublicClientApplication } from "./IPublicClientApplication.js";
|
|
29
|
+
import { DeviceCodeRequest } from "../request/DeviceCodeRequest.js";
|
|
30
|
+
import { AuthorizationUrlRequest } from "../request/AuthorizationUrlRequest.js";
|
|
31
|
+
import { AuthorizationCodeRequest } from "../request/AuthorizationCodeRequest.js";
|
|
32
|
+
import { InteractiveRequest } from "../request/InteractiveRequest.js";
|
|
33
|
+
import { NodeAuthError, NodeAuthErrorMessage } from "../error/NodeAuthError.js";
|
|
34
|
+
import { LoopbackClient } from "../network/LoopbackClient.js";
|
|
35
|
+
import { SilentFlowRequest } from "../request/SilentFlowRequest.js";
|
|
36
|
+
import { SignOutRequest } from "../request/SignOutRequest.js";
|
|
37
|
+
import { ILoopbackClient } from "../network/ILoopbackClient.js";
|
|
38
|
+
import { DeviceCodeClient } from "./DeviceCodeClient.js";
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* This class is to be used to acquire tokens for public client applications (desktop, mobile). Public client applications
|
|
42
|
+
* are not trusted to safely store application secrets, and therefore can only request tokens in the name of an user.
|
|
43
|
+
* @public
|
|
44
|
+
*/
|
|
45
|
+
export class PublicClientApplication
|
|
46
|
+
extends ClientApplication
|
|
47
|
+
implements IPublicClientApplication
|
|
48
|
+
{
|
|
49
|
+
private nativeBrokerPlugin?: INativeBrokerPlugin;
|
|
50
|
+
/**
|
|
51
|
+
* Important attributes in the Configuration object for auth are:
|
|
52
|
+
* - clientID: the application ID of your application. You can obtain one by registering your application with our Application registration portal.
|
|
53
|
+
* - authority: the authority URL for your application.
|
|
54
|
+
*
|
|
55
|
+
* AAD authorities are of the form https://login.microsoftonline.com/\{Enter_the_Tenant_Info_Here\}.
|
|
56
|
+
* - If your application supports Accounts in one organizational directory, replace "Enter_the_Tenant_Info_Here" value with the Tenant Id or Tenant name (for example, contoso.microsoft.com).
|
|
57
|
+
* - If your application supports Accounts in any organizational directory, replace "Enter_the_Tenant_Info_Here" value with organizations.
|
|
58
|
+
* - If your application supports Accounts in any organizational directory and personal Microsoft accounts, replace "Enter_the_Tenant_Info_Here" value with common.
|
|
59
|
+
* - To restrict support to Personal Microsoft accounts only, replace "Enter_the_Tenant_Info_Here" value with consumers.
|
|
60
|
+
*
|
|
61
|
+
* Azure B2C authorities are of the form https://\{instance\}/\{tenant\}/\{policy\}. Each policy is considered
|
|
62
|
+
* its own authority. You will have to set the all of the knownAuthorities at the time of the client application
|
|
63
|
+
* construction.
|
|
64
|
+
*
|
|
65
|
+
* ADFS authorities are of the form https://\{instance\}/adfs.
|
|
66
|
+
*/
|
|
67
|
+
constructor(configuration: Configuration) {
|
|
68
|
+
super(configuration);
|
|
69
|
+
if (this.config.broker.nativeBrokerPlugin) {
|
|
70
|
+
if (this.config.broker.nativeBrokerPlugin.isBrokerAvailable) {
|
|
71
|
+
this.nativeBrokerPlugin = this.config.broker.nativeBrokerPlugin;
|
|
72
|
+
this.nativeBrokerPlugin.setLogger(
|
|
73
|
+
this.config.system.loggerOptions
|
|
74
|
+
);
|
|
75
|
+
} else {
|
|
76
|
+
this.logger.warning(
|
|
77
|
+
"NativeBroker implementation was provided but the broker is unavailable."
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Acquires a token from the authority using OAuth2.0 device code flow.
|
|
85
|
+
* This flow is designed for devices that do not have access to a browser or have input constraints.
|
|
86
|
+
* The authorization server issues a DeviceCode object with a verification code, an end-user code,
|
|
87
|
+
* and the end-user verification URI. The DeviceCode object is provided through a callback, and the end-user should be
|
|
88
|
+
* instructed to use another device to navigate to the verification URI to input credentials.
|
|
89
|
+
* Since the client cannot receive incoming requests, it polls the authorization server repeatedly
|
|
90
|
+
* until the end-user completes input of credentials.
|
|
91
|
+
*/
|
|
92
|
+
public async acquireTokenByDeviceCode(
|
|
93
|
+
request: DeviceCodeRequest
|
|
94
|
+
): Promise<AuthenticationResult | null> {
|
|
95
|
+
this.logger.info(
|
|
96
|
+
"acquireTokenByDeviceCode called",
|
|
97
|
+
request.correlationId
|
|
98
|
+
);
|
|
99
|
+
const validRequest: CommonDeviceCodeRequest = Object.assign(
|
|
100
|
+
request,
|
|
101
|
+
await this.initializeBaseRequest(request)
|
|
102
|
+
);
|
|
103
|
+
const serverTelemetryManager = this.initializeServerTelemetryManager(
|
|
104
|
+
ApiId.acquireTokenByDeviceCode,
|
|
105
|
+
validRequest.correlationId
|
|
106
|
+
);
|
|
107
|
+
try {
|
|
108
|
+
const deviceCodeConfig = await this.buildOauthClientConfiguration(
|
|
109
|
+
validRequest.authority,
|
|
110
|
+
validRequest.correlationId,
|
|
111
|
+
serverTelemetryManager,
|
|
112
|
+
undefined,
|
|
113
|
+
request.azureCloudOptions
|
|
114
|
+
);
|
|
115
|
+
const deviceCodeClient = new DeviceCodeClient(deviceCodeConfig);
|
|
116
|
+
this.logger.verbose(
|
|
117
|
+
"Device code client created",
|
|
118
|
+
validRequest.correlationId
|
|
119
|
+
);
|
|
120
|
+
return deviceCodeClient.acquireToken(validRequest);
|
|
121
|
+
} catch (e) {
|
|
122
|
+
if (e instanceof AuthError) {
|
|
123
|
+
e.setCorrelationId(validRequest.correlationId);
|
|
124
|
+
}
|
|
125
|
+
serverTelemetryManager.cacheFailedRequest(e as AuthError);
|
|
126
|
+
throw e;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Acquires a token interactively via the browser by requesting an authorization code then exchanging it for a token.
|
|
132
|
+
*/
|
|
133
|
+
async acquireTokenInteractive(
|
|
134
|
+
request: InteractiveRequest
|
|
135
|
+
): Promise<AuthenticationResult> {
|
|
136
|
+
const correlationId =
|
|
137
|
+
request.correlationId || this.cryptoProvider.createNewGuid();
|
|
138
|
+
this.logger.trace("acquireTokenInteractive called", correlationId);
|
|
139
|
+
const {
|
|
140
|
+
openBrowser,
|
|
141
|
+
successTemplate,
|
|
142
|
+
errorTemplate,
|
|
143
|
+
windowHandle,
|
|
144
|
+
loopbackClient: customLoopbackClient,
|
|
145
|
+
...remainingProperties
|
|
146
|
+
} = request;
|
|
147
|
+
|
|
148
|
+
if (this.nativeBrokerPlugin) {
|
|
149
|
+
const brokerRequest: NativeRequest = {
|
|
150
|
+
...remainingProperties,
|
|
151
|
+
clientId: this.config.auth.clientId,
|
|
152
|
+
scopes: request.scopes || OIDC_DEFAULT_SCOPES,
|
|
153
|
+
redirectUri: `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}`,
|
|
154
|
+
authority: request.authority || this.config.auth.authority,
|
|
155
|
+
correlationId: correlationId,
|
|
156
|
+
extraParameters: {
|
|
157
|
+
...remainingProperties.extraQueryParameters,
|
|
158
|
+
...remainingProperties.tokenQueryParameters,
|
|
159
|
+
},
|
|
160
|
+
accountId: remainingProperties.account?.nativeAccountId,
|
|
161
|
+
};
|
|
162
|
+
return this.nativeBrokerPlugin.acquireTokenInteractive(
|
|
163
|
+
brokerRequest,
|
|
164
|
+
windowHandle
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const { verifier, challenge } =
|
|
169
|
+
await this.cryptoProvider.generatePkceCodes();
|
|
170
|
+
|
|
171
|
+
const loopbackClient: ILoopbackClient =
|
|
172
|
+
customLoopbackClient || new LoopbackClient();
|
|
173
|
+
|
|
174
|
+
let authCodeResponse: ServerAuthorizationCodeResponse = {};
|
|
175
|
+
let authCodeListenerError: AuthError | null = null;
|
|
176
|
+
try {
|
|
177
|
+
const authCodeListener = loopbackClient
|
|
178
|
+
.listenForAuthCode(successTemplate, errorTemplate)
|
|
179
|
+
.then((response) => {
|
|
180
|
+
authCodeResponse = response;
|
|
181
|
+
})
|
|
182
|
+
.catch((e) => {
|
|
183
|
+
// Store the promise instead of throwing so we can control when its thrown
|
|
184
|
+
authCodeListenerError = e;
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
// Wait for server to be listening
|
|
188
|
+
const redirectUri = await this.waitForRedirectUri(loopbackClient);
|
|
189
|
+
|
|
190
|
+
const validRequest: AuthorizationUrlRequest = {
|
|
191
|
+
...remainingProperties,
|
|
192
|
+
correlationId: correlationId,
|
|
193
|
+
scopes: request.scopes || OIDC_DEFAULT_SCOPES,
|
|
194
|
+
redirectUri: redirectUri,
|
|
195
|
+
responseMode: ResponseMode.QUERY,
|
|
196
|
+
codeChallenge: challenge,
|
|
197
|
+
codeChallengeMethod: CodeChallengeMethodValues.S256,
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const authCodeUrl = await this.getAuthCodeUrl(validRequest);
|
|
201
|
+
await openBrowser(authCodeUrl);
|
|
202
|
+
await authCodeListener;
|
|
203
|
+
if (authCodeListenerError) {
|
|
204
|
+
throw authCodeListenerError;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (authCodeResponse.error) {
|
|
208
|
+
throw new ServerError(
|
|
209
|
+
authCodeResponse.error,
|
|
210
|
+
authCodeResponse.error_description,
|
|
211
|
+
authCodeResponse.suberror
|
|
212
|
+
);
|
|
213
|
+
} else if (!authCodeResponse.code) {
|
|
214
|
+
throw NodeAuthError.createNoAuthCodeInResponseError();
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
const clientInfo = authCodeResponse.client_info;
|
|
218
|
+
const tokenRequest: AuthorizationCodeRequest = {
|
|
219
|
+
code: authCodeResponse.code,
|
|
220
|
+
codeVerifier: verifier,
|
|
221
|
+
clientInfo: clientInfo || CommonConstants.EMPTY_STRING,
|
|
222
|
+
...validRequest,
|
|
223
|
+
};
|
|
224
|
+
return this.acquireTokenByCode(tokenRequest);
|
|
225
|
+
} finally {
|
|
226
|
+
loopbackClient.closeServer();
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Returns a token retrieved either from the cache or by exchanging the refresh token for a fresh access token. If brokering is enabled the token request will be serviced by the broker.
|
|
232
|
+
* @param request
|
|
233
|
+
* @returns
|
|
234
|
+
*/
|
|
235
|
+
async acquireTokenSilent(
|
|
236
|
+
request: SilentFlowRequest
|
|
237
|
+
): Promise<AuthenticationResult> {
|
|
238
|
+
const correlationId =
|
|
239
|
+
request.correlationId || this.cryptoProvider.createNewGuid();
|
|
240
|
+
this.logger.trace("acquireTokenSilent called", correlationId);
|
|
241
|
+
|
|
242
|
+
if (this.nativeBrokerPlugin) {
|
|
243
|
+
const brokerRequest: NativeRequest = {
|
|
244
|
+
...request,
|
|
245
|
+
clientId: this.config.auth.clientId,
|
|
246
|
+
scopes: request.scopes || OIDC_DEFAULT_SCOPES,
|
|
247
|
+
redirectUri: `${Constants.HTTP_PROTOCOL}${Constants.LOCALHOST}`,
|
|
248
|
+
authority: request.authority || this.config.auth.authority,
|
|
249
|
+
correlationId: correlationId,
|
|
250
|
+
extraParameters: request.tokenQueryParameters,
|
|
251
|
+
accountId: request.account.nativeAccountId,
|
|
252
|
+
forceRefresh: request.forceRefresh || false,
|
|
253
|
+
};
|
|
254
|
+
return this.nativeBrokerPlugin.acquireTokenSilent(brokerRequest);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return super.acquireTokenSilent(request);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Removes cache artifacts associated with the given account
|
|
262
|
+
* @param request
|
|
263
|
+
* @returns
|
|
264
|
+
*/
|
|
265
|
+
async signOut(request: SignOutRequest): Promise<void> {
|
|
266
|
+
if (this.nativeBrokerPlugin && request.account.nativeAccountId) {
|
|
267
|
+
const signoutRequest: NativeSignOutRequest = {
|
|
268
|
+
clientId: this.config.auth.clientId,
|
|
269
|
+
accountId: request.account.nativeAccountId,
|
|
270
|
+
correlationId:
|
|
271
|
+
request.correlationId ||
|
|
272
|
+
this.cryptoProvider.createNewGuid(),
|
|
273
|
+
};
|
|
274
|
+
await this.nativeBrokerPlugin.signOut(signoutRequest);
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
await this.getTokenCache().removeAccount(request.account);
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Returns all cached accounts for this application. If brokering is enabled this request will be serviced by the broker.
|
|
282
|
+
* @returns
|
|
283
|
+
*/
|
|
284
|
+
async getAllAccounts(): Promise<AccountInfo[]> {
|
|
285
|
+
if (this.nativeBrokerPlugin) {
|
|
286
|
+
const correlationId = this.cryptoProvider.createNewGuid();
|
|
287
|
+
return this.nativeBrokerPlugin.getAllAccounts(
|
|
288
|
+
this.config.auth.clientId,
|
|
289
|
+
correlationId
|
|
290
|
+
);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return this.getTokenCache().getAllAccounts();
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Attempts to retrieve the redirectUri from the loopback server. If the loopback server does not start listening for requests within the timeout this will throw.
|
|
298
|
+
* @param loopbackClient
|
|
299
|
+
* @returns
|
|
300
|
+
*/
|
|
301
|
+
private async waitForRedirectUri(
|
|
302
|
+
loopbackClient: ILoopbackClient
|
|
303
|
+
): Promise<string> {
|
|
304
|
+
return new Promise<string>((resolve, reject) => {
|
|
305
|
+
let ticks = 0;
|
|
306
|
+
const id = setInterval(() => {
|
|
307
|
+
if (
|
|
308
|
+
LOOPBACK_SERVER_CONSTANTS.TIMEOUT_MS /
|
|
309
|
+
LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS <
|
|
310
|
+
ticks
|
|
311
|
+
) {
|
|
312
|
+
clearInterval(id);
|
|
313
|
+
reject(NodeAuthError.createLoopbackServerTimeoutError());
|
|
314
|
+
return;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
const r = loopbackClient.getRedirectUri();
|
|
319
|
+
clearInterval(id);
|
|
320
|
+
resolve(r);
|
|
321
|
+
return;
|
|
322
|
+
} catch (e) {
|
|
323
|
+
if (
|
|
324
|
+
e instanceof AuthError &&
|
|
325
|
+
e.errorCode ===
|
|
326
|
+
NodeAuthErrorMessage.noLoopbackServerExists.code
|
|
327
|
+
) {
|
|
328
|
+
// Loopback server is not listening yet
|
|
329
|
+
ticks++;
|
|
330
|
+
return;
|
|
331
|
+
}
|
|
332
|
+
clearInterval(id);
|
|
333
|
+
reject(e);
|
|
334
|
+
return;
|
|
335
|
+
}
|
|
336
|
+
}, LOOPBACK_SERVER_CONSTANTS.INTERVAL_MS);
|
|
337
|
+
});
|
|
338
|
+
}
|
|
339
|
+
}
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
AuthenticationResult,
|
|
8
|
+
Authority,
|
|
9
|
+
BaseClient,
|
|
10
|
+
CcsCredentialType,
|
|
11
|
+
ClientConfiguration,
|
|
12
|
+
CommonUsernamePasswordRequest,
|
|
13
|
+
GrantType,
|
|
14
|
+
NetworkResponse,
|
|
15
|
+
RequestParameterBuilder,
|
|
16
|
+
RequestThumbprint,
|
|
17
|
+
ResponseHandler,
|
|
18
|
+
ServerAuthorizationTokenResponse,
|
|
19
|
+
StringUtils,
|
|
20
|
+
TimeUtils,
|
|
21
|
+
UrlString,
|
|
22
|
+
} from "@azure/msal-common";
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Oauth2.0 Password grant client
|
|
26
|
+
* Note: We are only supporting public clients for password grant and for purely testing purposes
|
|
27
|
+
*/
|
|
28
|
+
export class UsernamePasswordClient extends BaseClient {
|
|
29
|
+
constructor(configuration: ClientConfiguration) {
|
|
30
|
+
super(configuration);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* API to acquire a token by passing the username and password to the service in exchage of credentials
|
|
35
|
+
* password_grant
|
|
36
|
+
* @param request
|
|
37
|
+
*/
|
|
38
|
+
async acquireToken(
|
|
39
|
+
request: CommonUsernamePasswordRequest
|
|
40
|
+
): Promise<AuthenticationResult | null> {
|
|
41
|
+
this.logger.info("in acquireToken call in username-password client");
|
|
42
|
+
|
|
43
|
+
const reqTimestamp = TimeUtils.nowSeconds();
|
|
44
|
+
const response = await this.executeTokenRequest(
|
|
45
|
+
this.authority,
|
|
46
|
+
request
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const responseHandler = new ResponseHandler(
|
|
50
|
+
this.config.authOptions.clientId,
|
|
51
|
+
this.cacheManager,
|
|
52
|
+
this.cryptoUtils,
|
|
53
|
+
this.logger,
|
|
54
|
+
this.config.serializableCache,
|
|
55
|
+
this.config.persistencePlugin
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
// Validate response. This function throws a server error if an error is returned by the server.
|
|
59
|
+
responseHandler.validateTokenResponse(response.body);
|
|
60
|
+
const tokenResponse = responseHandler.handleServerTokenResponse(
|
|
61
|
+
response.body,
|
|
62
|
+
this.authority,
|
|
63
|
+
reqTimestamp,
|
|
64
|
+
request
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
return tokenResponse;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Executes POST request to token endpoint
|
|
72
|
+
* @param authority
|
|
73
|
+
* @param request
|
|
74
|
+
*/
|
|
75
|
+
private async executeTokenRequest(
|
|
76
|
+
authority: Authority,
|
|
77
|
+
request: CommonUsernamePasswordRequest
|
|
78
|
+
): Promise<NetworkResponse<ServerAuthorizationTokenResponse>> {
|
|
79
|
+
const queryParametersString = this.createTokenQueryParameters(request);
|
|
80
|
+
const endpoint = UrlString.appendQueryString(
|
|
81
|
+
authority.tokenEndpoint,
|
|
82
|
+
queryParametersString
|
|
83
|
+
);
|
|
84
|
+
const requestBody = this.createTokenRequestBody(request);
|
|
85
|
+
const headers: Record<string, string> = this.createTokenRequestHeaders({
|
|
86
|
+
credential: request.username,
|
|
87
|
+
type: CcsCredentialType.UPN,
|
|
88
|
+
});
|
|
89
|
+
const thumbprint: RequestThumbprint = {
|
|
90
|
+
clientId: this.config.authOptions.clientId,
|
|
91
|
+
authority: authority.canonicalAuthority,
|
|
92
|
+
scopes: request.scopes,
|
|
93
|
+
claims: request.claims,
|
|
94
|
+
authenticationScheme: request.authenticationScheme,
|
|
95
|
+
resourceRequestMethod: request.resourceRequestMethod,
|
|
96
|
+
resourceRequestUri: request.resourceRequestUri,
|
|
97
|
+
shrClaims: request.shrClaims,
|
|
98
|
+
sshKid: request.sshKid,
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
return this.executePostToTokenEndpoint(
|
|
102
|
+
endpoint,
|
|
103
|
+
requestBody,
|
|
104
|
+
headers,
|
|
105
|
+
thumbprint,
|
|
106
|
+
request.correlationId
|
|
107
|
+
);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Generates a map for all the params to be sent to the service
|
|
112
|
+
* @param request
|
|
113
|
+
*/
|
|
114
|
+
private createTokenRequestBody(
|
|
115
|
+
request: CommonUsernamePasswordRequest
|
|
116
|
+
): string {
|
|
117
|
+
const parameterBuilder = new RequestParameterBuilder();
|
|
118
|
+
|
|
119
|
+
parameterBuilder.addClientId(this.config.authOptions.clientId);
|
|
120
|
+
parameterBuilder.addUsername(request.username);
|
|
121
|
+
parameterBuilder.addPassword(request.password);
|
|
122
|
+
|
|
123
|
+
parameterBuilder.addScopes(request.scopes);
|
|
124
|
+
|
|
125
|
+
parameterBuilder.addResponseTypeForTokenAndIdToken();
|
|
126
|
+
|
|
127
|
+
parameterBuilder.addGrantType(GrantType.RESOURCE_OWNER_PASSWORD_GRANT);
|
|
128
|
+
parameterBuilder.addClientInfo();
|
|
129
|
+
|
|
130
|
+
parameterBuilder.addLibraryInfo(this.config.libraryInfo);
|
|
131
|
+
parameterBuilder.addApplicationTelemetry(
|
|
132
|
+
this.config.telemetry.application
|
|
133
|
+
);
|
|
134
|
+
parameterBuilder.addThrottling();
|
|
135
|
+
|
|
136
|
+
if (this.serverTelemetryManager) {
|
|
137
|
+
parameterBuilder.addServerTelemetry(this.serverTelemetryManager);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const correlationId =
|
|
141
|
+
request.correlationId ||
|
|
142
|
+
this.config.cryptoInterface.createNewGuid();
|
|
143
|
+
parameterBuilder.addCorrelationId(correlationId);
|
|
144
|
+
|
|
145
|
+
if (this.config.clientCredentials.clientSecret) {
|
|
146
|
+
parameterBuilder.addClientSecret(
|
|
147
|
+
this.config.clientCredentials.clientSecret
|
|
148
|
+
);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
if (this.config.clientCredentials.clientAssertion) {
|
|
152
|
+
const clientAssertion =
|
|
153
|
+
this.config.clientCredentials.clientAssertion;
|
|
154
|
+
parameterBuilder.addClientAssertion(clientAssertion.assertion);
|
|
155
|
+
parameterBuilder.addClientAssertionType(
|
|
156
|
+
clientAssertion.assertionType
|
|
157
|
+
);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
if (
|
|
161
|
+
!StringUtils.isEmptyObj(request.claims) ||
|
|
162
|
+
(this.config.authOptions.clientCapabilities &&
|
|
163
|
+
this.config.authOptions.clientCapabilities.length > 0)
|
|
164
|
+
) {
|
|
165
|
+
parameterBuilder.addClaims(
|
|
166
|
+
request.claims,
|
|
167
|
+
this.config.authOptions.clientCapabilities
|
|
168
|
+
);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
if (
|
|
172
|
+
this.config.systemOptions.preventCorsPreflight &&
|
|
173
|
+
request.username
|
|
174
|
+
) {
|
|
175
|
+
parameterBuilder.addCcsUpn(request.username);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return parameterBuilder.createQueryString();
|
|
179
|
+
}
|
|
180
|
+
}
|