token.d.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import {AWSError} from './error';
  2. /**
  3. * Represents AWS token object, which contains {token}, and optional
  4. * {expireTime}.
  5. * Creating a `Token` object allows you to pass around your
  6. * token to configuration and service objects.
  7. *
  8. * Note that this class typically does not need to be constructed manually,
  9. * as the {AWS.Config} and {AWS.Service} classes both accept simple
  10. * options hashes with the two keys. The token from this object will be used
  11. * automatically in operations which require them.
  12. *
  13. * ## Expiring and Refreshing Token
  14. *
  15. * Occasionally token can expire in the middle of a long-running
  16. * application. In this case, the SDK will automatically attempt to
  17. * refresh the token from the storage location if the Token
  18. * class implements the {refresh} method.
  19. *
  20. * If you are implementing a token storage location, you
  21. * will want to create a subclass of the `Token` class and
  22. * override the {refresh} method. This method allows token to be
  23. * retrieved from the backing store, be it a file system, database, or
  24. * some network storage. The method should reset the token attributes
  25. * on the object.
  26. */
  27. export class Token {
  28. /**
  29. * Creates a Token object with a given set of token information as an options hash.
  30. *
  31. * @param {object} options - An option hash containing a set of token information.
  32. */
  33. constructor(options: TokenOptions);
  34. /**
  35. * Gets the existing token, refreshing it if it's are not yet loaded or have expired.
  36. * Users should call this method before using refresh(), as this will not attempt to reload
  37. * tokeb when they are already loaded into the object.
  38. *
  39. * @param {get} callback - When called with no error, the token information has been loaded into the object.
  40. */
  41. get(callback: (err?: AWSError) => void): void;
  42. /**
  43. * Gets the existing token, refreshing ot if necessary, and returns
  44. * a promise that will be fulfilled immediately (if no refresh is necessary)
  45. * or when the refresh has completed.
  46. */
  47. getPromise(): Promise<void>;
  48. /**
  49. * Returns whether the token object should call refresh()
  50. */
  51. needsRefresh(): boolean;
  52. /**
  53. * Refreshes the token.
  54. * Users should call get() before attempting to forcibly refresh token.
  55. *
  56. * @param {function} callback - When called with no error, the token information has been loaded into the object.
  57. */
  58. refresh(callback: (err?: AWSError) => void): void;
  59. /**
  60. * Invokes a token refresh and returns a promise that will be fulfilled
  61. * when the refresh has completed or rejected when the refresh has failed.
  62. * Users should call get() before attempting to forcibly refresh token.
  63. */
  64. refreshPromise(): Promise<void>;
  65. /**
  66. * The literal token string.
  67. */
  68. token: string;
  69. /**
  70. * Whether the token has expired and require a refresh.
  71. * Used in conjunction with expireTime.
  72. */
  73. expired: boolean;
  74. /**
  75. * Time when token should be considered expired.
  76. * Used in conjunction with expired.
  77. */
  78. expireTime: Date;
  79. static expiryWindow: number;
  80. }
  81. export interface TokenOptions {
  82. /**
  83. * The literal token string.
  84. */
  85. token: string
  86. /**
  87. * The time at which the token expires.
  88. */
  89. expireTime?: Date
  90. }