UNPKG

@microsoft/teams.api

Version:

<p> <a href="https://www.npmjs.com/package/@microsoft/teams.api" target="_blank"> <img src="https://img.shields.io/npm/v/@microsoft/teams.api/latest" /> </a> <a href="https://www.npmjs.com/package/@microsoft/teams.api?activeTab=code" t

74 lines (71 loc) • 2.99 kB
'use strict'; var api = require('@opentelemetry/api'); var cloudEnvironment = require('../auth/cloud-environment'); var constants = require('../diagnostics/constants'); var helpers = require('../diagnostics/helpers'); var models = require('../models'); function createTokenProviderFactory(tokenProvider, defaultAgenticIdentity, cloud = cloudEnvironment.PUBLIC) { return () => { return traceAuthTokenAcquisition(getAuthFlow(defaultAgenticIdentity), async () => { if (!defaultAgenticIdentity) { return await tokenProvider.getAppToken(cloud.botScope) ?? void 0; } if (models.isUserBackedAgenticIdentity(defaultAgenticIdentity)) { if (!tokenProvider.getAgenticUserToken) { throw new Error( "This client is scoped to a user-backed AgenticIdentity, but the configured token provider does not implement `getAgenticUserToken`. Falling back to an app-only token would authenticate under the wrong identity." ); } return await tokenProvider.getAgenticUserToken( cloud.agenticIdentityBotScope, requireAgenticAppId(defaultAgenticIdentity, "user-backed"), defaultAgenticIdentity.agenticUserId, defaultAgenticIdentity.tenantId ) ?? void 0; } if (!tokenProvider.getAgenticAppToken) { throw new Error( "This client is scoped to an app-backed AgenticIdentity, but the configured token provider does not implement `getAgenticAppToken`. Falling back to an app-only token would authenticate under the wrong identity." ); } return await tokenProvider.getAgenticAppToken( cloud.agenticIdentityBotScope, requireAgenticAppId(defaultAgenticIdentity, "app-backed"), defaultAgenticIdentity.tenantId ) ?? void 0; }); }; } function getAuthFlow(agenticIdentity) { if (!agenticIdentity) { return constants.AUTH_FLOWS.appOnly; } return models.isUserBackedAgenticIdentity(agenticIdentity) ? constants.AUTH_FLOWS.agenticUser : constants.AUTH_FLOWS.agenticApp; } function requireAgenticAppId(agenticIdentity, identityFlavor) { if (!agenticIdentity.agenticAppId) { throw new Error( `agenticAppId is required for ${identityFlavor} AgenticIdentity token acquisition` ); } return agenticIdentity.agenticAppId; } async function traceAuthTokenAcquisition(authFlow, acquireToken) { return helpers.getTeamsApiTracer().startActiveSpan( constants.API_SPAN_NAMES.authOutbound, { kind: api.SpanKind.CLIENT, attributes: { [constants.API_ATTRIBUTE_NAMES.authFlow]: authFlow } }, async (span) => { try { return await acquireToken(); } catch (error) { helpers.recordTeamsApiException(span, error); throw error; } finally { span.end(); } } ); } exports.createTokenProviderFactory = createTokenProviderFactory; //# sourceMappingURL=token-provider-factory.js.map //# sourceMappingURL=token-provider-factory.js.map