@azure/identity
Version:
Provides credential implementations for Azure SDK libraries that can authenticate with Microsoft Entra ID
107 lines (106 loc) • 4.25 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var brokerCredential_exports = {};
__export(brokerCredential_exports, {
BrokerCredential: () => BrokerCredential
});
module.exports = __toCommonJS(brokerCredential_exports);
var import_tenantIdUtils = require("../util/tenantIdUtils.js");
var import_logging = require("../util/logging.js");
var import_scopeUtils = require("../util/scopeUtils.js");
var import_tracing = require("../util/tracing.js");
var import_msalClient = require("../msal/nodeFlows/msalClient.js");
var import_constants = require("../constants.js");
var import_errors = require("../errors.js");
const logger = (0, import_logging.credentialLogger)("BrokerCredential");
class BrokerCredential {
brokerMsalClient;
brokerTenantId;
brokerAdditionallyAllowedTenantIds;
/**
* Creates an instance of BrokerCredential with the required broker options.
*
* This credential uses WAM (Web Account Manager) for authentication, which provides
* better security and user experience on Windows platforms.
*
* @param options - Options for configuring the broker credential, including required broker options.
*/
constructor(options) {
this.brokerTenantId = (0, import_tenantIdUtils.resolveTenantId)(logger, options.tenantId);
this.brokerAdditionallyAllowedTenantIds = (0, import_tenantIdUtils.resolveAdditionallyAllowedTenantIds)(
options?.additionallyAllowedTenants
);
const msalClientOptions = {
...options,
tokenCredentialOptions: options,
logger,
brokerOptions: {
enabled: true,
parentWindowHandle: new Uint8Array(0),
useDefaultBrokerAccount: true
}
};
this.brokerMsalClient = (0, import_msalClient.createMsalClient)(
import_constants.DeveloperSignOnClientId,
this.brokerTenantId,
msalClientOptions
);
}
/**
* Authenticates with Microsoft Entra ID using WAM broker and returns an access token if successful.
* If authentication fails, a {@link CredentialUnavailableError} will be thrown with the details of the failure.
*
* This method extends the base getToken method to support silentAuthenticationOnly option
* when using broker authentication.
*
* @param scopes - The list of scopes for which the token will have access.
* @param options - The options used to configure the token request, including silentAuthenticationOnly option.
*/
async getToken(scopes, options = {}) {
return import_tracing.tracingClient.withSpan(
`${this.constructor.name}.getToken`,
options,
async (newOptions) => {
newOptions.tenantId = (0, import_tenantIdUtils.processMultiTenantRequest)(
this.brokerTenantId,
newOptions,
this.brokerAdditionallyAllowedTenantIds,
logger
);
const arrayScopes = (0, import_scopeUtils.ensureScopes)(scopes);
try {
return this.brokerMsalClient.getBrokeredToken(arrayScopes, true, {
...newOptions,
disableAutomaticAuthentication: true
});
} catch (e) {
logger.getToken.info((0, import_logging.formatError)(arrayScopes, e));
throw new import_errors.CredentialUnavailableError(
"Failed to acquire token using broker authentication",
{ cause: e }
);
}
}
);
}
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BrokerCredential
});
//# sourceMappingURL=brokerCredential.js.map