common.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * Copyright (C) 2020 Tencent Cloud.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. 'use strict';
  17. const crypto = require('crypto');
  18. const { secretId, secretKey, isReport } = require('./config');
  19. /**
  20. * 获取腾讯云API签名方法V3
  21. * @param {string} servicename - 服务名称,例如:ocr、sms等
  22. * @param {string} payload - 接口请求正文
  23. * @return {string[]} 返回API请求接口headers所需要的认证信息
  24. */
  25. function sign(servicename, payload) {
  26. // 配置校验
  27. if (!secretId || !secretKey) {
  28. throw new Error('请检查云函数密钥配置文件!');
  29. }
  30. if (!servicename) {
  31. throw new Error('请填写服务名称');
  32. }
  33. const payloadHash = crypto.createHash('sha256').update(payload).digest('hex');
  34. const requestString = `POST\n/\n\ncontent-type:application/json\nhost:${servicename}.tencentcloudapi.com\n\ncontent-type;host\n${payloadHash}`;
  35. const currentDate = new Date();
  36. const timestamp = `${Math.floor(currentDate.getTime() / 1000)}`;
  37. const dateString = currentDate.toISOString().substr(0, 10);
  38. const requestStringHash = crypto.createHash('sha256').update(requestString).digest('hex');
  39. const stringToSign = `TC3-HMAC-SHA256\n${timestamp}\n${dateString}/${servicename}/tc3_request\n${requestStringHash}`;
  40. const secretDate = crypto.createHmac('sha256', `TC3${secretKey}`).update(dateString).digest();
  41. const secretService = crypto.createHmac('sha256', secretDate).update(servicename).digest();
  42. const secretSigning = crypto.createHmac('sha256', secretService).update('tc3_request').digest();
  43. const signature = crypto.createHmac('sha256', secretSigning).update(stringToSign).digest('hex');
  44. return [
  45. timestamp,
  46. `TC3-HMAC-SHA256 Credential=${secretId}/${dateString}/${servicename}/tc3_request, SignedHeaders=content-type;host, Signature=${signature}`
  47. ];
  48. }
  49. /**
  50. * 插件使用统计,异步处理,忽略结果,不阻塞主流程
  51. * @param {string} module - 模块名称
  52. * @param {object} extraInfo - 附加信息
  53. */
  54. function report(module, extraInfo) {
  55. if (!isReport) {
  56. return;
  57. }
  58. if (!secretId || !secretKey) {
  59. return;
  60. }
  61. uniCloud.callFunction({
  62. name: 'tencentcloud-base-plugin',
  63. data: {
  64. secretId,
  65. secretKey,
  66. module,
  67. extraInfo
  68. }
  69. });
  70. }
  71. module.exports = { sign, report };