JSPM

passport-wechat-enterprise

0.0.25
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 110
  • Score
    100M100P100Q84289F
  • License MIT

wechat enterprise oauth

Package Exports

  • passport-wechat-enterprise

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (passport-wechat-enterprise) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

wechat-enterprise-oauth

This module is for wechat enterprise OAuth.

How to use: provider.json

{
  "wechat": {
    "provider": "wechat",
    "module": "wechat-enterprise-oauth",
    "callbackURL": "/auth/wechat/callback",
    "authPath": "/auth/wechat",
    "successRedirect": "/auth/wechat/account",
    "failureRedirect": "/auth/wechat/login",
    "scope": ["snsapi_base"],
    "corpId": "wxc0a4ff55beded3bc",
    "corpSecret": "y6n1zmiA096wsdffsF7heLeLmlUBA2P6dkjAZt1Lqf3VZFX_"
  }
}

for auth path:

router.get('/auth/wechat', passport.authenticate('wechat', {getCachedToken: getCachedAccessToken, saveToken: storeAccessToken}));

NOTICE: {getCachedToken: getCachedAccessToken, saveToken: storeAccessToken} since wechat has limitation for access_token request, so you need to cache the access token, and save the token when get a new one. If you don't want to use cached token, or save token, just ignore this, it will request an access token every time.

var AccessToken = require('wechat-enterprise-oauth').AccessToken;
var getCachedAccessToken = function(callback) {
    App.findById('12345', function(err, app){
      if (err) return callback(err);
        if (app.apiToken && app.apiToken.expires < new Date()) {
          return callback();
        }
        return callback(null, new AccessToken(app.apiToken.accessToken, app.apiToken.expires));
    });
  };

  var storeAccessToken = function (token, callback) {
        token.expires = moment().add(7100, 'seconds').toDate();
        App.updateAll({type: 'enterprise'}, { apiToken: token }, callback);
  };

AcessToken construct: AccessToken (accessToken: string, expirationTime: Date) AccessToken functions:

isExpired(): Boolean
getToken(): String

for callback path:

router.get('/auth/wechat/callback', passport.authenticate('wechat'));

callback path is which set in the provider.json file, and Loopback if you already set the callback URL in the providers, you can ignore this, loopback-passport-compoment will hook it this path by default.

If auth succeeded, depends on if the user has followed the enterprise account results will be different: Followed:

{
   "UserId":"USERID",
   "DeviceId":"DEVICEID"
}

Not followed

{
   "OpenId":"OPENID",
   "DeviceId":"DEVICEID"
}

This result will be set as user, you should write your serialize and deserialize for the user if necessary.