calculatePostSignature.js 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. const { policy2Str } = require('../utils/policy2Str');
  2. const signHelper = require('../signUtils');
  3. const { isObject } = require('../utils/isObject');
  4. const proto = exports;
  5. /**
  6. * @param {Object or JSON} policy specifies the validity of the fields in the request.
  7. * @return {Object} params
  8. * {String} params.OSSAccessKeyId
  9. * {String} params.Signature
  10. * {String} params.policy JSON text encoded with UTF-8 and Base64.
  11. */
  12. proto.calculatePostSignature = function calculatePostSignature(policy) {
  13. if (!isObject(policy) && typeof policy !== 'string') {
  14. throw new Error('policy must be JSON string or Object');
  15. }
  16. if (!isObject(policy)) {
  17. try {
  18. JSON.stringify(JSON.parse(policy));
  19. } catch (error) {
  20. throw new Error('policy must be JSON string or Object');
  21. }
  22. }
  23. policy = Buffer.from(policy2Str(policy), 'utf8').toString('base64');
  24. const Signature = signHelper.computeSignature(this.options.accessKeySecret, policy);
  25. const query = {
  26. OSSAccessKeyId: this.options.accessKeyId,
  27. Signature,
  28. policy
  29. };
  30. return query;
  31. };