UNPKG

2.35 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 * as base64 from "../util/base64";
5import { Constants } from "../util/constants";
6var HeaderConstants = Constants.HeaderConstants;
7var DEFAULT_AUTHORIZATION_SCHEME = "Basic";
8var BasicAuthenticationCredentials = /** @class */ (function () {
9 /**
10 * Creates a new BasicAuthenticationCredentials object.
11 *
12 * @constructor
13 * @param {string} userName User name.
14 * @param {string} password Password.
15 * @param {string} [authorizationScheme] The authorization scheme.
16 */
17 function BasicAuthenticationCredentials(userName, password, authorizationScheme) {
18 if (authorizationScheme === void 0) { authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME; }
19 this.authorizationScheme = DEFAULT_AUTHORIZATION_SCHEME;
20 if (userName === null || userName === undefined || typeof userName.valueOf() !== "string") {
21 throw new Error("userName cannot be null or undefined and must be of type string.");
22 }
23 if (password === null || password === undefined || typeof password.valueOf() !== "string") {
24 throw new Error("password cannot be null or undefined and must be of type string.");
25 }
26 this.userName = userName;
27 this.password = password;
28 this.authorizationScheme = authorizationScheme;
29 }
30 /**
31 * Signs a request with the Authentication header.
32 *
33 * @param {WebResourceLike} webResource The WebResourceLike to be signed.
34 * @returns {Promise<WebResourceLike>} The signed request object.
35 */
36 BasicAuthenticationCredentials.prototype.signRequest = function (webResource) {
37 var credentials = this.userName + ":" + this.password;
38 var encodedCredentials = this.authorizationScheme + " " + base64.encodeString(credentials);
39 if (!webResource.headers)
40 webResource.headers = new HttpHeaders();
41 webResource.headers.set(HeaderConstants.AUTHORIZATION, encodedCredentials);
42 return Promise.resolve(webResource);
43 };
44 return BasicAuthenticationCredentials;
45}());
46export { BasicAuthenticationCredentials };
47//# sourceMappingURL=basicAuthenticationCredentials.js.map
\No newline at end of file