verify-request-sign.js 1.5 KB
Newer Older
1 2 3 4
const crypto = require('crypto')
const { ERROR } = require('../common/error')
const needSignFunctions = new Set([
  'externalRegister',
5 6
  'externalLogin',
  'updateUserInfoByExternal'
7 8 9 10
])

module.exports = function () {
  const methodName = this.getMethodName()
11
  const { source } = this.getUniversalClientInfo()
12

13 14 15
  // 指定接口需要鉴权
  if (!needSignFunctions.has(methodName)) return

16 17 18 19 20 21 22
  // 非 HTTP 方式请求拒绝访问
  if (source !== 'http') {
    throw {
      errCode: ERROR.ILLEGAL_REQUEST
    }
  }

23 24 25
  if (!this.config.requestAuthSecret || typeof this.config.requestAuthSecret !== 'string') {
    throw {
      errCode: ERROR.CONFIG_FIELD_REQUIRED,
26
      errMsgValue: {
27 28 29 30 31
        field: 'requestAuthSecret'
      }
    }
  }

32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
  const timeout = 20 * 1000 // 请求超过20秒不能再请求,防止重放攻击
  const { headers, body: _body } = this.getHttpInfo()
  const { 'uni-id-nonce': nonce, 'uni-id-timestamp': timestamp, 'uni-id-signature': signature } = headers
  const body = JSON.parse(_body).params || {}
  const bodyStr = Object.keys(body)
    .sort()
    .filter(item => typeof body[item] !== 'object')
    .map(item => `${item}=${body[item]}`)
    .join('&')

  if (isNaN(Number(timestamp)) || (Number(timestamp) + timeout) < Date.now()) {
    throw {
      errCode: ERROR.ILLEGAL_REQUEST
    }
  }

  const reSignature = crypto.createHmac('sha256', `${this.config.requestAuthSecret + nonce}`).update(`${timestamp}${bodyStr}`).digest('hex')

  if (signature !== reSignature.toUpperCase()) {
    throw {
      errCode: ERROR.ILLEGAL_REQUEST
    }
  }
}