setSTSToken.ts 716 B

1234567891011121314151617181920212223
  1. import { formatObjKey } from './formatObjKey';
  2. export async function setSTSToken(this: any) {
  3. if (!this.options) this.options = {};
  4. let credentials = await this.options.refreshSTSToken();
  5. credentials = formatObjKey(credentials, 'firstLowerCase');
  6. if (credentials.securityToken) {
  7. credentials.stsToken = credentials.securityToken;
  8. }
  9. checkCredentials(credentials);
  10. Object.assign(this.options, credentials);
  11. }
  12. function checkCredentials(obj) {
  13. const stsTokenKey = ['accessKeySecret', 'accessKeyId', 'stsToken'];
  14. const objKeys = Object.keys(obj);
  15. stsTokenKey.forEach(_ => {
  16. if (!objKeys.find(key => key === _)) {
  17. throw Error(`refreshSTSToken must return contains ${_}`);
  18. }
  19. });
  20. }