applicationTokenCredentials.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. "use strict";
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. // Licensed under the MIT License. See License.txt in the project root for license information.
  4. var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
  5. return new (P || (P = Promise))(function (resolve, reject) {
  6. function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
  7. function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
  8. function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
  9. step((generator = generator.apply(thisArg, _arguments || [])).next());
  10. });
  11. };
  12. Object.defineProperty(exports, "__esModule", { value: true });
  13. const applicationTokenCredentialsBase_1 = require("./applicationTokenCredentialsBase");
  14. const authConstants_1 = require("../util/authConstants");
  15. class ApplicationTokenCredentials extends applicationTokenCredentialsBase_1.ApplicationTokenCredentialsBase {
  16. /**
  17. * Creates a new ApplicationTokenCredentials object.
  18. * See {@link https://azure.microsoft.com/en-us/documentation/articles/active-directory-devquickstarts-dotnet/ Active Directory Quickstart for .Net}
  19. * for detailed instructions on creating an Azure Active Directory application.
  20. * @constructor
  21. * @param {string} clientId The active directory application client id.
  22. * @param {string} domain The domain or tenant id containing this application.
  23. * @param {string} secret The authentication secret for the application.
  24. * @param {string} [tokenAudience] The audience for which the token is requested. Valid values are 'graph', 'batch', or any other resource like 'https://vault.azure.net/'.
  25. * If tokenAudience is 'graph' then domain should also be provided and its value should not be the default 'common' tenant. It must be a string (preferrably in a guid format).
  26. * @param {Environment} [environment] The azure environment to authenticate with.
  27. * @param {object} [tokenCache] The token cache. Default value is the MemoryCache object from adal.
  28. */
  29. constructor(clientId, domain, secret, tokenAudience, environment, tokenCache) {
  30. if (!secret || typeof secret.valueOf() !== "string") {
  31. throw new Error("secret must be a non empty string.");
  32. }
  33. super(clientId, domain, tokenAudience, environment, tokenCache);
  34. this.secret = secret;
  35. }
  36. /**
  37. * Tries to get the token from cache initially. If that is unsuccessfull then it tries to get the token from ADAL.
  38. * @returns {Promise<TokenResponse>} A promise that resolves to TokenResponse and rejects with an Error.
  39. */
  40. getToken() {
  41. return __awaiter(this, void 0, void 0, function* () {
  42. try {
  43. const tokenResponse = yield this.getTokenFromCache();
  44. return tokenResponse;
  45. }
  46. catch (error) {
  47. if (error.message &&
  48. error.message.startsWith(authConstants_1.AuthConstants.SDK_INTERNAL_ERROR)) {
  49. return Promise.reject(error);
  50. }
  51. const resource = this.getActiveDirectoryResourceId();
  52. return new Promise((resolve, reject) => {
  53. this.authContext.acquireTokenWithClientCredentials(resource, this.clientId, this.secret, (error, tokenResponse) => {
  54. if (error) {
  55. return reject(error);
  56. }
  57. if (tokenResponse.error || tokenResponse.errorDescription) {
  58. return reject(tokenResponse);
  59. }
  60. return resolve(tokenResponse);
  61. });
  62. });
  63. }
  64. });
  65. }
  66. }
  67. exports.ApplicationTokenCredentials = ApplicationTokenCredentials;
  68. //# sourceMappingURL=applicationTokenCredentials.js.map