setSTSToken.ts 997 B

1234567891011121314151617181920212223242526272829303132
  1. import { formatObjKey } from './formatObjKey';
  2. export async function setSTSToken(this: any) {
  3. if (!this.options) this.options = {};
  4. const now = new Date();
  5. if (this.stsTokenFreshTime) {
  6. if (+now - this.stsTokenFreshTime >= this.options.refreshSTSTokenInterval) {
  7. this.stsTokenFreshTime = now;
  8. let credentials = await this.options.refreshSTSToken();
  9. credentials = formatObjKey(credentials, 'firstLowerCase');
  10. if (credentials.securityToken) {
  11. credentials.stsToken = credentials.securityToken;
  12. }
  13. checkCredentials(credentials);
  14. Object.assign(this.options, credentials);
  15. }
  16. } else {
  17. this.stsTokenFreshTime = now;
  18. }
  19. return null;
  20. }
  21. export function checkCredentials(obj) {
  22. const stsTokenKey = ['accessKeySecret', 'accessKeyId', 'stsToken'];
  23. const objKeys = Object.keys(obj);
  24. stsTokenKey.forEach(_ => {
  25. if (!objKeys.find(key => key === _)) {
  26. throw Error(`refreshSTSToken must return contains ${_}`);
  27. }
  28. });
  29. }