msiVmTokenCredentials.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 msiTokenCredentials_1 = require("./msiTokenCredentials");
  14. const ms_rest_js_1 = require("@azure/ms-rest-js");
  15. /**
  16. * @class MSIVmTokenCredentials
  17. */
  18. class MSIVmTokenCredentials extends msiTokenCredentials_1.MSITokenCredentials {
  19. constructor(options) {
  20. if (!options)
  21. options = {};
  22. super(options);
  23. if (!options.msiEndpoint) {
  24. options.msiEndpoint = "http://169.254.169.254/metadata/identity/oauth2/token";
  25. }
  26. else if (typeof options.msiEndpoint !== "string") {
  27. throw new Error("msiEndpoint must be a string.");
  28. }
  29. const urlBuilder = ms_rest_js_1.URLBuilder.parse(options.msiEndpoint);
  30. if (!urlBuilder.getScheme()) {
  31. options.msiEndpoint = `http://${options.msiEndpoint}`;
  32. }
  33. if (!options.apiVersion) {
  34. options.apiVersion = "2018-02-01";
  35. }
  36. else if (typeof options.apiVersion !== "string") {
  37. throw new Error("apiVersion must be a string.");
  38. }
  39. if (!options.httpMethod) {
  40. options.httpMethod = "GET";
  41. }
  42. this.apiVersion = options.apiVersion;
  43. this.msiEndpoint = options.msiEndpoint;
  44. this.httpMethod = options.httpMethod;
  45. this.objectId = options.objectId;
  46. this.clientId = options.clientId;
  47. this.identityId = options.identityId;
  48. }
  49. /**
  50. * Prepares and sends a POST request to a service endpoint hosted on the Azure VM, which responds with the access token.
  51. * @return {Promise<MSITokenResponse>} Promise with the tokenResponse (tokenType and accessToken are the two important properties).
  52. */
  53. getToken() {
  54. return __awaiter(this, void 0, void 0, function* () {
  55. const reqOptions = this.prepareRequestOptions();
  56. let opRes;
  57. let result;
  58. opRes = yield this._httpClient.sendRequest(reqOptions);
  59. result = this.parseTokenResponse(opRes.bodyAsText);
  60. if (!result.tokenType) {
  61. throw new Error(`Invalid token response, did not find tokenType. Response body is: ${opRes.bodyAsText}`);
  62. }
  63. else if (!result.accessToken) {
  64. throw new Error(`Invalid token response, did not find accessToken. Response body is: ${opRes.bodyAsText}`);
  65. }
  66. return result;
  67. });
  68. }
  69. prepareRequestOptions() {
  70. const reqOptions = {
  71. url: this.msiEndpoint,
  72. headers: {
  73. "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
  74. "Metadata": "true"
  75. },
  76. method: this.httpMethod,
  77. queryParameters: {
  78. "api-version": this.apiVersion,
  79. "resource": this.resource,
  80. "object_id": this.objectId,
  81. "client_id": this.clientId,
  82. "mi_res_id": this.identityId
  83. }
  84. };
  85. const webResource = new ms_rest_js_1.WebResource();
  86. return webResource.prepare(reqOptions);
  87. }
  88. }
  89. exports.MSIVmTokenCredentials = MSIVmTokenCredentials;
  90. //# sourceMappingURL=msiVmTokenCredentials.js.map