UNPKG

@microsoft/signalr

Version:
50 lines 2.28 kB
"use strict"; // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. Object.defineProperty(exports, "__esModule", { value: true }); exports.AccessTokenHttpClient = void 0; const HeaderNames_1 = require("./HeaderNames"); const HttpClient_1 = require("./HttpClient"); /** @private */ class AccessTokenHttpClient extends HttpClient_1.HttpClient { constructor(innerClient, accessTokenFactory) { super(); this._innerClient = innerClient; this._accessTokenFactory = accessTokenFactory; } async send(request) { let allowRetry = true; if (this._accessTokenFactory && (!this._accessToken || (request.url && request.url.indexOf("/negotiate?") > 0))) { // don't retry if the request is a negotiate or if we just got a potentially new token from the access token factory allowRetry = false; this._accessToken = await this._accessTokenFactory(); } this._setAuthorizationHeader(request); const response = await this._innerClient.send(request); if (allowRetry && response.statusCode === 401 && this._accessTokenFactory) { this._accessToken = await this._accessTokenFactory(); this._setAuthorizationHeader(request); return await this._innerClient.send(request); } return response; } _setAuthorizationHeader(request) { if (!request.headers) { request.headers = {}; } if (this._accessToken) { request.headers[HeaderNames_1.HeaderNames.Authorization] = `Bearer ${this._accessToken}`; } // don't remove the header if there isn't an access token factory, the user manually added the header in this case else if (this._accessTokenFactory) { if (request.headers[HeaderNames_1.HeaderNames.Authorization]) { delete request.headers[HeaderNames_1.HeaderNames.Authorization]; } } } getCookieString(url) { return this._innerClient.getCookieString(url); } } exports.AccessTokenHttpClient = AccessTokenHttpClient; //# sourceMappingURL=AccessTokenHttpClient.js.map