web_identity_credentials.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. var AWS = require('../core');
  2. var STS = require('../../clients/sts');
  3. /**
  4. * Represents credentials retrieved from STS Web Identity Federation support.
  5. *
  6. * By default this provider gets credentials using the
  7. * {AWS.STS.assumeRoleWithWebIdentity} service operation. This operation
  8. * requires a `RoleArn` containing the ARN of the IAM trust policy for the
  9. * application for which credentials will be given. In addition, the
  10. * `WebIdentityToken` must be set to the token provided by the identity
  11. * provider. See {constructor} for an example on creating a credentials
  12. * object with proper `RoleArn` and `WebIdentityToken` values.
  13. *
  14. * ## Refreshing Credentials from Identity Service
  15. *
  16. * In addition to AWS credentials expiring after a given amount of time, the
  17. * login token from the identity provider will also expire. Once this token
  18. * expires, it will not be usable to refresh AWS credentials, and another
  19. * token will be needed. The SDK does not manage refreshing of the token value,
  20. * but this can be done through a "refresh token" supported by most identity
  21. * providers. Consult the documentation for the identity provider for refreshing
  22. * tokens. Once the refreshed token is acquired, you should make sure to update
  23. * this new token in the credentials object's {params} property. The following
  24. * code will update the WebIdentityToken, assuming you have retrieved an updated
  25. * token from the identity provider:
  26. *
  27. * ```javascript
  28. * AWS.config.credentials.params.WebIdentityToken = updatedToken;
  29. * ```
  30. *
  31. * Future calls to `credentials.refresh()` will now use the new token.
  32. *
  33. * @!attribute params
  34. * @return [map] the map of params passed to
  35. * {AWS.STS.assumeRoleWithWebIdentity}. To update the token, set the
  36. * `params.WebIdentityToken` property.
  37. * @!attribute data
  38. * @return [map] the raw data response from the call to
  39. * {AWS.STS.assumeRoleWithWebIdentity}. Use this if you want to get
  40. * access to other properties from the response.
  41. */
  42. AWS.WebIdentityCredentials = AWS.util.inherit(AWS.Credentials, {
  43. /**
  44. * Creates a new credentials object.
  45. * @param (see AWS.STS.assumeRoleWithWebIdentity)
  46. * @example Creating a new credentials object
  47. * AWS.config.credentials = new AWS.WebIdentityCredentials({
  48. * RoleArn: 'arn:aws:iam::1234567890:role/WebIdentity',
  49. * WebIdentityToken: 'ABCDEFGHIJKLMNOP', // token from identity service
  50. * RoleSessionName: 'web' // optional name, defaults to web-identity
  51. * }, {
  52. * // optionally provide configuration to apply to the underlying AWS.STS service client
  53. * // if configuration is not provided, then configuration will be pulled from AWS.config
  54. *
  55. * // specify timeout options
  56. * httpOptions: {
  57. * timeout: 100
  58. * }
  59. * });
  60. * @see AWS.STS.assumeRoleWithWebIdentity
  61. * @see AWS.Config
  62. */
  63. constructor: function WebIdentityCredentials(params, clientConfig) {
  64. AWS.Credentials.call(this);
  65. this.expired = true;
  66. this.params = params;
  67. this.params.RoleSessionName = this.params.RoleSessionName || 'web-identity';
  68. this.data = null;
  69. this._clientConfig = AWS.util.copy(clientConfig || {});
  70. },
  71. /**
  72. * Refreshes credentials using {AWS.STS.assumeRoleWithWebIdentity}
  73. *
  74. * @callback callback function(err)
  75. * Called when the STS service responds (or fails). When
  76. * this callback is called with no error, it means that the credentials
  77. * information has been loaded into the object (as the `accessKeyId`,
  78. * `secretAccessKey`, and `sessionToken` properties).
  79. * @param err [Error] if an error occurred, this value will be filled
  80. * @see get
  81. */
  82. refresh: function refresh(callback) {
  83. this.coalesceRefresh(callback || AWS.util.fn.callback);
  84. },
  85. /**
  86. * @api private
  87. */
  88. load: function load(callback) {
  89. var self = this;
  90. self.createClients();
  91. self.service.assumeRoleWithWebIdentity(function (err, data) {
  92. self.data = null;
  93. if (!err) {
  94. self.data = data;
  95. self.service.credentialsFrom(data, self);
  96. }
  97. callback(err);
  98. });
  99. },
  100. /**
  101. * @api private
  102. */
  103. createClients: function() {
  104. if (!this.service) {
  105. var stsConfig = AWS.util.merge({}, this._clientConfig);
  106. stsConfig.params = this.params;
  107. this.service = new STS(stsConfig);
  108. }
  109. }
  110. });