tokenCredentials.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  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 { Constants } from "../util/constants";
  5. var HeaderConstants = Constants.HeaderConstants;
  6. var DEFAULT_AUTHORIZATION_SCHEME = "Bearer";
  7. /**
  8. * A credentials object that uses a token string and a authorzation scheme to authenticate.
  9. */
  10. var 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 {WebResource} webResource The WebResource to be signed.
  31. * @return {Promise<WebResource>} 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. }());
  41. export { TokenCredentials };
  42. //# sourceMappingURL=tokenCredentials.js.map