12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- // Licensed to the .NET Foundation under one or more agreements.
- // The .NET Foundation licenses this file to you under the MIT license.
- import { HeaderNames } from "./HeaderNames";
- import { HttpClient } from "./HttpClient";
- /** @private */
- export class AccessTokenHttpClient extends 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.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.Authorization]) {
- delete request.headers[HeaderNames.Authorization];
- }
- }
- }
- getCookieString(url) {
- return this._innerClient.getCookieString(url);
- }
- }
- //# sourceMappingURL=AccessTokenHttpClient.js.map
|