|
分享者:Cbinbin,來自原文地址
近期微信更新了第三方平臺,小程序可以授權(quán)第三方,看了一些接口,把所學(xué)的做下記錄。
授權(quán)流程圖(部分接口)
其中應(yīng)該注意的是 授權(quán)碼換取2個token 和 微信登錄 這兩個接口。 注意:1、pre_auth_code 和 auth_code 是不一樣的,pre_auth_code 是預(yù)授權(quán)碼,經(jīng)過用戶進(jìn)入授權(quán)頁掃二維碼后授權(quán),跳轉(zhuǎn)回調(diào)返回給你的才是授權(quán)碼(auth_code)。2、微信登錄url帶的 component_access_token 確實就是 component_access_token,并不是像微信參數(shù)說明里的“小程序授權(quán)的authorizer_access_token”。//本人首先用的是 authorizer_access_token ,結(jié)果在小程序里老是抱錯 "{errcode: 48001, errmsg: 'api unauthorized', ...}" 獲取 component_verify_ticket
Token 和 EncodingAESKey 為上面所填寫的Token和Key。 1. 接收json數(shù)據(jù)
...
require('body-parser-xml')(bodyParser)
server.use(bodyParser.xml({
limit: '2MB',
xmlParseOptions: {
normalize: true,
normalizeTags: true,
explicitArray: false
}
}))
...
2. 驗證簽名
var crypto = require('crypto')
var sha1 = crypto.createHash('sha1')
, dev_msg_signature = sha1.update(
[token, timestamp, nonce, msg_encrypt].sort().join('')
).digest('hex')
if(dev_msg_signature === msg_signature) return 'pass'
3. AES解密
var pkcs7Encoder = {
//對需要加密的明文進(jìn)行填充補(bǔ)位
encode: function(text) {
var blockSize = 32
var textLength = text.length
//計算需要填充的位數(shù)
var amountToPad = blockSize - (textLength % blockSize)
var result = new Buffer(amountToPad)
result.fill(amountToPad)
return Buffer.concat([text, result]) //尾部填充
},
//刪除解密后明文的補(bǔ)位字符
decode: function(text) {
var pad = text[text.length - 1]
if (pad < 1 || pad > 32) {
pad = 0
}
return text.slice(0, text.length - pad)
}
}
module.exports = pkcs7Encoder
var crypto = require('crypto')
, pkcs7Encoder = require('./pkcs7Encoder')
class wxBizMsgCrypto {
constructor(encodingAesKey, appId) {
if(!encodingAesKey || !appId) {
throw new Error('please check arguments')
}
var AESKey = new Buffer(encodingAesKey + '=', 'base64')
if(AESKey.length !== 32) {
throw new Error('encodingAESKey invalid')
}
this.AESKey = AESKey
this.iv = AESKey.slice(0, 16)
this.appId = appId
}
encryptMsg(text) {
// 獲取16B的隨機(jī)字符串
var randomString = crypto.pseudoRandomBytes(16)
, msg = new Buffer(text)
, id = new Buffer(this.appId)
// 獲取4B的內(nèi)容長度的網(wǎng)絡(luò)字節(jié)序
var msgLength = new Buffer(4)
//寫入無符號32位整型,大端對齊
msgLength.writeUInt32BE(msg.length, 0)
var bufMsg = Buffer.concat([randomString, msgLength, msg, id])
// 對明文進(jìn)行補(bǔ)位操作
var encoded = pkcs7Encoder.encode(bufMsg)
var cipher = crypto.createCipheriv('aes-256-cbc', this.AESKey, this.iv);
cipher.setAutoPadding(false)
var cipheredMsg = Buffer.concat([cipher.update(encoded), cipher.final()])
// 返回加密數(shù)據(jù)的base64編碼
return cipheredMsg.toString('base64')
}
decryptMsg(resXml) {
var msg_encrypt = resXml.encrypt
try {
var decipher = crypto.createDecipheriv('aes-256-cbc', this.AESKey, this.iv)
decipher.setAutoPadding(false)
//Buffer.concat() 緩沖區(qū)合并
var deciphered = Buffer.concat([decipher.update(msg_encrypt, 'base64'), decipher.final()])
deciphered = pkcs7Encoder.decode(deciphered)
var content = deciphered.slice(16)
, length = content.slice(0, 4).readUInt32BE(0)
} catch (err) {
throw new Error(err)
}
return {
msgXml: content.slice(4, length + 4).toString(),
appid: content.slice(length + 4).toString()
}
}
}
module.exports = wxBizMsgCrypto
var xml2jsonString = require('xml2js').parseString
xml2jsonString(xml, {async:true}, (err, json)=> {
if(err) return console.log(err)
console.log(json)
})
到這解密算是完成了。 |