basicAuthenticationCredentials.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // Copyright (c) Microsoft Corporation. All rights reserved.
  2. // Licensed under the MIT License. See License.txt in the project root for license information.
  3. import { HttpHeaders } from "../httpHeaders";
  4. import * as base64 from "../util/base64";
  5. import { Constants } from "../util/constants";
  6. var HeaderConstants = Constants.HeaderConstants;
  7. var DEFAULT_AUTHORIZATION_SCHEME = "Basic";
  8. var 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 {WebResource} webResource The WebResource to be signed.
  34. * @returns {Promise<WebResource>} 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. }());
  46. export { BasicAuthenticationCredentials };
  47. //# sourceMappingURL=basicAuthenticationCredentials.js.map