Skip to content

client_secret加密处理说明

📌 请注意:在非HTTPs环境下,为保证client_secret安全传输,需要进行加密处理,处理方式如下

  1. client_idclient_secret 和 当前 UTC 时间(例:Wed, 23 Jan 2013 06:43:08 GMT) 采用 “:”拼接,在对拼接后字符串进行 sha256 哈希;
  2. 将以上哈希值与当前 UTC 时间采用;拼接,再加上"SEC "前缀标识,得到最终的 client_secret
  3. 哈希 client_secret 具有 10s 有效期

伪代码示例:

json
client_secret="SEC sha256($client_id + : + $client_secret + : + $utc_time);$utc_time"

golang代码实现:

go
func makeSECSecret(clientId, clientSecret string, t time.Time) string {
    utc := t.Format(http.TimeFormat)
    return fmt.Sprintf("SEC %x;%s",
        sha256.Sum256([]byte(fmt.Sprintf("%s:%s:%s", clientId, clientSecret, utc))), utc)
}

Postman脚本示例:

javascript
let CryptoJS = require('crypto-js') 
let use_sec = pm.request.url.query.get("sec");
let body = pm.request.body;
if (use_sec === "1" || use_sec === "true") {
    let gmt_date = new Date().toUTCString();
    let id = body.urlencoded.get("client_id");
    let secret = body.urlencoded.get("client_secret");
    let data = `${id}:${secret}:${gmt_date}`
    data = CryptoJS.SHA256(data).toString()
    data = `SEC ${data};${gmt_date}`
    body.urlencoded.each(function(item){
        if (item.key == "client_secret") {
            item.value = data;
        }
    })
}