项目作者: xixilive

项目描述 :
Express middleware to decrypt wechat userInfo data for weapp(微信小程序) login scenario
高级语言: JavaScript
项目地址: git://github.com/xixilive/express-weapp-auth.git
创建时间: 2017-01-23T09:20:58Z
项目社区:https://github.com/xixilive/express-weapp-auth

开源协议:MIT License

下载


Express-weapp-auth

Build Status

Express middleware to decrypt wechat userInfo data for weapp login scenario.

Installation

  1. # via Github
  2. npm install xixilive/express-weapp-auth --save
  3. # via npm
  4. npm install express-weapp-auth --save

Usage

  1. // basic example
  2. import {middleware} from 'express-weapp-auth'
  3. const app = require('express')()
  4. app.post(
  5. '/session/:code',
  6. middleware('appId', 'appSecret'),
  7. (req, res, next) => {
  8. const {openId, sessionKey, userInfo} = req.weappAuth
  9. //your logic here
  10. }
  11. )
  12. // advance example
  13. app.use(
  14. '/weapp/session/',
  15. middleware('appId', 'appSecret', (req) => {
  16. return req.body
  17. }, {dataKey: 'customDataKey'}),
  18. (req, res, next) => {
  19. const {openId, sessionKey, userInfo} = req.customDataKey
  20. //your logic here
  21. }
  22. )

Middleware

  1. // all arguments
  2. middleware('appId', 'appSecret' [, paramsResolver, options])
  3. // without optional arguments
  4. middleware('appId', 'appSecret')
  5. // without options argument
  6. middleware('appId', 'appSecret' paramsResolver)
  7. // without paramsResolver argument
  8. middleware('appId', 'appSecret' options)

Arguments

  • appId: required, weapp app ID

  • appSecret: required, weapp app secret

  • paramsResolver: optional, a function(req){} to resolve auth-params for request object

  • options: optional, {dataKey: 'the key assign to req object to store decrypted data'}

ParamsResolver(req)

It will use a built-in default resolver to resolve params for request if there has no function passed to middleware function. and the default function resolves params in a certain priority:

  • req.body with the highest priority

  • req.query with middle priority

  • req.params with the lowest priority

And it expects the resolver function to return an object value with following structure:

  1. {
  2. code: 'login code',
  3. rawData: 'rawData',
  4. signature: 'signature for rawData',
  5. encryptedData: 'encrypted userInfo',
  6. iv: 'cipher/decipher vector'
  7. }

For more details about this, please visit 微信小程序 API