@azure/identity 4.3.1-alpha.20240627.1 → 4.3.1-alpha.20240702.3

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.js CHANGED
@@ -2056,6 +2056,16 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2056
2056
  state.logger.getToken.info("Attempting to acquire token silently");
2057
2057
  return app.acquireTokenSilent(silentRequest);
2058
2058
  }
2059
+ /**
2060
+ * Builds an authority URL for the given request. The authority may be different than the one used when creating the MSAL client
2061
+ * if the user is creating cross-tenant requests
2062
+ */
2063
+ function calculateRequestAuthority(options) {
2064
+ if (options === null || options === void 0 ? void 0 : options.tenantId) {
2065
+ return getAuthority(options.tenantId, createMsalClientOptions.authorityHost);
2066
+ }
2067
+ return state.msalConfig.auth.authority;
2068
+ }
2059
2069
  /**
2060
2070
  * Performs silent authentication using MSAL to acquire an access token.
2061
2071
  * If silent authentication fails, falls back to interactive authentication.
@@ -2109,7 +2119,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2109
2119
  try {
2110
2120
  const response = await msalApp.acquireTokenByClientCredential({
2111
2121
  scopes,
2112
- authority: state.msalConfig.auth.authority,
2122
+ authority: calculateRequestAuthority(options),
2113
2123
  azureRegion: calculateRegionalAuthority(),
2114
2124
  claims: options === null || options === void 0 ? void 0 : options.claims,
2115
2125
  });
@@ -2131,7 +2141,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2131
2141
  try {
2132
2142
  const response = await msalApp.acquireTokenByClientCredential({
2133
2143
  scopes,
2134
- authority: state.msalConfig.auth.authority,
2144
+ authority: calculateRequestAuthority(options),
2135
2145
  azureRegion: calculateRegionalAuthority(),
2136
2146
  claims: options === null || options === void 0 ? void 0 : options.claims,
2137
2147
  clientAssertion,
@@ -2154,7 +2164,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2154
2164
  try {
2155
2165
  const response = await msalApp.acquireTokenByClientCredential({
2156
2166
  scopes,
2157
- authority: state.msalConfig.auth.authority,
2167
+ authority: calculateRequestAuthority(options),
2158
2168
  azureRegion: calculateRegionalAuthority(),
2159
2169
  claims: options === null || options === void 0 ? void 0 : options.claims,
2160
2170
  });
@@ -2178,7 +2188,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2178
2188
  scopes,
2179
2189
  cancel: (_b = (_a = options === null || options === void 0 ? void 0 : options.abortSignal) === null || _a === void 0 ? void 0 : _a.aborted) !== null && _b !== void 0 ? _b : false,
2180
2190
  deviceCodeCallback,
2181
- authority: state.msalConfig.auth.authority,
2191
+ authority: calculateRequestAuthority(options),
2182
2192
  claims: options === null || options === void 0 ? void 0 : options.claims,
2183
2193
  };
2184
2194
  const deviceCodeRequest = msalApp.acquireTokenByDeviceCode(requestOptions);
@@ -2198,7 +2208,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2198
2208
  scopes,
2199
2209
  username,
2200
2210
  password,
2201
- authority: state.msalConfig.auth.authority,
2211
+ authority: calculateRequestAuthority(options),
2202
2212
  claims: options === null || options === void 0 ? void 0 : options.claims,
2203
2213
  };
2204
2214
  return msalApp.acquireTokenByUsernamePassword(requestOptions);
@@ -2227,28 +2237,33 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2227
2237
  scopes,
2228
2238
  redirectUri,
2229
2239
  code: authorizationCode,
2230
- authority: state.msalConfig.auth.authority,
2240
+ authority: calculateRequestAuthority(options),
2231
2241
  claims: options === null || options === void 0 ? void 0 : options.claims,
2232
2242
  });
2233
2243
  });
2234
2244
  }
2235
- async function getTokenOnBehalfOf(scopes, userAssertionToken, clientSecretOrCertificate, options = {}) {
2245
+ async function getTokenOnBehalfOf(scopes, userAssertionToken, clientCredentials, options = {}) {
2236
2246
  msalLogger.getToken.info(`Attempting to acquire token on behalf of another user`);
2237
- if (typeof clientSecretOrCertificate === "string") {
2247
+ if (typeof clientCredentials === "string") {
2238
2248
  // Client secret
2239
2249
  msalLogger.getToken.info(`Using client secret for on behalf of flow`);
2240
- state.msalConfig.auth.clientSecret = clientSecretOrCertificate;
2250
+ state.msalConfig.auth.clientSecret = clientCredentials;
2251
+ }
2252
+ else if (typeof clientCredentials === "function") {
2253
+ // Client Assertion
2254
+ msalLogger.getToken.info(`Using client assertion callback for on behalf of flow`);
2255
+ state.msalConfig.auth.clientAssertion = clientCredentials;
2241
2256
  }
2242
2257
  else {
2243
2258
  // Client certificate
2244
2259
  msalLogger.getToken.info(`Using client certificate for on behalf of flow`);
2245
- state.msalConfig.auth.clientCertificate = clientSecretOrCertificate;
2260
+ state.msalConfig.auth.clientCertificate = clientCredentials;
2246
2261
  }
2247
2262
  const msalApp = await getConfidentialApp(options);
2248
2263
  try {
2249
2264
  const response = await msalApp.acquireTokenOnBehalfOf({
2250
2265
  scopes,
2251
- authority: state.msalConfig.auth.authority,
2266
+ authority: calculateRequestAuthority(options),
2252
2267
  claims: options.claims,
2253
2268
  oboAssertion: userAssertionToken,
2254
2269
  });
@@ -2315,7 +2330,7 @@ To work with multiple accounts for the same Client ID and Tenant ID, please prov
2315
2330
  await interactiveBrowserMockable.open(url, { wait: true, newInstance: true });
2316
2331
  },
2317
2332
  scopes,
2318
- authority: state.msalConfig.auth.authority,
2333
+ authority: calculateRequestAuthority(options),
2319
2334
  claims: options === null || options === void 0 ? void 0 : options.claims,
2320
2335
  loginHint: options === null || options === void 0 ? void 0 : options.loginHint,
2321
2336
  errorTemplate: (_a = options === null || options === void 0 ? void 0 : options.browserCustomizationOptions) === null || _a === void 0 ? void 0 : _a.errorMessage,
@@ -2382,9 +2397,8 @@ class ClientAssertionCredential {
2382
2397
  async getToken(scopes, options = {}) {
2383
2398
  return tracingClient.withSpan(`${this.constructor.name}.getToken`, options, async (newOptions) => {
2384
2399
  newOptions.tenantId = processMultiTenantRequest(this.tenantId, newOptions, this.additionallyAllowedTenantIds, logger$h);
2385
- const clientAssertion = await this.getAssertion();
2386
2400
  const arrayScopes = Array.isArray(scopes) ? scopes : [scopes];
2387
- return this.msalClient.getTokenByClientAssertion(arrayScopes, clientAssertion, newOptions);
2401
+ return this.msalClient.getTokenByClientAssertion(arrayScopes, this.getAssertion, newOptions);
2388
2402
  });
2389
2403
  }
2390
2404
  }
@@ -4278,14 +4292,19 @@ class OnBehalfOfCredential {
4278
4292
  constructor(options) {
4279
4293
  const { clientSecret } = options;
4280
4294
  const { certificatePath, sendCertificateChain } = options;
4295
+ const { getAssertion } = options;
4281
4296
  const { tenantId, clientId, userAssertionToken, additionallyAllowedTenants: additionallyAllowedTenantIds, } = options;
4282
- if (!tenantId || !clientId || !(clientSecret || certificatePath) || !userAssertionToken) {
4283
- throw new Error(`${credentialName}: tenantId, clientId, clientSecret (or certificatePath) and userAssertionToken are required parameters.`);
4297
+ if (!tenantId ||
4298
+ !clientId ||
4299
+ !(clientSecret || certificatePath || getAssertion) ||
4300
+ !userAssertionToken) {
4301
+ throw new Error(`${credentialName}: tenantId, clientId, clientSecret (or certificatePath or getAssertion) and userAssertionToken are required parameters.`);
4284
4302
  }
4285
4303
  this.certificatePath = certificatePath;
4286
4304
  this.clientSecret = clientSecret;
4287
4305
  this.userAssertionToken = userAssertionToken;
4288
4306
  this.sendCertificateChain = sendCertificateChain;
4307
+ this.clientAssertion = getAssertion;
4289
4308
  this.tenantId = tenantId;
4290
4309
  this.additionallyAllowedTenantIds = resolveAdditionallyAllowedTenantIds(additionallyAllowedTenantIds);
4291
4310
  this.msalClient = createMsalClient(clientId, this.tenantId, Object.assign(Object.assign({}, options), { logger, tokenCredentialOptions: options }));
@@ -4308,9 +4327,12 @@ class OnBehalfOfCredential {
4308
4327
  else if (this.clientSecret) {
4309
4328
  return this.msalClient.getTokenOnBehalfOf(arrayScopes, this.userAssertionToken, this.clientSecret, options);
4310
4329
  }
4330
+ else if (this.clientAssertion) {
4331
+ return this.msalClient.getTokenOnBehalfOf(arrayScopes, this.userAssertionToken, this.clientAssertion, options);
4332
+ }
4311
4333
  else {
4312
- // this is a bug, as the constructor should have thrown an error if neither clientSecret nor certificatePath were provided
4313
- throw new Error("Expected either clientSecret or certificatePath to be defined.");
4334
+ // this is an invalid scenario and is a bug, as the constructor should have thrown an error if neither clientSecret nor certificatePath nor clientAssertion were provided
4335
+ throw new Error("Expected either clientSecret or certificatePath or clientAssertion to be defined.");
4314
4336
  }
4315
4337
  });
4316
4338
  }