UNPKG

1.76 kBJavaScriptView Raw
1// Copyright (c) Microsoft Corporation. All rights reserved.
2// Licensed under the MIT License. See License.txt in the project root for license information.
3import { HttpHeaders } from "../httpHeaders";
4import { Constants } from "../util/constants";
5var HeaderConstants = Constants.HeaderConstants;
6var DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
7/**
8 * A credentials object that uses a token string and a authorzation scheme to authenticate.
9 */
10var TokenCredentials = /** @class */ (function () {
11 /**
12 * Creates a new TokenCredentials object.
13 *
14 * @constructor
15 * @param {string} token The token.
16 * @param {string} [authorizationScheme] The authorization scheme.
17 */
18 function TokenCredentials(token, authorizationScheme) {
19 if (authorizationScheme === void 0) { authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME; }
20 this.authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME;
21 if (!token) {
22 throw new Error("token cannot be null or undefined.");
23 }
24 this.token = token;
25 this.authorizationScheme = authorizationScheme;
26 }
27 /**
28 * Signs a request with the Authentication header.
29 *
30 * @param {WebResourceLike} webResource The WebResourceLike to be signed.
31 * @return {Promise<WebResourceLike>} The signed request object.
32 */
33 TokenCredentials.prototype.signRequest = function (webResource) {
34 if (!webResource.headers)
35 webResource.headers = new HttpHeaders();
36 webResource.headers.set(HeaderConstants.AUTHORIZATION, this.authorizationScheme + " " + this.token);
37 return Promise.resolve(webResource);
38 };
39 return TokenCredentials;
40}());
41export { TokenCredentials };
42//# sourceMappingURL=tokenCredentials.js.map
\No newline at end of file