@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
72 lines (70 loc) • 2.97 kB
JavaScript
import { SpanKind } from '@opentelemetry/api';
import { PUBLIC } from '../auth/cloud-environment.mjs';
import { AUTH_FLOWS, API_SPAN_NAMES, API_ATTRIBUTE_NAMES } from '../diagnostics/constants.mjs';
import { getTeamsApiTracer, recordTeamsApiException } from '../diagnostics/helpers.mjs';
import { isUserBackedAgenticIdentity } from '../models/index.mjs';
function createTokenProviderFactory(tokenProvider, defaultAgenticIdentity, cloud = PUBLIC) {
return () => {
return traceAuthTokenAcquisition(getAuthFlow(defaultAgenticIdentity), async () => {
if (!defaultAgenticIdentity) {
return await tokenProvider.getAppToken(cloud.botScope) ?? void 0;
}
if (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 AUTH_FLOWS.appOnly;
}
return isUserBackedAgenticIdentity(agenticIdentity) ? AUTH_FLOWS.agenticUser : 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 getTeamsApiTracer().startActiveSpan(
API_SPAN_NAMES.authOutbound,
{ kind: SpanKind.CLIENT, attributes: { [API_ATTRIBUTE_NAMES.authFlow]: authFlow } },
async (span) => {
try {
return await acquireToken();
} catch (error) {
recordTeamsApiException(span, error);
throw error;
} finally {
span.end();
}
}
);
}
export { createTokenProviderFactory };
//# sourceMappingURL=token-provider-factory.mjs.map
//# sourceMappingURL=token-provider-factory.mjs.map