@azure/identity 4.3.0-alpha.20240509.1 → 4.3.0-alpha.20240513.2
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/dist/index.js +498 -516
- package/dist/index.js.map +1 -1
- package/dist-esm/src/credentials/usernamePasswordCredential.js +5 -7
- package/dist-esm/src/credentials/usernamePasswordCredential.js.map +1 -1
- package/dist-esm/src/msal/nodeFlows/msalClient.js +15 -0
- package/dist-esm/src/msal/nodeFlows/msalClient.js.map +1 -1
- package/package.json +4 -4
- package/types/identity.d.ts +3 -1
package/dist/index.js
CHANGED
|
@@ -2165,6 +2165,20 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
|
|
|
2165
2165
|
return deviceCodeRequest;
|
|
2166
2166
|
});
|
|
2167
2167
|
}
|
|
2168
|
+
async function getTokenByUsernamePassword(scopes, username, password, options = {}) {
|
|
2169
|
+
msalLogger.getToken.info(`Attempting to acquire token using username and password`);
|
|
2170
|
+
const msalApp = await getPublicApp(options);
|
|
2171
|
+
return withSilentAuthentication(msalApp, scopes, options, () => {
|
|
2172
|
+
const requestOptions = {
|
|
2173
|
+
scopes,
|
|
2174
|
+
username,
|
|
2175
|
+
password,
|
|
2176
|
+
authority: state.msalConfig.auth.authority,
|
|
2177
|
+
claims: options === null || options === void 0 ? void 0 : options.claims,
|
|
2178
|
+
};
|
|
2179
|
+
return msalApp.acquireTokenByUsernamePassword(requestOptions);
|
|
2180
|
+
});
|
|
2181
|
+
}
|
|
2168
2182
|
function getActiveAccount() {
|
|
2169
2183
|
if (!state.cachedAccount) {
|
|
2170
2184
|
return undefined;
|
|
@@ -2177,6 +2191,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
|
|
|
2177
2191
|
getTokenByClientAssertion,
|
|
2178
2192
|
getTokenByClientCertificate,
|
|
2179
2193
|
getTokenByDeviceCode,
|
|
2194
|
+
getTokenByUsernamePassword,
|
|
2180
2195
|
};
|
|
2181
2196
|
}
|
|
2182
2197
|
|
|
@@ -3455,553 +3470,209 @@ class ClientSecretCredential {
|
|
|
3455
3470
|
|
|
3456
3471
|
// Copyright (c) Microsoft Corporation.
|
|
3457
3472
|
// Licensed under the MIT license.
|
|
3473
|
+
const logger$7 = credentialLogger("UsernamePasswordCredential");
|
|
3458
3474
|
/**
|
|
3459
|
-
*
|
|
3460
|
-
*
|
|
3461
|
-
*
|
|
3462
|
-
*
|
|
3463
|
-
* which includes handlers for successful responses and errors.
|
|
3464
|
-
*
|
|
3465
|
-
* @internal
|
|
3475
|
+
* Enables authentication to Microsoft Entra ID with a user's
|
|
3476
|
+
* username and password. This credential requires a high degree of
|
|
3477
|
+
* trust so you should only use it when other, more secure credential
|
|
3478
|
+
* types can't be used.
|
|
3466
3479
|
*/
|
|
3467
|
-
class
|
|
3468
|
-
|
|
3469
|
-
|
|
3470
|
-
|
|
3471
|
-
|
|
3472
|
-
|
|
3473
|
-
|
|
3474
|
-
|
|
3475
|
-
|
|
3476
|
-
|
|
3477
|
-
|
|
3478
|
-
|
|
3479
|
-
|
|
3480
|
-
|
|
3481
|
-
|
|
3482
|
-
this.enableMsaPassthrough = (_c = options === null || options === void 0 ? void 0 : options.brokerOptions) === null || _c === void 0 ? void 0 : _c.legacyEnableMsaPassthrough;
|
|
3483
|
-
this.parentWindowHandle = (_d = options.brokerOptions) === null || _d === void 0 ? void 0 : _d.parentWindowHandle;
|
|
3484
|
-
// If persistence has been configured
|
|
3485
|
-
if (persistenceProvider !== undefined && ((_e = options.tokenCachePersistenceOptions) === null || _e === void 0 ? void 0 : _e.enabled)) {
|
|
3486
|
-
const cacheBaseName = options.tokenCachePersistenceOptions.name || DEFAULT_TOKEN_CACHE_NAME;
|
|
3487
|
-
const nonCaeOptions = Object.assign({ name: `${cacheBaseName}.${CACHE_NON_CAE_SUFFIX}` }, options.tokenCachePersistenceOptions);
|
|
3488
|
-
const caeOptions = Object.assign({ name: `${cacheBaseName}.${CACHE_CAE_SUFFIX}` }, options.tokenCachePersistenceOptions);
|
|
3489
|
-
this.createCachePlugin = () => persistenceProvider(nonCaeOptions);
|
|
3490
|
-
this.createCachePluginCae = () => persistenceProvider(caeOptions);
|
|
3491
|
-
}
|
|
3492
|
-
else if ((_f = options.tokenCachePersistenceOptions) === null || _f === void 0 ? void 0 : _f.enabled) {
|
|
3493
|
-
throw new Error([
|
|
3494
|
-
"Persistent token caching was requested, but no persistence provider was configured.",
|
|
3495
|
-
"You must install the identity-cache-persistence plugin package (`npm install --save @azure/identity-cache-persistence`)",
|
|
3496
|
-
"and enable it by importing `useIdentityPlugin` from `@azure/identity` and calling",
|
|
3497
|
-
"`useIdentityPlugin(cachePersistencePlugin)` before using `tokenCachePersistenceOptions`.",
|
|
3498
|
-
].join(" "));
|
|
3499
|
-
}
|
|
3500
|
-
// If broker has not been configured
|
|
3501
|
-
if (!hasNativeBroker() && this.enableBroker) {
|
|
3502
|
-
throw new Error([
|
|
3503
|
-
"Broker for WAM was requested to be enabled, but no native broker was configured.",
|
|
3504
|
-
"You must install the identity-broker plugin package (`npm install --save @azure/identity-broker`)",
|
|
3505
|
-
"and enable it by importing `useIdentityPlugin` from `@azure/identity` and calling",
|
|
3506
|
-
"`useIdentityPlugin(createNativeBrokerPlugin())` before using `enableBroker`.",
|
|
3507
|
-
].join(" "));
|
|
3480
|
+
class UsernamePasswordCredential {
|
|
3481
|
+
/**
|
|
3482
|
+
* Creates an instance of the UsernamePasswordCredential with the details
|
|
3483
|
+
* needed to authenticate against Microsoft Entra ID with a username
|
|
3484
|
+
* and password.
|
|
3485
|
+
*
|
|
3486
|
+
* @param tenantId - The Microsoft Entra tenant (directory).
|
|
3487
|
+
* @param clientId - The client (application) ID of an App Registration in the tenant.
|
|
3488
|
+
* @param username - The user account's e-mail address (user name).
|
|
3489
|
+
* @param password - The user account's account password
|
|
3490
|
+
* @param options - Options for configuring the client which makes the authentication request.
|
|
3491
|
+
*/
|
|
3492
|
+
constructor(tenantId, clientId, username, password, options = {}) {
|
|
3493
|
+
if (!tenantId || !clientId || !username || !password) {
|
|
3494
|
+
throw new Error("UsernamePasswordCredential: tenantId, clientId, username and password are required parameters. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot.");
|
|
3508
3495
|
}
|
|
3509
|
-
this.
|
|
3496
|
+
this.tenantId = tenantId;
|
|
3497
|
+
this.additionallyAllowedTenantIds = resolveAdditionallyAllowedTenantIds(options === null || options === void 0 ? void 0 : options.additionallyAllowedTenants);
|
|
3498
|
+
this.username = username;
|
|
3499
|
+
this.password = password;
|
|
3500
|
+
this.msalClient = createMsalClient(clientId, this.tenantId, Object.assign(Object.assign({}, options), { tokenCredentialOptions: options !== null && options !== void 0 ? options : {} }));
|
|
3510
3501
|
}
|
|
3511
3502
|
/**
|
|
3512
|
-
*
|
|
3503
|
+
* Authenticates with Microsoft Entra ID and returns an access token if successful.
|
|
3504
|
+
* If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
|
|
3505
|
+
*
|
|
3506
|
+
* If the user provided the option `disableAutomaticAuthentication`,
|
|
3507
|
+
* once the token can't be retrieved silently,
|
|
3508
|
+
* this method won't attempt to request user interaction to retrieve the token.
|
|
3509
|
+
*
|
|
3510
|
+
* @param scopes - The list of scopes for which the token will have access.
|
|
3511
|
+
* @param options - The options used to configure any requests this
|
|
3512
|
+
* TokenCredential implementation might make.
|
|
3513
3513
|
*/
|
|
3514
|
-
|
|
3515
|
-
|
|
3516
|
-
|
|
3517
|
-
|
|
3518
|
-
|
|
3519
|
-
|
|
3520
|
-
this.identityClient = new IdentityClient(Object.assign(Object.assign({}, options.tokenCredentialOptions), { authorityHost: authority, loggingOptions: options.loggingOptions }));
|
|
3521
|
-
const clientCapabilities = [];
|
|
3522
|
-
return {
|
|
3523
|
-
auth: {
|
|
3524
|
-
clientId,
|
|
3525
|
-
authority,
|
|
3526
|
-
knownAuthorities: getKnownAuthorities(tenantId, authority, options.disableInstanceDiscovery),
|
|
3527
|
-
clientCapabilities,
|
|
3528
|
-
},
|
|
3529
|
-
// Cache is defined in this.prepare();
|
|
3530
|
-
system: {
|
|
3531
|
-
networkClient: this.identityClient,
|
|
3532
|
-
loggerOptions: {
|
|
3533
|
-
loggerCallback: defaultLoggerCallback(options.logger),
|
|
3534
|
-
logLevel: getMSALLogLevel(logger$r.getLogLevel()),
|
|
3535
|
-
piiLoggingEnabled: (_a = options.loggingOptions) === null || _a === void 0 ? void 0 : _a.enableUnsafeSupportLogging,
|
|
3536
|
-
},
|
|
3537
|
-
},
|
|
3538
|
-
};
|
|
3539
|
-
}
|
|
3540
|
-
getApp(appType, enableCae) {
|
|
3541
|
-
const app = enableCae ? this.caeApp : this.app;
|
|
3542
|
-
if (appType === "publicFirst") {
|
|
3543
|
-
return (app.public || app.confidential);
|
|
3544
|
-
}
|
|
3545
|
-
else if (appType === "confidentialFirst") {
|
|
3546
|
-
return (app.confidential || app.public);
|
|
3547
|
-
}
|
|
3548
|
-
else if (appType === "confidential") {
|
|
3549
|
-
return app.confidential;
|
|
3550
|
-
}
|
|
3551
|
-
else {
|
|
3552
|
-
return app.public;
|
|
3553
|
-
}
|
|
3514
|
+
async getToken(scopes, options = {}) {
|
|
3515
|
+
return tracingClient.withSpan(`${this.constructor.name}.getToken`, options, async (newOptions) => {
|
|
3516
|
+
newOptions.tenantId = processMultiTenantRequest(this.tenantId, newOptions, this.additionallyAllowedTenantIds, logger$7);
|
|
3517
|
+
const arrayScopes = ensureScopes(scopes);
|
|
3518
|
+
return this.msalClient.getTokenByUsernamePassword(arrayScopes, this.username, this.password, newOptions);
|
|
3519
|
+
});
|
|
3554
3520
|
}
|
|
3521
|
+
}
|
|
3522
|
+
|
|
3523
|
+
// Copyright (c) Microsoft Corporation.
|
|
3524
|
+
// Licensed under the MIT license.
|
|
3525
|
+
/**
|
|
3526
|
+
* Contains the list of all supported environment variable names so that an
|
|
3527
|
+
* appropriate error message can be generated when no credentials can be
|
|
3528
|
+
* configured.
|
|
3529
|
+
*
|
|
3530
|
+
* @internal
|
|
3531
|
+
*/
|
|
3532
|
+
const AllSupportedEnvironmentVariables = [
|
|
3533
|
+
"AZURE_TENANT_ID",
|
|
3534
|
+
"AZURE_CLIENT_ID",
|
|
3535
|
+
"AZURE_CLIENT_SECRET",
|
|
3536
|
+
"AZURE_CLIENT_CERTIFICATE_PATH",
|
|
3537
|
+
"AZURE_CLIENT_CERTIFICATE_PASSWORD",
|
|
3538
|
+
"AZURE_USERNAME",
|
|
3539
|
+
"AZURE_PASSWORD",
|
|
3540
|
+
"AZURE_ADDITIONALLY_ALLOWED_TENANTS",
|
|
3541
|
+
];
|
|
3542
|
+
function getAdditionallyAllowedTenants() {
|
|
3543
|
+
var _a;
|
|
3544
|
+
const additionallyAllowedValues = (_a = process.env.AZURE_ADDITIONALLY_ALLOWED_TENANTS) !== null && _a !== void 0 ? _a : "";
|
|
3545
|
+
return additionallyAllowedValues.split(";");
|
|
3546
|
+
}
|
|
3547
|
+
const credentialName$2 = "EnvironmentCredential";
|
|
3548
|
+
const logger$6 = credentialLogger(credentialName$2);
|
|
3549
|
+
/**
|
|
3550
|
+
* Enables authentication to Microsoft Entra ID using a client secret or certificate, or as a user
|
|
3551
|
+
* with a username and password.
|
|
3552
|
+
*/
|
|
3553
|
+
class EnvironmentCredential {
|
|
3555
3554
|
/**
|
|
3556
|
-
*
|
|
3555
|
+
* Creates an instance of the EnvironmentCredential class and decides what credential to use depending on the available environment variables.
|
|
3556
|
+
*
|
|
3557
|
+
* Required environment variables:
|
|
3558
|
+
* - `AZURE_TENANT_ID`: The Microsoft Entra tenant (directory) ID.
|
|
3559
|
+
* - `AZURE_CLIENT_ID`: The client (application) ID of an App Registration in the tenant.
|
|
3560
|
+
*
|
|
3561
|
+
* If setting the AZURE_TENANT_ID, then you can also set the additionally allowed tenants
|
|
3562
|
+
* - `AZURE_ADDITIONALLY_ALLOWED_TENANTS`: For multi-tenant applications, specifies additional tenants for which the credential may acquire tokens with a single semicolon delimited string. Use * to allow all tenants.
|
|
3563
|
+
*
|
|
3564
|
+
* Environment variables used for client credential authentication:
|
|
3565
|
+
* - `AZURE_CLIENT_SECRET`: A client secret that was generated for the App Registration.
|
|
3566
|
+
* - `AZURE_CLIENT_CERTIFICATE_PATH`: The path to a PEM certificate to use during the authentication, instead of the client secret.
|
|
3567
|
+
* - `AZURE_CLIENT_CERTIFICATE_PASSWORD`: (optional) password for the certificate file.
|
|
3568
|
+
*
|
|
3569
|
+
* Alternatively, users can provide environment variables for username and password authentication:
|
|
3570
|
+
* - `AZURE_USERNAME`: Username to authenticate with.
|
|
3571
|
+
* - `AZURE_PASSWORD`: Password to authenticate with.
|
|
3572
|
+
*
|
|
3573
|
+
* If the environment variables required to perform the authentication are missing, a {@link CredentialUnavailableError} will be thrown.
|
|
3574
|
+
* If the authentication fails, or if there's an unknown error, an {@link AuthenticationError} will be thrown.
|
|
3575
|
+
*
|
|
3576
|
+
* @param options - Options for configuring the client which makes the authentication request.
|
|
3557
3577
|
*/
|
|
3558
|
-
|
|
3559
|
-
|
|
3560
|
-
|
|
3561
|
-
|
|
3562
|
-
|
|
3563
|
-
|
|
3564
|
-
|
|
3565
|
-
}
|
|
3566
|
-
|
|
3567
|
-
|
|
3568
|
-
this.msalConfig.auth.clientCapabilities = ["cp1"];
|
|
3578
|
+
constructor(options) {
|
|
3579
|
+
// Keep track of any missing environment variables for error details
|
|
3580
|
+
this._credential = undefined;
|
|
3581
|
+
const assigned = processEnvVars(AllSupportedEnvironmentVariables).assigned.join(", ");
|
|
3582
|
+
logger$6.info(`Found the following environment variables: ${assigned}`);
|
|
3583
|
+
const tenantId = process.env.AZURE_TENANT_ID, clientId = process.env.AZURE_CLIENT_ID, clientSecret = process.env.AZURE_CLIENT_SECRET;
|
|
3584
|
+
const additionallyAllowedTenantIds = getAdditionallyAllowedTenants();
|
|
3585
|
+
const newOptions = Object.assign(Object.assign({}, options), { additionallyAllowedTenantIds });
|
|
3586
|
+
if (tenantId) {
|
|
3587
|
+
checkTenantId(logger$6, tenantId);
|
|
3569
3588
|
}
|
|
3570
|
-
if (
|
|
3589
|
+
if (tenantId && clientId && clientSecret) {
|
|
3590
|
+
logger$6.info(`Invoking ClientSecretCredential with tenant ID: ${tenantId}, clientId: ${clientId} and clientSecret: [REDACTED]`);
|
|
3591
|
+
this._credential = new ClientSecretCredential(tenantId, clientId, clientSecret, newOptions);
|
|
3571
3592
|
return;
|
|
3572
3593
|
}
|
|
3573
|
-
|
|
3574
|
-
|
|
3575
|
-
|
|
3576
|
-
};
|
|
3577
|
-
|
|
3578
|
-
|
|
3579
|
-
this.msalConfig.cache = {
|
|
3580
|
-
cachePlugin: await this.createCachePlugin(),
|
|
3581
|
-
};
|
|
3582
|
-
}
|
|
3583
|
-
if (hasNativeBroker() && this.enableBroker) {
|
|
3584
|
-
this.msalConfig.broker = {
|
|
3585
|
-
nativeBrokerPlugin: nativeBrokerInfo.broker,
|
|
3586
|
-
};
|
|
3587
|
-
if (!this.parentWindowHandle) {
|
|
3588
|
-
// error should have been thrown from within the constructor of InteractiveBrowserCredential
|
|
3589
|
-
this.logger.warning("Parent window handle is not specified for the broker. This may cause unexpected behavior. Please provide the parentWindowHandle.");
|
|
3590
|
-
}
|
|
3591
|
-
}
|
|
3592
|
-
if (options === null || options === void 0 ? void 0 : options.enableCae) {
|
|
3593
|
-
this.caeApp.public = new msalCommon__namespace.PublicClientApplication(this.msalConfig);
|
|
3594
|
-
}
|
|
3595
|
-
else {
|
|
3596
|
-
this.app.public = new msalCommon__namespace.PublicClientApplication(this.msalConfig);
|
|
3594
|
+
const certificatePath = process.env.AZURE_CLIENT_CERTIFICATE_PATH;
|
|
3595
|
+
const certificatePassword = process.env.AZURE_CLIENT_CERTIFICATE_PASSWORD;
|
|
3596
|
+
if (tenantId && clientId && certificatePath) {
|
|
3597
|
+
logger$6.info(`Invoking ClientCertificateCredential with tenant ID: ${tenantId}, clientId: ${clientId} and certificatePath: ${certificatePath}`);
|
|
3598
|
+
this._credential = new ClientCertificateCredential(tenantId, clientId, { certificatePath, certificatePassword }, newOptions);
|
|
3599
|
+
return;
|
|
3597
3600
|
}
|
|
3598
|
-
|
|
3599
|
-
|
|
3600
|
-
|
|
3601
|
-
|
|
3602
|
-
|
|
3603
|
-
this.msalConfig.auth.clientAssertion ||
|
|
3604
|
-
this.msalConfig.auth.clientCertificate) {
|
|
3605
|
-
if (options === null || options === void 0 ? void 0 : options.enableCae) {
|
|
3606
|
-
this.caeApp.confidential = new msalCommon__namespace.ConfidentialClientApplication(this.msalConfig);
|
|
3607
|
-
}
|
|
3608
|
-
else {
|
|
3609
|
-
this.app.confidential = new msalCommon__namespace.ConfidentialClientApplication(this.msalConfig);
|
|
3610
|
-
}
|
|
3611
|
-
}
|
|
3612
|
-
else {
|
|
3613
|
-
if (this.requiresConfidential) {
|
|
3614
|
-
throw new Error("Unable to generate the MSAL confidential client. Missing either the client's secret, certificate or assertion.");
|
|
3615
|
-
}
|
|
3616
|
-
}
|
|
3617
|
-
}
|
|
3618
|
-
/**
|
|
3619
|
-
* Allows the cancellation of a MSAL request.
|
|
3620
|
-
*/
|
|
3621
|
-
withCancellation(promise, abortSignal, onCancel) {
|
|
3622
|
-
return new Promise((resolve, reject) => {
|
|
3623
|
-
promise
|
|
3624
|
-
.then((msalToken) => {
|
|
3625
|
-
return resolve(msalToken);
|
|
3626
|
-
})
|
|
3627
|
-
.catch(reject);
|
|
3628
|
-
if (abortSignal) {
|
|
3629
|
-
abortSignal.addEventListener("abort", () => {
|
|
3630
|
-
onCancel === null || onCancel === void 0 ? void 0 : onCancel();
|
|
3631
|
-
});
|
|
3632
|
-
}
|
|
3633
|
-
});
|
|
3634
|
-
}
|
|
3635
|
-
/**
|
|
3636
|
-
* Returns the existing account, attempts to load the account from MSAL.
|
|
3637
|
-
*/
|
|
3638
|
-
async getActiveAccount(enableCae = false) {
|
|
3639
|
-
if (this.account) {
|
|
3640
|
-
return this.account;
|
|
3641
|
-
}
|
|
3642
|
-
const cache = this.getApp("confidentialFirst", enableCae).getTokenCache();
|
|
3643
|
-
const accountsByTenant = await (cache === null || cache === void 0 ? void 0 : cache.getAllAccounts());
|
|
3644
|
-
if (!accountsByTenant) {
|
|
3645
|
-
return;
|
|
3646
|
-
}
|
|
3647
|
-
if (accountsByTenant.length === 1) {
|
|
3648
|
-
this.account = msalToPublic(this.clientId, accountsByTenant[0]);
|
|
3649
|
-
}
|
|
3650
|
-
else {
|
|
3651
|
-
this.logger
|
|
3652
|
-
.info(`More than one account was found authenticated for this Client ID and Tenant ID.
|
|
3653
|
-
However, no "authenticationRecord" has been provided for this credential,
|
|
3654
|
-
therefore we're unable to pick between these accounts.
|
|
3655
|
-
A new login attempt will be requested, to ensure the correct account is picked.
|
|
3656
|
-
To work with multiple accounts for the same Client ID and Tenant ID, please provide an "authenticationRecord" when initializing a credential to prevent this from happening.`);
|
|
3657
|
-
return;
|
|
3658
|
-
}
|
|
3659
|
-
return this.account;
|
|
3660
|
-
}
|
|
3661
|
-
/**
|
|
3662
|
-
* Attempts to retrieve a token from cache.
|
|
3663
|
-
*/
|
|
3664
|
-
async getTokenSilent(scopes, options) {
|
|
3665
|
-
var _a, _b, _c;
|
|
3666
|
-
await this.getActiveAccount(options === null || options === void 0 ? void 0 : options.enableCae);
|
|
3667
|
-
if (!this.account) {
|
|
3668
|
-
throw new AuthenticationRequiredError({
|
|
3669
|
-
scopes,
|
|
3670
|
-
getTokenOptions: options,
|
|
3671
|
-
message: "Silent authentication failed. We couldn't retrieve an active account from the cache.",
|
|
3672
|
-
});
|
|
3673
|
-
}
|
|
3674
|
-
const silentRequest = {
|
|
3675
|
-
// To be able to re-use the account, the Token Cache must also have been provided.
|
|
3676
|
-
account: publicToMsal(this.account),
|
|
3677
|
-
correlationId: options === null || options === void 0 ? void 0 : options.correlationId,
|
|
3678
|
-
scopes,
|
|
3679
|
-
authority: options === null || options === void 0 ? void 0 : options.authority,
|
|
3680
|
-
claims: options === null || options === void 0 ? void 0 : options.claims,
|
|
3681
|
-
};
|
|
3682
|
-
if (hasNativeBroker() && this.enableBroker) {
|
|
3683
|
-
if (!silentRequest.tokenQueryParameters) {
|
|
3684
|
-
silentRequest.tokenQueryParameters = {};
|
|
3685
|
-
}
|
|
3686
|
-
if (!this.parentWindowHandle) {
|
|
3687
|
-
// error should have been thrown from within the constructor of InteractiveBrowserCredential
|
|
3688
|
-
this.logger.warning("Parent window handle is not specified for the broker. This may cause unexpected behavior. Please provide the parentWindowHandle.");
|
|
3689
|
-
}
|
|
3690
|
-
if (this.enableMsaPassthrough) {
|
|
3691
|
-
silentRequest.tokenQueryParameters["msal_request_type"] = "consumer_passthrough";
|
|
3692
|
-
}
|
|
3693
|
-
}
|
|
3694
|
-
try {
|
|
3695
|
-
this.logger.info("Attempting to acquire token silently");
|
|
3696
|
-
/**
|
|
3697
|
-
* The following code to retrieve all accounts is done as a workaround in an attempt to force the
|
|
3698
|
-
* refresh of the token cache with the token and the account passed in through the
|
|
3699
|
-
* `authenticationRecord` parameter. See issue - https://github.com/Azure/azure-sdk-for-js/issues/24349#issuecomment-1496715651
|
|
3700
|
-
* This workaround serves as a workaround for silent authentication not happening when authenticationRecord is passed.
|
|
3701
|
-
*/
|
|
3702
|
-
await ((_a = this.getApp("publicFirst", options === null || options === void 0 ? void 0 : options.enableCae)) === null || _a === void 0 ? void 0 : _a.getTokenCache().getAllAccounts());
|
|
3703
|
-
const response = (_c = (await ((_b = this.getApp("confidential", options === null || options === void 0 ? void 0 : options.enableCae)) === null || _b === void 0 ? void 0 : _b.acquireTokenSilent(silentRequest)))) !== null && _c !== void 0 ? _c : (await this.getApp("public", options === null || options === void 0 ? void 0 : options.enableCae).acquireTokenSilent(silentRequest));
|
|
3704
|
-
return this.handleResult(scopes, response || undefined);
|
|
3705
|
-
}
|
|
3706
|
-
catch (err) {
|
|
3707
|
-
throw handleMsalError(scopes, err, options);
|
|
3601
|
+
const username = process.env.AZURE_USERNAME;
|
|
3602
|
+
const password = process.env.AZURE_PASSWORD;
|
|
3603
|
+
if (tenantId && clientId && username && password) {
|
|
3604
|
+
logger$6.info(`Invoking UsernamePasswordCredential with tenant ID: ${tenantId}, clientId: ${clientId} and username: ${username}`);
|
|
3605
|
+
this._credential = new UsernamePasswordCredential(tenantId, clientId, username, password, newOptions);
|
|
3708
3606
|
}
|
|
3709
3607
|
}
|
|
3710
3608
|
/**
|
|
3711
|
-
*
|
|
3712
|
-
*
|
|
3609
|
+
* Authenticates with Microsoft Entra ID and returns an access token if successful.
|
|
3610
|
+
*
|
|
3611
|
+
* @param scopes - The list of scopes for which the token will have access.
|
|
3612
|
+
* @param options - Optional parameters. See {@link GetTokenOptions}.
|
|
3713
3613
|
*/
|
|
3714
3614
|
async getToken(scopes, options = {}) {
|
|
3715
|
-
|
|
3716
|
-
this.
|
|
3717
|
-
|
|
3718
|
-
|
|
3719
|
-
|
|
3720
|
-
|
|
3721
|
-
|
|
3722
|
-
|
|
3723
|
-
|
|
3724
|
-
|
|
3725
|
-
|
|
3726
|
-
|
|
3727
|
-
|
|
3728
|
-
|
|
3729
|
-
|
|
3730
|
-
}
|
|
3731
|
-
// We don't return the promise since we want to catch errors right here.
|
|
3732
|
-
return await this.getTokenSilent(scopes, options);
|
|
3733
|
-
}
|
|
3734
|
-
catch (err) {
|
|
3735
|
-
if (err.name !== "AuthenticationRequiredError") {
|
|
3736
|
-
throw err;
|
|
3737
|
-
}
|
|
3738
|
-
if (options === null || options === void 0 ? void 0 : options.disableAutomaticAuthentication) {
|
|
3739
|
-
throw new AuthenticationRequiredError({
|
|
3740
|
-
scopes,
|
|
3741
|
-
getTokenOptions: options,
|
|
3742
|
-
message: "Automatic authentication has been disabled. You may call the authentication() method.",
|
|
3743
|
-
});
|
|
3615
|
+
return tracingClient.withSpan(`${credentialName$2}.getToken`, options, async (newOptions) => {
|
|
3616
|
+
if (this._credential) {
|
|
3617
|
+
try {
|
|
3618
|
+
const result = await this._credential.getToken(scopes, newOptions);
|
|
3619
|
+
logger$6.getToken.info(formatSuccess(scopes));
|
|
3620
|
+
return result;
|
|
3621
|
+
}
|
|
3622
|
+
catch (err) {
|
|
3623
|
+
const authenticationError = new AuthenticationError(400, {
|
|
3624
|
+
error: `${credentialName$2} authentication failed. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.`,
|
|
3625
|
+
error_description: err.message.toString().split("More details:").join(""),
|
|
3626
|
+
});
|
|
3627
|
+
logger$6.getToken.info(formatError(scopes, authenticationError));
|
|
3628
|
+
throw authenticationError;
|
|
3629
|
+
}
|
|
3744
3630
|
}
|
|
3745
|
-
|
|
3746
|
-
|
|
3747
|
-
}
|
|
3748
|
-
}
|
|
3749
|
-
/**
|
|
3750
|
-
* Handles the MSAL authentication result.
|
|
3751
|
-
* If the result has an account, we update the local account reference.
|
|
3752
|
-
* If the token received is invalid, an error will be thrown depending on what's missing.
|
|
3753
|
-
*/
|
|
3754
|
-
handleResult(scopes, result, getTokenOptions) {
|
|
3755
|
-
if (result === null || result === void 0 ? void 0 : result.account) {
|
|
3756
|
-
this.account = msalToPublic(this.clientId, result.account);
|
|
3757
|
-
}
|
|
3758
|
-
ensureValidMsalToken(scopes, result, getTokenOptions);
|
|
3759
|
-
this.logger.getToken.info(formatSuccess(scopes));
|
|
3760
|
-
return {
|
|
3761
|
-
token: result.accessToken,
|
|
3762
|
-
expiresOnTimestamp: result.expiresOn.getTime(),
|
|
3763
|
-
};
|
|
3631
|
+
throw new CredentialUnavailableError(`${credentialName$2} is unavailable. No underlying credential could be used. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.`);
|
|
3632
|
+
});
|
|
3764
3633
|
}
|
|
3765
3634
|
}
|
|
3766
3635
|
|
|
3767
3636
|
// Copyright (c) Microsoft Corporation.
|
|
3768
3637
|
// Licensed under the MIT license.
|
|
3638
|
+
const logger$5 = credentialLogger("DefaultAzureCredential");
|
|
3769
3639
|
/**
|
|
3770
|
-
*
|
|
3640
|
+
* Creates a {@link ManagedIdentityCredential} from the provided options.
|
|
3641
|
+
* @param options - Options to configure the credential.
|
|
3642
|
+
*
|
|
3771
3643
|
* @internal
|
|
3772
3644
|
*/
|
|
3773
|
-
|
|
3774
|
-
|
|
3775
|
-
|
|
3776
|
-
|
|
3777
|
-
|
|
3645
|
+
function createDefaultManagedIdentityCredential(options = {}) {
|
|
3646
|
+
var _a, _b, _c, _d;
|
|
3647
|
+
(_a = options.retryOptions) !== null && _a !== void 0 ? _a : (options.retryOptions = {
|
|
3648
|
+
maxRetries: 5,
|
|
3649
|
+
retryDelayInMs: 800,
|
|
3650
|
+
});
|
|
3651
|
+
const managedIdentityClientId = (_b = options === null || options === void 0 ? void 0 : options.managedIdentityClientId) !== null && _b !== void 0 ? _b : process.env.AZURE_CLIENT_ID;
|
|
3652
|
+
const workloadIdentityClientId = (_c = options === null || options === void 0 ? void 0 : options.workloadIdentityClientId) !== null && _c !== void 0 ? _c : managedIdentityClientId;
|
|
3653
|
+
const managedResourceId = options === null || options === void 0 ? void 0 : options.managedIdentityResourceId;
|
|
3654
|
+
const workloadFile = process.env.AZURE_FEDERATED_TOKEN_FILE;
|
|
3655
|
+
const tenantId = (_d = options === null || options === void 0 ? void 0 : options.tenantId) !== null && _d !== void 0 ? _d : process.env.AZURE_TENANT_ID;
|
|
3656
|
+
if (managedResourceId) {
|
|
3657
|
+
const managedIdentityResourceIdOptions = Object.assign(Object.assign({}, options), { resourceId: managedResourceId });
|
|
3658
|
+
return new ManagedIdentityCredential(managedIdentityResourceIdOptions);
|
|
3778
3659
|
}
|
|
3779
|
-
|
|
3780
|
-
|
|
3781
|
-
|
|
3782
|
-
|
|
3783
|
-
|
|
3784
|
-
|
|
3785
|
-
|
|
3786
|
-
authority: options === null || options === void 0 ? void 0 : options.authority,
|
|
3787
|
-
claims: options === null || options === void 0 ? void 0 : options.claims,
|
|
3788
|
-
};
|
|
3789
|
-
const result = await this.getApp("public", options === null || options === void 0 ? void 0 : options.enableCae).acquireTokenByUsernamePassword(requestOptions);
|
|
3790
|
-
return this.handleResult(scopes, result || undefined);
|
|
3791
|
-
}
|
|
3792
|
-
catch (error) {
|
|
3793
|
-
throw handleMsalError(scopes, error, options);
|
|
3794
|
-
}
|
|
3660
|
+
if (workloadFile && workloadIdentityClientId) {
|
|
3661
|
+
const workloadIdentityCredentialOptions = Object.assign(Object.assign({}, options), { tenantId: tenantId });
|
|
3662
|
+
return new ManagedIdentityCredential(workloadIdentityClientId, workloadIdentityCredentialOptions);
|
|
3663
|
+
}
|
|
3664
|
+
if (managedIdentityClientId) {
|
|
3665
|
+
const managedIdentityClientOptions = Object.assign(Object.assign({}, options), { clientId: managedIdentityClientId });
|
|
3666
|
+
return new ManagedIdentityCredential(managedIdentityClientOptions);
|
|
3795
3667
|
}
|
|
3668
|
+
// We may be able to return a UnavailableCredential here, but that may be a breaking change
|
|
3669
|
+
return new ManagedIdentityCredential(options);
|
|
3796
3670
|
}
|
|
3797
|
-
|
|
3798
|
-
// Copyright (c) Microsoft Corporation.
|
|
3799
|
-
// Licensed under the MIT license.
|
|
3800
|
-
const logger$7 = credentialLogger("UsernamePasswordCredential");
|
|
3801
3671
|
/**
|
|
3802
|
-
*
|
|
3803
|
-
*
|
|
3804
|
-
*
|
|
3805
|
-
*
|
|
3806
|
-
*/
|
|
3807
|
-
class UsernamePasswordCredential {
|
|
3808
|
-
/**
|
|
3809
|
-
* Creates an instance of the UsernamePasswordCredential with the details
|
|
3810
|
-
* needed to authenticate against Microsoft Entra ID with a username
|
|
3811
|
-
* and password.
|
|
3812
|
-
*
|
|
3813
|
-
* @param tenantId - The Microsoft Entra tenant (directory).
|
|
3814
|
-
* @param clientId - The client (application) ID of an App Registration in the tenant.
|
|
3815
|
-
* @param username - The user account's e-mail address (user name).
|
|
3816
|
-
* @param password - The user account's account password
|
|
3817
|
-
* @param options - Options for configuring the client which makes the authentication request.
|
|
3818
|
-
*/
|
|
3819
|
-
constructor(tenantId, clientId, username, password, options = {}) {
|
|
3820
|
-
if (!tenantId || !clientId || !username || !password) {
|
|
3821
|
-
throw new Error("UsernamePasswordCredential: tenantId, clientId, username and password are required parameters. To troubleshoot, visit https://aka.ms/azsdk/js/identity/usernamepasswordcredential/troubleshoot.");
|
|
3822
|
-
}
|
|
3823
|
-
this.tenantId = tenantId;
|
|
3824
|
-
this.additionallyAllowedTenantIds = resolveAdditionallyAllowedTenantIds(options === null || options === void 0 ? void 0 : options.additionallyAllowedTenants);
|
|
3825
|
-
this.msalFlow = new MsalUsernamePassword(Object.assign(Object.assign({}, options), { logger: logger$7,
|
|
3826
|
-
clientId,
|
|
3827
|
-
tenantId,
|
|
3828
|
-
username,
|
|
3829
|
-
password, tokenCredentialOptions: options || {} }));
|
|
3830
|
-
}
|
|
3831
|
-
/**
|
|
3832
|
-
* Authenticates with Microsoft Entra ID and returns an access token if successful.
|
|
3833
|
-
* If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
|
|
3834
|
-
*
|
|
3835
|
-
* If the user provided the option `disableAutomaticAuthentication`,
|
|
3836
|
-
* once the token can't be retrieved silently,
|
|
3837
|
-
* this method won't attempt to request user interaction to retrieve the token.
|
|
3838
|
-
*
|
|
3839
|
-
* @param scopes - The list of scopes for which the token will have access.
|
|
3840
|
-
* @param options - The options used to configure any requests this
|
|
3841
|
-
* TokenCredential implementation might make.
|
|
3842
|
-
*/
|
|
3843
|
-
async getToken(scopes, options = {}) {
|
|
3844
|
-
return tracingClient.withSpan(`${this.constructor.name}.getToken`, options, async (newOptions) => {
|
|
3845
|
-
newOptions.tenantId = processMultiTenantRequest(this.tenantId, newOptions, this.additionallyAllowedTenantIds, logger$7);
|
|
3846
|
-
const arrayScopes = ensureScopes(scopes);
|
|
3847
|
-
return this.msalFlow.getToken(arrayScopes, newOptions);
|
|
3848
|
-
});
|
|
3849
|
-
}
|
|
3850
|
-
}
|
|
3851
|
-
|
|
3852
|
-
// Copyright (c) Microsoft Corporation.
|
|
3853
|
-
// Licensed under the MIT license.
|
|
3854
|
-
/**
|
|
3855
|
-
* Contains the list of all supported environment variable names so that an
|
|
3856
|
-
* appropriate error message can be generated when no credentials can be
|
|
3857
|
-
* configured.
|
|
3858
|
-
*
|
|
3859
|
-
* @internal
|
|
3860
|
-
*/
|
|
3861
|
-
const AllSupportedEnvironmentVariables = [
|
|
3862
|
-
"AZURE_TENANT_ID",
|
|
3863
|
-
"AZURE_CLIENT_ID",
|
|
3864
|
-
"AZURE_CLIENT_SECRET",
|
|
3865
|
-
"AZURE_CLIENT_CERTIFICATE_PATH",
|
|
3866
|
-
"AZURE_CLIENT_CERTIFICATE_PASSWORD",
|
|
3867
|
-
"AZURE_USERNAME",
|
|
3868
|
-
"AZURE_PASSWORD",
|
|
3869
|
-
"AZURE_ADDITIONALLY_ALLOWED_TENANTS",
|
|
3870
|
-
];
|
|
3871
|
-
function getAdditionallyAllowedTenants() {
|
|
3872
|
-
var _a;
|
|
3873
|
-
const additionallyAllowedValues = (_a = process.env.AZURE_ADDITIONALLY_ALLOWED_TENANTS) !== null && _a !== void 0 ? _a : "";
|
|
3874
|
-
return additionallyAllowedValues.split(";");
|
|
3875
|
-
}
|
|
3876
|
-
const credentialName$2 = "EnvironmentCredential";
|
|
3877
|
-
const logger$6 = credentialLogger(credentialName$2);
|
|
3878
|
-
/**
|
|
3879
|
-
* Enables authentication to Microsoft Entra ID using a client secret or certificate, or as a user
|
|
3880
|
-
* with a username and password.
|
|
3881
|
-
*/
|
|
3882
|
-
class EnvironmentCredential {
|
|
3883
|
-
/**
|
|
3884
|
-
* Creates an instance of the EnvironmentCredential class and decides what credential to use depending on the available environment variables.
|
|
3885
|
-
*
|
|
3886
|
-
* Required environment variables:
|
|
3887
|
-
* - `AZURE_TENANT_ID`: The Microsoft Entra tenant (directory) ID.
|
|
3888
|
-
* - `AZURE_CLIENT_ID`: The client (application) ID of an App Registration in the tenant.
|
|
3889
|
-
*
|
|
3890
|
-
* If setting the AZURE_TENANT_ID, then you can also set the additionally allowed tenants
|
|
3891
|
-
* - `AZURE_ADDITIONALLY_ALLOWED_TENANTS`: For multi-tenant applications, specifies additional tenants for which the credential may acquire tokens with a single semicolon delimited string. Use * to allow all tenants.
|
|
3892
|
-
*
|
|
3893
|
-
* Environment variables used for client credential authentication:
|
|
3894
|
-
* - `AZURE_CLIENT_SECRET`: A client secret that was generated for the App Registration.
|
|
3895
|
-
* - `AZURE_CLIENT_CERTIFICATE_PATH`: The path to a PEM certificate to use during the authentication, instead of the client secret.
|
|
3896
|
-
* - `AZURE_CLIENT_CERTIFICATE_PASSWORD`: (optional) password for the certificate file.
|
|
3897
|
-
*
|
|
3898
|
-
* Alternatively, users can provide environment variables for username and password authentication:
|
|
3899
|
-
* - `AZURE_USERNAME`: Username to authenticate with.
|
|
3900
|
-
* - `AZURE_PASSWORD`: Password to authenticate with.
|
|
3901
|
-
*
|
|
3902
|
-
* If the environment variables required to perform the authentication are missing, a {@link CredentialUnavailableError} will be thrown.
|
|
3903
|
-
* If the authentication fails, or if there's an unknown error, an {@link AuthenticationError} will be thrown.
|
|
3904
|
-
*
|
|
3905
|
-
* @param options - Options for configuring the client which makes the authentication request.
|
|
3906
|
-
*/
|
|
3907
|
-
constructor(options) {
|
|
3908
|
-
// Keep track of any missing environment variables for error details
|
|
3909
|
-
this._credential = undefined;
|
|
3910
|
-
const assigned = processEnvVars(AllSupportedEnvironmentVariables).assigned.join(", ");
|
|
3911
|
-
logger$6.info(`Found the following environment variables: ${assigned}`);
|
|
3912
|
-
const tenantId = process.env.AZURE_TENANT_ID, clientId = process.env.AZURE_CLIENT_ID, clientSecret = process.env.AZURE_CLIENT_SECRET;
|
|
3913
|
-
const additionallyAllowedTenantIds = getAdditionallyAllowedTenants();
|
|
3914
|
-
const newOptions = Object.assign(Object.assign({}, options), { additionallyAllowedTenantIds });
|
|
3915
|
-
if (tenantId) {
|
|
3916
|
-
checkTenantId(logger$6, tenantId);
|
|
3917
|
-
}
|
|
3918
|
-
if (tenantId && clientId && clientSecret) {
|
|
3919
|
-
logger$6.info(`Invoking ClientSecretCredential with tenant ID: ${tenantId}, clientId: ${clientId} and clientSecret: [REDACTED]`);
|
|
3920
|
-
this._credential = new ClientSecretCredential(tenantId, clientId, clientSecret, newOptions);
|
|
3921
|
-
return;
|
|
3922
|
-
}
|
|
3923
|
-
const certificatePath = process.env.AZURE_CLIENT_CERTIFICATE_PATH;
|
|
3924
|
-
const certificatePassword = process.env.AZURE_CLIENT_CERTIFICATE_PASSWORD;
|
|
3925
|
-
if (tenantId && clientId && certificatePath) {
|
|
3926
|
-
logger$6.info(`Invoking ClientCertificateCredential with tenant ID: ${tenantId}, clientId: ${clientId} and certificatePath: ${certificatePath}`);
|
|
3927
|
-
this._credential = new ClientCertificateCredential(tenantId, clientId, { certificatePath, certificatePassword }, newOptions);
|
|
3928
|
-
return;
|
|
3929
|
-
}
|
|
3930
|
-
const username = process.env.AZURE_USERNAME;
|
|
3931
|
-
const password = process.env.AZURE_PASSWORD;
|
|
3932
|
-
if (tenantId && clientId && username && password) {
|
|
3933
|
-
logger$6.info(`Invoking UsernamePasswordCredential with tenant ID: ${tenantId}, clientId: ${clientId} and username: ${username}`);
|
|
3934
|
-
this._credential = new UsernamePasswordCredential(tenantId, clientId, username, password, newOptions);
|
|
3935
|
-
}
|
|
3936
|
-
}
|
|
3937
|
-
/**
|
|
3938
|
-
* Authenticates with Microsoft Entra ID and returns an access token if successful.
|
|
3939
|
-
*
|
|
3940
|
-
* @param scopes - The list of scopes for which the token will have access.
|
|
3941
|
-
* @param options - Optional parameters. See {@link GetTokenOptions}.
|
|
3942
|
-
*/
|
|
3943
|
-
async getToken(scopes, options = {}) {
|
|
3944
|
-
return tracingClient.withSpan(`${credentialName$2}.getToken`, options, async (newOptions) => {
|
|
3945
|
-
if (this._credential) {
|
|
3946
|
-
try {
|
|
3947
|
-
const result = await this._credential.getToken(scopes, newOptions);
|
|
3948
|
-
logger$6.getToken.info(formatSuccess(scopes));
|
|
3949
|
-
return result;
|
|
3950
|
-
}
|
|
3951
|
-
catch (err) {
|
|
3952
|
-
const authenticationError = new AuthenticationError(400, {
|
|
3953
|
-
error: `${credentialName$2} authentication failed. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.`,
|
|
3954
|
-
error_description: err.message.toString().split("More details:").join(""),
|
|
3955
|
-
});
|
|
3956
|
-
logger$6.getToken.info(formatError(scopes, authenticationError));
|
|
3957
|
-
throw authenticationError;
|
|
3958
|
-
}
|
|
3959
|
-
}
|
|
3960
|
-
throw new CredentialUnavailableError(`${credentialName$2} is unavailable. No underlying credential could be used. To troubleshoot, visit https://aka.ms/azsdk/js/identity/environmentcredential/troubleshoot.`);
|
|
3961
|
-
});
|
|
3962
|
-
}
|
|
3963
|
-
}
|
|
3964
|
-
|
|
3965
|
-
// Copyright (c) Microsoft Corporation.
|
|
3966
|
-
// Licensed under the MIT license.
|
|
3967
|
-
const logger$5 = credentialLogger("DefaultAzureCredential");
|
|
3968
|
-
/**
|
|
3969
|
-
* Creates a {@link ManagedIdentityCredential} from the provided options.
|
|
3970
|
-
* @param options - Options to configure the credential.
|
|
3971
|
-
*
|
|
3972
|
-
* @internal
|
|
3973
|
-
*/
|
|
3974
|
-
function createDefaultManagedIdentityCredential(options = {}) {
|
|
3975
|
-
var _a, _b, _c, _d;
|
|
3976
|
-
(_a = options.retryOptions) !== null && _a !== void 0 ? _a : (options.retryOptions = {
|
|
3977
|
-
maxRetries: 5,
|
|
3978
|
-
retryDelayInMs: 800,
|
|
3979
|
-
});
|
|
3980
|
-
const managedIdentityClientId = (_b = options === null || options === void 0 ? void 0 : options.managedIdentityClientId) !== null && _b !== void 0 ? _b : process.env.AZURE_CLIENT_ID;
|
|
3981
|
-
const workloadIdentityClientId = (_c = options === null || options === void 0 ? void 0 : options.workloadIdentityClientId) !== null && _c !== void 0 ? _c : managedIdentityClientId;
|
|
3982
|
-
const managedResourceId = options === null || options === void 0 ? void 0 : options.managedIdentityResourceId;
|
|
3983
|
-
const workloadFile = process.env.AZURE_FEDERATED_TOKEN_FILE;
|
|
3984
|
-
const tenantId = (_d = options === null || options === void 0 ? void 0 : options.tenantId) !== null && _d !== void 0 ? _d : process.env.AZURE_TENANT_ID;
|
|
3985
|
-
if (managedResourceId) {
|
|
3986
|
-
const managedIdentityResourceIdOptions = Object.assign(Object.assign({}, options), { resourceId: managedResourceId });
|
|
3987
|
-
return new ManagedIdentityCredential(managedIdentityResourceIdOptions);
|
|
3988
|
-
}
|
|
3989
|
-
if (workloadFile && workloadIdentityClientId) {
|
|
3990
|
-
const workloadIdentityCredentialOptions = Object.assign(Object.assign({}, options), { tenantId: tenantId });
|
|
3991
|
-
return new ManagedIdentityCredential(workloadIdentityClientId, workloadIdentityCredentialOptions);
|
|
3992
|
-
}
|
|
3993
|
-
if (managedIdentityClientId) {
|
|
3994
|
-
const managedIdentityClientOptions = Object.assign(Object.assign({}, options), { clientId: managedIdentityClientId });
|
|
3995
|
-
return new ManagedIdentityCredential(managedIdentityClientOptions);
|
|
3996
|
-
}
|
|
3997
|
-
// We may be able to return a UnavailableCredential here, but that may be a breaking change
|
|
3998
|
-
return new ManagedIdentityCredential(options);
|
|
3999
|
-
}
|
|
4000
|
-
/**
|
|
4001
|
-
* Creates a {@link WorkloadIdentityCredential} from the provided options.
|
|
4002
|
-
* @param options - Options to configure the credential.
|
|
4003
|
-
*
|
|
4004
|
-
* @internal
|
|
3672
|
+
* Creates a {@link WorkloadIdentityCredential} from the provided options.
|
|
3673
|
+
* @param options - Options to configure the credential.
|
|
3674
|
+
*
|
|
3675
|
+
* @internal
|
|
4005
3676
|
*/
|
|
4006
3677
|
function createDefaultWorkloadIdentityCredential(options) {
|
|
4007
3678
|
var _a, _b, _c;
|
|
@@ -4105,6 +3776,317 @@ class DefaultAzureCredential extends ChainedTokenCredential {
|
|
|
4105
3776
|
}
|
|
4106
3777
|
}
|
|
4107
3778
|
|
|
3779
|
+
// Copyright (c) Microsoft Corporation.
|
|
3780
|
+
// Licensed under the MIT license.
|
|
3781
|
+
/**
|
|
3782
|
+
* MSAL partial base client for Node.js.
|
|
3783
|
+
*
|
|
3784
|
+
* It completes the input configuration with some default values.
|
|
3785
|
+
* It also provides with utility protected methods that can be used from any of the clients,
|
|
3786
|
+
* which includes handlers for successful responses and errors.
|
|
3787
|
+
*
|
|
3788
|
+
* @internal
|
|
3789
|
+
*/
|
|
3790
|
+
class MsalNode {
|
|
3791
|
+
constructor(options) {
|
|
3792
|
+
var _a, _b, _c, _d, _e, _f;
|
|
3793
|
+
this.app = {};
|
|
3794
|
+
this.caeApp = {};
|
|
3795
|
+
this.requiresConfidential = false;
|
|
3796
|
+
this.logger = options.logger;
|
|
3797
|
+
this.msalConfig = this.defaultNodeMsalConfig(options);
|
|
3798
|
+
this.tenantId = resolveTenantId(options.logger, options.tenantId, options.clientId);
|
|
3799
|
+
this.additionallyAllowedTenantIds = resolveAdditionallyAllowedTenantIds((_a = options === null || options === void 0 ? void 0 : options.tokenCredentialOptions) === null || _a === void 0 ? void 0 : _a.additionallyAllowedTenants);
|
|
3800
|
+
this.clientId = this.msalConfig.auth.clientId;
|
|
3801
|
+
if (options === null || options === void 0 ? void 0 : options.getAssertion) {
|
|
3802
|
+
this.getAssertion = options.getAssertion;
|
|
3803
|
+
}
|
|
3804
|
+
this.enableBroker = (_b = options === null || options === void 0 ? void 0 : options.brokerOptions) === null || _b === void 0 ? void 0 : _b.enabled;
|
|
3805
|
+
this.enableMsaPassthrough = (_c = options === null || options === void 0 ? void 0 : options.brokerOptions) === null || _c === void 0 ? void 0 : _c.legacyEnableMsaPassthrough;
|
|
3806
|
+
this.parentWindowHandle = (_d = options.brokerOptions) === null || _d === void 0 ? void 0 : _d.parentWindowHandle;
|
|
3807
|
+
// If persistence has been configured
|
|
3808
|
+
if (persistenceProvider !== undefined && ((_e = options.tokenCachePersistenceOptions) === null || _e === void 0 ? void 0 : _e.enabled)) {
|
|
3809
|
+
const cacheBaseName = options.tokenCachePersistenceOptions.name || DEFAULT_TOKEN_CACHE_NAME;
|
|
3810
|
+
const nonCaeOptions = Object.assign({ name: `${cacheBaseName}.${CACHE_NON_CAE_SUFFIX}` }, options.tokenCachePersistenceOptions);
|
|
3811
|
+
const caeOptions = Object.assign({ name: `${cacheBaseName}.${CACHE_CAE_SUFFIX}` }, options.tokenCachePersistenceOptions);
|
|
3812
|
+
this.createCachePlugin = () => persistenceProvider(nonCaeOptions);
|
|
3813
|
+
this.createCachePluginCae = () => persistenceProvider(caeOptions);
|
|
3814
|
+
}
|
|
3815
|
+
else if ((_f = options.tokenCachePersistenceOptions) === null || _f === void 0 ? void 0 : _f.enabled) {
|
|
3816
|
+
throw new Error([
|
|
3817
|
+
"Persistent token caching was requested, but no persistence provider was configured.",
|
|
3818
|
+
"You must install the identity-cache-persistence plugin package (`npm install --save @azure/identity-cache-persistence`)",
|
|
3819
|
+
"and enable it by importing `useIdentityPlugin` from `@azure/identity` and calling",
|
|
3820
|
+
"`useIdentityPlugin(cachePersistencePlugin)` before using `tokenCachePersistenceOptions`.",
|
|
3821
|
+
].join(" "));
|
|
3822
|
+
}
|
|
3823
|
+
// If broker has not been configured
|
|
3824
|
+
if (!hasNativeBroker() && this.enableBroker) {
|
|
3825
|
+
throw new Error([
|
|
3826
|
+
"Broker for WAM was requested to be enabled, but no native broker was configured.",
|
|
3827
|
+
"You must install the identity-broker plugin package (`npm install --save @azure/identity-broker`)",
|
|
3828
|
+
"and enable it by importing `useIdentityPlugin` from `@azure/identity` and calling",
|
|
3829
|
+
"`useIdentityPlugin(createNativeBrokerPlugin())` before using `enableBroker`.",
|
|
3830
|
+
].join(" "));
|
|
3831
|
+
}
|
|
3832
|
+
this.azureRegion = calculateRegionalAuthority(options.regionalAuthority);
|
|
3833
|
+
}
|
|
3834
|
+
/**
|
|
3835
|
+
* Generates a MSAL configuration that generally works for Node.js
|
|
3836
|
+
*/
|
|
3837
|
+
defaultNodeMsalConfig(options) {
|
|
3838
|
+
var _a;
|
|
3839
|
+
const clientId = options.clientId || DeveloperSignOnClientId;
|
|
3840
|
+
const tenantId = resolveTenantId(options.logger, options.tenantId, options.clientId);
|
|
3841
|
+
this.authorityHost = options.authorityHost || process.env.AZURE_AUTHORITY_HOST;
|
|
3842
|
+
const authority = getAuthority(tenantId, this.authorityHost);
|
|
3843
|
+
this.identityClient = new IdentityClient(Object.assign(Object.assign({}, options.tokenCredentialOptions), { authorityHost: authority, loggingOptions: options.loggingOptions }));
|
|
3844
|
+
const clientCapabilities = [];
|
|
3845
|
+
return {
|
|
3846
|
+
auth: {
|
|
3847
|
+
clientId,
|
|
3848
|
+
authority,
|
|
3849
|
+
knownAuthorities: getKnownAuthorities(tenantId, authority, options.disableInstanceDiscovery),
|
|
3850
|
+
clientCapabilities,
|
|
3851
|
+
},
|
|
3852
|
+
// Cache is defined in this.prepare();
|
|
3853
|
+
system: {
|
|
3854
|
+
networkClient: this.identityClient,
|
|
3855
|
+
loggerOptions: {
|
|
3856
|
+
loggerCallback: defaultLoggerCallback(options.logger),
|
|
3857
|
+
logLevel: getMSALLogLevel(logger$r.getLogLevel()),
|
|
3858
|
+
piiLoggingEnabled: (_a = options.loggingOptions) === null || _a === void 0 ? void 0 : _a.enableUnsafeSupportLogging,
|
|
3859
|
+
},
|
|
3860
|
+
},
|
|
3861
|
+
};
|
|
3862
|
+
}
|
|
3863
|
+
getApp(appType, enableCae) {
|
|
3864
|
+
const app = enableCae ? this.caeApp : this.app;
|
|
3865
|
+
if (appType === "publicFirst") {
|
|
3866
|
+
return (app.public || app.confidential);
|
|
3867
|
+
}
|
|
3868
|
+
else if (appType === "confidentialFirst") {
|
|
3869
|
+
return (app.confidential || app.public);
|
|
3870
|
+
}
|
|
3871
|
+
else if (appType === "confidential") {
|
|
3872
|
+
return app.confidential;
|
|
3873
|
+
}
|
|
3874
|
+
else {
|
|
3875
|
+
return app.public;
|
|
3876
|
+
}
|
|
3877
|
+
}
|
|
3878
|
+
/**
|
|
3879
|
+
* Prepares the MSAL applications.
|
|
3880
|
+
*/
|
|
3881
|
+
async init(options) {
|
|
3882
|
+
if (options === null || options === void 0 ? void 0 : options.abortSignal) {
|
|
3883
|
+
options.abortSignal.addEventListener("abort", () => {
|
|
3884
|
+
// This will abort any pending request in the IdentityClient,
|
|
3885
|
+
// based on the received or generated correlationId
|
|
3886
|
+
this.identityClient.abortRequests(options.correlationId);
|
|
3887
|
+
});
|
|
3888
|
+
}
|
|
3889
|
+
const app = (options === null || options === void 0 ? void 0 : options.enableCae) ? this.caeApp : this.app;
|
|
3890
|
+
if (options === null || options === void 0 ? void 0 : options.enableCae) {
|
|
3891
|
+
this.msalConfig.auth.clientCapabilities = ["cp1"];
|
|
3892
|
+
}
|
|
3893
|
+
if (app.public || app.confidential) {
|
|
3894
|
+
return;
|
|
3895
|
+
}
|
|
3896
|
+
if ((options === null || options === void 0 ? void 0 : options.enableCae) && this.createCachePluginCae !== undefined) {
|
|
3897
|
+
this.msalConfig.cache = {
|
|
3898
|
+
cachePlugin: await this.createCachePluginCae(),
|
|
3899
|
+
};
|
|
3900
|
+
}
|
|
3901
|
+
if (this.createCachePlugin !== undefined) {
|
|
3902
|
+
this.msalConfig.cache = {
|
|
3903
|
+
cachePlugin: await this.createCachePlugin(),
|
|
3904
|
+
};
|
|
3905
|
+
}
|
|
3906
|
+
if (hasNativeBroker() && this.enableBroker) {
|
|
3907
|
+
this.msalConfig.broker = {
|
|
3908
|
+
nativeBrokerPlugin: nativeBrokerInfo.broker,
|
|
3909
|
+
};
|
|
3910
|
+
if (!this.parentWindowHandle) {
|
|
3911
|
+
// error should have been thrown from within the constructor of InteractiveBrowserCredential
|
|
3912
|
+
this.logger.warning("Parent window handle is not specified for the broker. This may cause unexpected behavior. Please provide the parentWindowHandle.");
|
|
3913
|
+
}
|
|
3914
|
+
}
|
|
3915
|
+
if (options === null || options === void 0 ? void 0 : options.enableCae) {
|
|
3916
|
+
this.caeApp.public = new msalCommon__namespace.PublicClientApplication(this.msalConfig);
|
|
3917
|
+
}
|
|
3918
|
+
else {
|
|
3919
|
+
this.app.public = new msalCommon__namespace.PublicClientApplication(this.msalConfig);
|
|
3920
|
+
}
|
|
3921
|
+
if (this.getAssertion) {
|
|
3922
|
+
this.msalConfig.auth.clientAssertion = await this.getAssertion();
|
|
3923
|
+
}
|
|
3924
|
+
// The confidential client requires either a secret, assertion or certificate.
|
|
3925
|
+
if (this.msalConfig.auth.clientSecret ||
|
|
3926
|
+
this.msalConfig.auth.clientAssertion ||
|
|
3927
|
+
this.msalConfig.auth.clientCertificate) {
|
|
3928
|
+
if (options === null || options === void 0 ? void 0 : options.enableCae) {
|
|
3929
|
+
this.caeApp.confidential = new msalCommon__namespace.ConfidentialClientApplication(this.msalConfig);
|
|
3930
|
+
}
|
|
3931
|
+
else {
|
|
3932
|
+
this.app.confidential = new msalCommon__namespace.ConfidentialClientApplication(this.msalConfig);
|
|
3933
|
+
}
|
|
3934
|
+
}
|
|
3935
|
+
else {
|
|
3936
|
+
if (this.requiresConfidential) {
|
|
3937
|
+
throw new Error("Unable to generate the MSAL confidential client. Missing either the client's secret, certificate or assertion.");
|
|
3938
|
+
}
|
|
3939
|
+
}
|
|
3940
|
+
}
|
|
3941
|
+
/**
|
|
3942
|
+
* Allows the cancellation of a MSAL request.
|
|
3943
|
+
*/
|
|
3944
|
+
withCancellation(promise, abortSignal, onCancel) {
|
|
3945
|
+
return new Promise((resolve, reject) => {
|
|
3946
|
+
promise
|
|
3947
|
+
.then((msalToken) => {
|
|
3948
|
+
return resolve(msalToken);
|
|
3949
|
+
})
|
|
3950
|
+
.catch(reject);
|
|
3951
|
+
if (abortSignal) {
|
|
3952
|
+
abortSignal.addEventListener("abort", () => {
|
|
3953
|
+
onCancel === null || onCancel === void 0 ? void 0 : onCancel();
|
|
3954
|
+
});
|
|
3955
|
+
}
|
|
3956
|
+
});
|
|
3957
|
+
}
|
|
3958
|
+
/**
|
|
3959
|
+
* Returns the existing account, attempts to load the account from MSAL.
|
|
3960
|
+
*/
|
|
3961
|
+
async getActiveAccount(enableCae = false) {
|
|
3962
|
+
if (this.account) {
|
|
3963
|
+
return this.account;
|
|
3964
|
+
}
|
|
3965
|
+
const cache = this.getApp("confidentialFirst", enableCae).getTokenCache();
|
|
3966
|
+
const accountsByTenant = await (cache === null || cache === void 0 ? void 0 : cache.getAllAccounts());
|
|
3967
|
+
if (!accountsByTenant) {
|
|
3968
|
+
return;
|
|
3969
|
+
}
|
|
3970
|
+
if (accountsByTenant.length === 1) {
|
|
3971
|
+
this.account = msalToPublic(this.clientId, accountsByTenant[0]);
|
|
3972
|
+
}
|
|
3973
|
+
else {
|
|
3974
|
+
this.logger
|
|
3975
|
+
.info(`More than one account was found authenticated for this Client ID and Tenant ID.
|
|
3976
|
+
However, no "authenticationRecord" has been provided for this credential,
|
|
3977
|
+
therefore we're unable to pick between these accounts.
|
|
3978
|
+
A new login attempt will be requested, to ensure the correct account is picked.
|
|
3979
|
+
To work with multiple accounts for the same Client ID and Tenant ID, please provide an "authenticationRecord" when initializing a credential to prevent this from happening.`);
|
|
3980
|
+
return;
|
|
3981
|
+
}
|
|
3982
|
+
return this.account;
|
|
3983
|
+
}
|
|
3984
|
+
/**
|
|
3985
|
+
* Attempts to retrieve a token from cache.
|
|
3986
|
+
*/
|
|
3987
|
+
async getTokenSilent(scopes, options) {
|
|
3988
|
+
var _a, _b, _c;
|
|
3989
|
+
await this.getActiveAccount(options === null || options === void 0 ? void 0 : options.enableCae);
|
|
3990
|
+
if (!this.account) {
|
|
3991
|
+
throw new AuthenticationRequiredError({
|
|
3992
|
+
scopes,
|
|
3993
|
+
getTokenOptions: options,
|
|
3994
|
+
message: "Silent authentication failed. We couldn't retrieve an active account from the cache.",
|
|
3995
|
+
});
|
|
3996
|
+
}
|
|
3997
|
+
const silentRequest = {
|
|
3998
|
+
// To be able to re-use the account, the Token Cache must also have been provided.
|
|
3999
|
+
account: publicToMsal(this.account),
|
|
4000
|
+
correlationId: options === null || options === void 0 ? void 0 : options.correlationId,
|
|
4001
|
+
scopes,
|
|
4002
|
+
authority: options === null || options === void 0 ? void 0 : options.authority,
|
|
4003
|
+
claims: options === null || options === void 0 ? void 0 : options.claims,
|
|
4004
|
+
};
|
|
4005
|
+
if (hasNativeBroker() && this.enableBroker) {
|
|
4006
|
+
if (!silentRequest.tokenQueryParameters) {
|
|
4007
|
+
silentRequest.tokenQueryParameters = {};
|
|
4008
|
+
}
|
|
4009
|
+
if (!this.parentWindowHandle) {
|
|
4010
|
+
// error should have been thrown from within the constructor of InteractiveBrowserCredential
|
|
4011
|
+
this.logger.warning("Parent window handle is not specified for the broker. This may cause unexpected behavior. Please provide the parentWindowHandle.");
|
|
4012
|
+
}
|
|
4013
|
+
if (this.enableMsaPassthrough) {
|
|
4014
|
+
silentRequest.tokenQueryParameters["msal_request_type"] = "consumer_passthrough";
|
|
4015
|
+
}
|
|
4016
|
+
}
|
|
4017
|
+
try {
|
|
4018
|
+
this.logger.info("Attempting to acquire token silently");
|
|
4019
|
+
/**
|
|
4020
|
+
* The following code to retrieve all accounts is done as a workaround in an attempt to force the
|
|
4021
|
+
* refresh of the token cache with the token and the account passed in through the
|
|
4022
|
+
* `authenticationRecord` parameter. See issue - https://github.com/Azure/azure-sdk-for-js/issues/24349#issuecomment-1496715651
|
|
4023
|
+
* This workaround serves as a workaround for silent authentication not happening when authenticationRecord is passed.
|
|
4024
|
+
*/
|
|
4025
|
+
await ((_a = this.getApp("publicFirst", options === null || options === void 0 ? void 0 : options.enableCae)) === null || _a === void 0 ? void 0 : _a.getTokenCache().getAllAccounts());
|
|
4026
|
+
const response = (_c = (await ((_b = this.getApp("confidential", options === null || options === void 0 ? void 0 : options.enableCae)) === null || _b === void 0 ? void 0 : _b.acquireTokenSilent(silentRequest)))) !== null && _c !== void 0 ? _c : (await this.getApp("public", options === null || options === void 0 ? void 0 : options.enableCae).acquireTokenSilent(silentRequest));
|
|
4027
|
+
return this.handleResult(scopes, response || undefined);
|
|
4028
|
+
}
|
|
4029
|
+
catch (err) {
|
|
4030
|
+
throw handleMsalError(scopes, err, options);
|
|
4031
|
+
}
|
|
4032
|
+
}
|
|
4033
|
+
/**
|
|
4034
|
+
* Wrapper around each MSAL flow get token operation: doGetToken.
|
|
4035
|
+
* If disableAutomaticAuthentication is sent through the constructor, it will prevent MSAL from requesting the user input.
|
|
4036
|
+
*/
|
|
4037
|
+
async getToken(scopes, options = {}) {
|
|
4038
|
+
const tenantId = processMultiTenantRequest(this.tenantId, options, this.additionallyAllowedTenantIds) ||
|
|
4039
|
+
this.tenantId;
|
|
4040
|
+
options.authority = getAuthority(tenantId, this.authorityHost);
|
|
4041
|
+
options.correlationId = (options === null || options === void 0 ? void 0 : options.correlationId) || randomUUID();
|
|
4042
|
+
await this.init(options);
|
|
4043
|
+
try {
|
|
4044
|
+
// MSAL now caches tokens based on their claims,
|
|
4045
|
+
// so now one has to keep track fo claims in order to retrieve the newer tokens from acquireTokenSilent
|
|
4046
|
+
// This update happened on PR: https://github.com/AzureAD/microsoft-authentication-library-for-js/pull/4533
|
|
4047
|
+
const optionsClaims = options.claims;
|
|
4048
|
+
if (optionsClaims) {
|
|
4049
|
+
this.cachedClaims = optionsClaims;
|
|
4050
|
+
}
|
|
4051
|
+
if (this.cachedClaims && !optionsClaims) {
|
|
4052
|
+
options.claims = this.cachedClaims;
|
|
4053
|
+
}
|
|
4054
|
+
// We don't return the promise since we want to catch errors right here.
|
|
4055
|
+
return await this.getTokenSilent(scopes, options);
|
|
4056
|
+
}
|
|
4057
|
+
catch (err) {
|
|
4058
|
+
if (err.name !== "AuthenticationRequiredError") {
|
|
4059
|
+
throw err;
|
|
4060
|
+
}
|
|
4061
|
+
if (options === null || options === void 0 ? void 0 : options.disableAutomaticAuthentication) {
|
|
4062
|
+
throw new AuthenticationRequiredError({
|
|
4063
|
+
scopes,
|
|
4064
|
+
getTokenOptions: options,
|
|
4065
|
+
message: "Automatic authentication has been disabled. You may call the authentication() method.",
|
|
4066
|
+
});
|
|
4067
|
+
}
|
|
4068
|
+
this.logger.info(`Silent authentication failed, falling back to interactive method.`);
|
|
4069
|
+
return this.doGetToken(scopes, options);
|
|
4070
|
+
}
|
|
4071
|
+
}
|
|
4072
|
+
/**
|
|
4073
|
+
* Handles the MSAL authentication result.
|
|
4074
|
+
* If the result has an account, we update the local account reference.
|
|
4075
|
+
* If the token received is invalid, an error will be thrown depending on what's missing.
|
|
4076
|
+
*/
|
|
4077
|
+
handleResult(scopes, result, getTokenOptions) {
|
|
4078
|
+
if (result === null || result === void 0 ? void 0 : result.account) {
|
|
4079
|
+
this.account = msalToPublic(this.clientId, result.account);
|
|
4080
|
+
}
|
|
4081
|
+
ensureValidMsalToken(scopes, result, getTokenOptions);
|
|
4082
|
+
this.logger.getToken.info(formatSuccess(scopes));
|
|
4083
|
+
return {
|
|
4084
|
+
token: result.accessToken,
|
|
4085
|
+
expiresOnTimestamp: result.expiresOn.getTime(),
|
|
4086
|
+
};
|
|
4087
|
+
}
|
|
4088
|
+
}
|
|
4089
|
+
|
|
4108
4090
|
// Copyright (c) Microsoft Corporation.
|
|
4109
4091
|
// Licensed under the MIT license.
|
|
4110
4092
|
/**
|