UNPKG

@microsoft/kiota-authentication-spfx

Version:

Authentication provider for using Kiota in SPFx solutions

63 lines 3.41 kB
/** * ------------------------------------------------------------------------------------------- * Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License. * See License in the project root for license information. * ------------------------------------------------------------------------------------------- */ import { AllowedHostsValidator, validateProtocol } from "@microsoft/kiota-abstractions"; import { trace } from "@opentelemetry/api"; import { ObservabilityOptionsImpl } from "./observabilityOptions.js"; export class AzureAdSpfxAccessTokenProvider { /** *@param tokenProvider The tokenProvider provided by the SharePoint framework *@param applicationIdUri The application ID URI of the Azure AD App that we want to Authenticate *@param allowedHosts The allowed hosts to use for authentication. *@param useCachedToken Allows the developer to specify if cached tokens should be returned. *@param observabilityOptions The observability options to use for authentication. */ constructor(tokenProvider, applicationIdUri, allowedHosts = new Set(), useCachedToken, observabilityOptions = new ObservabilityOptionsImpl()) { this.tokenProvider = tokenProvider; this.applicationIdUri = applicationIdUri; this.useCachedToken = useCachedToken; this.observabilityOptions = observabilityOptions; /** * @inheritdoc */ this.getAuthorizationToken = (url, additionalAuthenticationContext) => { return trace.getTracer(this.observabilityOptions.getTracerInstrumentationName()).startActiveSpan("getAuthorizationToken", (span) => { try { return this.getAuthorizationTokenInternal(url, additionalAuthenticationContext, span); } finally { span.end(); } }); }; this.getAuthorizationTokenInternal = async (url, additionalAuthenticationContext, span) => { if (!url || !this.allowedHostsValidator.isUrlHostValid(url)) { span === null || span === void 0 ? void 0 : span.setAttribute("com.microsoft.kiota.authentication.is_url_valid", false); return ""; } validateProtocol(url); span === null || span === void 0 ? void 0 : span.setAttribute("com.microsoft.kiota.authentication.is_url_valid", true); span === null || span === void 0 ? void 0 : span.setAttribute("com.microsoft.kiota.authentication.scopes", this.applicationIdUri); const accessToken = await this.tokenProvider.getToken(this.applicationIdUri, this.useCachedToken); return accessToken !== null && accessToken !== void 0 ? accessToken : ""; }; /** * @inheritdoc */ this.getAllowedHostsValidator = () => this.allowedHostsValidator; if (!tokenProvider) { throw new Error("parameter tokenProvider cannot be null"); } if (!applicationIdUri) { throw new Error("applicationIdUri cannot be null or empty"); } if (!observabilityOptions) { throw new Error("observabilityOptions cannot be null"); } this.allowedHostsValidator = new AllowedHostsValidator(allowedHosts); } } //# sourceMappingURL=azureAdSpfxAccessTokenProvider.js.map