saml_credentials.js 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. var AWS = require('../core');
  2. var STS = require('../../clients/sts');
  3. /**
  4. * Represents credentials retrieved from STS SAML support.
  5. *
  6. * By default this provider gets credentials using the
  7. * {AWS.STS.assumeRoleWithSAML} 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, as well as a `PrincipalArn`
  10. * representing the ARN for the SAML identity provider. In addition, the
  11. * `SAMLAssertion` must be set to the token provided by the identity
  12. * provider. See {constructor} for an example on creating a credentials
  13. * object with proper `RoleArn`, `PrincipalArn`, and `SAMLAssertion` values.
  14. *
  15. * ## Refreshing Credentials from Identity Service
  16. *
  17. * In addition to AWS credentials expiring after a given amount of time, the
  18. * login token from the identity provider will also expire. Once this token
  19. * expires, it will not be usable to refresh AWS credentials, and another
  20. * token will be needed. The SDK does not manage refreshing of the token value,
  21. * but this can be done through a "refresh token" supported by most identity
  22. * providers. Consult the documentation for the identity provider for refreshing
  23. * tokens. Once the refreshed token is acquired, you should make sure to update
  24. * this new token in the credentials object's {params} property. The following
  25. * code will update the SAMLAssertion, assuming you have retrieved an updated
  26. * token from the identity provider:
  27. *
  28. * ```javascript
  29. * AWS.config.credentials.params.SAMLAssertion = updatedToken;
  30. * ```
  31. *
  32. * Future calls to `credentials.refresh()` will now use the new token.
  33. *
  34. * @!attribute params
  35. * @return [map] the map of params passed to
  36. * {AWS.STS.assumeRoleWithSAML}. To update the token, set the
  37. * `params.SAMLAssertion` property.
  38. */
  39. AWS.SAMLCredentials = AWS.util.inherit(AWS.Credentials, {
  40. /**
  41. * Creates a new credentials object.
  42. * @param (see AWS.STS.assumeRoleWithSAML)
  43. * @example Creating a new credentials object
  44. * AWS.config.credentials = new AWS.SAMLCredentials({
  45. * RoleArn: 'arn:aws:iam::1234567890:role/SAMLRole',
  46. * PrincipalArn: 'arn:aws:iam::1234567890:role/SAMLPrincipal',
  47. * SAMLAssertion: 'base64-token', // base64-encoded token from IdP
  48. * });
  49. * @see AWS.STS.assumeRoleWithSAML
  50. */
  51. constructor: function SAMLCredentials(params) {
  52. AWS.Credentials.call(this);
  53. this.expired = true;
  54. this.params = params;
  55. },
  56. /**
  57. * Refreshes credentials using {AWS.STS.assumeRoleWithSAML}
  58. *
  59. * @callback callback function(err)
  60. * Called when the STS service responds (or fails). When
  61. * this callback is called with no error, it means that the credentials
  62. * information has been loaded into the object (as the `accessKeyId`,
  63. * `secretAccessKey`, and `sessionToken` properties).
  64. * @param err [Error] if an error occurred, this value will be filled
  65. * @see get
  66. */
  67. refresh: function refresh(callback) {
  68. this.coalesceRefresh(callback || AWS.util.fn.callback);
  69. },
  70. /**
  71. * @api private
  72. */
  73. load: function load(callback) {
  74. var self = this;
  75. self.createClients();
  76. self.service.assumeRoleWithSAML(function (err, data) {
  77. if (!err) {
  78. self.service.credentialsFrom(data, self);
  79. }
  80. callback(err);
  81. });
  82. },
  83. /**
  84. * @api private
  85. */
  86. createClients: function() {
  87. this.service = this.service || new STS({params: this.params});
  88. }
  89. });