@microsoft/kiota-abstractions
Version:
Core abstractions for kiota generated libraries in TypeScript and JavaScript
37 lines • 2.15 kB
JavaScript
/**
* -------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All Rights Reserved. Licensed under the MIT License.
* See License in the project root for license information.
* -------------------------------------------------------------------------------------------
*/
import { Headers } from "../headers.js";
/** Provides a base class for implementing AuthenticationProvider for Bearer token scheme. */
export class BaseBearerTokenAuthenticationProvider {
/**
* The constructor for the BaseBearerTokenAuthenticationProvider
* @param accessTokenProvider The AccessTokenProvider instance that this provider will use to authenticate requests.
*/
constructor(accessTokenProvider) {
this.accessTokenProvider = accessTokenProvider;
this.authenticateRequest = async (request, additionalAuthenticationContext) => {
var _a;
if (!request) {
throw new Error("request info cannot be null");
}
if ((additionalAuthenticationContext === null || additionalAuthenticationContext === void 0 ? void 0 : additionalAuthenticationContext.claims) && request.headers.has(BaseBearerTokenAuthenticationProvider.authorizationHeaderKey)) {
request.headers.delete(BaseBearerTokenAuthenticationProvider.authorizationHeaderKey);
}
if (!((_a = request.headers) === null || _a === void 0 ? void 0 : _a.has(BaseBearerTokenAuthenticationProvider.authorizationHeaderKey))) {
const token = await this.accessTokenProvider.getAuthorizationToken(request.URL, additionalAuthenticationContext);
if (!request.headers) {
request.headers = new Headers();
}
if (token) {
request.headers.add(BaseBearerTokenAuthenticationProvider.authorizationHeaderKey, `Bearer ${token}`);
}
}
};
}
}
BaseBearerTokenAuthenticationProvider.authorizationHeaderKey = "Authorization";
//# sourceMappingURL=baseBearerTokenAuthenticationProvider.js.map