|
分享者:Cbinbin,来自原文地址
近期微信更新了第三方平台,小程序可以授权第三方,看了一些接口,把所学的做下记录。
授权流程图(部分接口)
其中应该注意的是 授权码换取2个token 和 微信登录 这两个接口。 注意:1、pre_auth_code 和 auth_code 是不一样的,pre_auth_code 是预授权码,经过用户进入授权页扫二维码后授权,跳转回调返回给你的才是授权码(auth_code)。2、微信登录url带的 component_access_token 确实就是 component_access_token,并不是像微信参数说明里的“小程序授权的authorizer_access_token”。//本人首先用的是 authorizer_access_token ,结果在小程序里老是抱错 "{errcode: 48001, errmsg: 'api unauthorized', ...}" 获取 component_verify_ticket
Token 和 EncodingAESKey 为上面所填写的Token和Key。 1. 接收json数据
...
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 = {
//对需要加密的明文进行填充补位
encode: function(text) {
var blockSize = 32
var textLength = text.length
//计算需要填充的位数
var amountToPad = blockSize - (textLength % blockSize)
var result = new Buffer(amountToPad)
result.fill(amountToPad)
return Buffer.concat([text, result]) //尾部填充
},
//删除解密后明文的补位字符
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的随机字符串
var randomString = crypto.pseudoRandomBytes(16)
, msg = new Buffer(text)
, id = new Buffer(this.appId)
// 获取4B的内容长度的网络字节序
var msgLength = new Buffer(4)
//写入无符号32位整型,大端对齐
msgLength.writeUInt32BE(msg.length, 0)
var bufMsg = Buffer.concat([randomString, msgLength, msg, id])
// 对明文进行补位操作
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()])
// 返回加密数据的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() 缓冲区合并
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)
})
到这解密算是完成了。 |