keyVaultFactory.js 4.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. Object.defineProperty(exports, "__esModule", { value: true });
  5. const applicationTokenCredentials_1 = require("./applicationTokenCredentials");
  6. const applicationTokenCertificateCredentials_1 = require("./applicationTokenCertificateCredentials");
  7. const deviceTokenCredentials_1 = require("./deviceTokenCredentials");
  8. const msiAppServiceTokenCredentials_1 = require("./msiAppServiceTokenCredentials");
  9. const msiTokenCredentials_1 = require("./msiTokenCredentials");
  10. const msiVmTokenCredentials_1 = require("./msiVmTokenCredentials");
  11. const tokenCredentialsBase_1 = require("./tokenCredentialsBase");
  12. const userTokenCredentials_1 = require("./userTokenCredentials");
  13. const adal_node_1 = require("adal-node");
  14. function createAuthenticator(credentials) {
  15. const convertedCredentials = _convert(credentials);
  16. const authenticator = _createAuthenticatorMapper(convertedCredentials);
  17. return authenticator;
  18. }
  19. exports.createAuthenticator = createAuthenticator;
  20. function _convert(credentials) {
  21. if (credentials instanceof msiAppServiceTokenCredentials_1.MSIAppServiceTokenCredentials) {
  22. return new msiAppServiceTokenCredentials_1.MSIAppServiceTokenCredentials({
  23. msiEndpoint: credentials.msiEndpoint,
  24. msiSecret: credentials.msiSecret,
  25. msiApiVersion: credentials.msiApiVersion,
  26. resource: credentials.resource
  27. });
  28. }
  29. else if (credentials instanceof msiVmTokenCredentials_1.MSIVmTokenCredentials) {
  30. return new msiVmTokenCredentials_1.MSIVmTokenCredentials({
  31. resource: credentials.resource,
  32. msiEndpoint: credentials.msiEndpoint
  33. });
  34. }
  35. else if (credentials instanceof msiTokenCredentials_1.MSITokenCredentials) {
  36. throw new Error("MSI-credentials not one of: MSIVmTokenCredentials, MSIAppServiceTokenCredentials");
  37. }
  38. else {
  39. return credentials;
  40. }
  41. }
  42. function _createAuthenticatorMapper(credentials) {
  43. return (challenge) => new Promise((resolve, reject) => {
  44. // Function to take token Response and format a authorization value
  45. const _formAuthorizationValue = (err, tokenResponse) => {
  46. if (err) {
  47. return reject(err);
  48. }
  49. if (tokenResponse.error) {
  50. return reject(tokenResponse.error);
  51. }
  52. tokenResponse = tokenResponse;
  53. // Calculate the value to be set in the request's Authorization header and resume the call.
  54. const authorizationValue = tokenResponse.tokenType + " " + tokenResponse.accessToken;
  55. return resolve(authorizationValue);
  56. };
  57. // Create a new authentication context.
  58. if (credentials instanceof tokenCredentialsBase_1.TokenCredentialsBase) {
  59. const context = new adal_node_1.AuthenticationContext(challenge.authorization, true, credentials.authContext && credentials.authContext.cache);
  60. if (credentials instanceof applicationTokenCredentials_1.ApplicationTokenCredentials) {
  61. return context.acquireTokenWithClientCredentials(challenge.resource, credentials.clientId, credentials.secret, _formAuthorizationValue);
  62. }
  63. else if (credentials instanceof applicationTokenCertificateCredentials_1.ApplicationTokenCertificateCredentials) {
  64. return context.acquireTokenWithClientCertificate(challenge.resource, credentials.clientId, credentials.certificate, credentials.thumbprint, _formAuthorizationValue);
  65. }
  66. else if (credentials instanceof userTokenCredentials_1.UserTokenCredentials) {
  67. return context.acquireTokenWithUsernamePassword(challenge.resource, credentials.username, credentials.password, credentials.clientId, _formAuthorizationValue);
  68. }
  69. else if (credentials instanceof deviceTokenCredentials_1.DeviceTokenCredentials) {
  70. return context.acquireToken(challenge.resource, credentials.username, credentials.clientId, _formAuthorizationValue);
  71. }
  72. }
  73. else if (credentials instanceof msiTokenCredentials_1.MSITokenCredentials) {
  74. return credentials.getToken();
  75. }
  76. else {
  77. return reject(new Error("credentials must be one of: ApplicationTokenCredentials, UserTokenCredentials, " +
  78. "DeviceTokenCredentials, MSITokenCredentials"));
  79. }
  80. });
  81. }
  82. //# sourceMappingURL=keyVaultFactory.js.map