-
Notifications
You must be signed in to change notification settings - Fork 51
/
strategy.js
159 lines (129 loc) · 4.64 KB
/
strategy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
'use strict';
/*
* passport-wechat
* http://www.liangyali.com
*
* Copyright (c) 2014 liangyali
* Licensed under the MIT license.
*/
var util = require('util');
var passport = require('passport-strategy');
var OAuth = require('wechat-oauth');
var debug = require('debug')('passport-wechat');
var extend = require("xtend");
function WechatStrategy(options, verify) {
options = options || {};
if (!verify) {
throw new TypeError('WeChatStrategy required a verify callback');
}
if (typeof verify !== 'function') {
throw new TypeError('_verify must be function');
}
if (!options.appID) {
throw new TypeError('WechatStrategy requires a appID option');
}
if (!options.appSecret) {
throw new TypeError('WechatStrategy requires a appSecret option');
}
passport.Strategy.call(this, options, verify);
this.name = options.name || 'wechat';
this._client = options.client || 'wechat';
this._verify = verify;
this._oauth = new OAuth(options.appID, options.appSecret, options.getToken, options.saveToken);
this._callbackURL = options.callbackURL;
this._lang = options.lang || 'en';
this._state = options.state;
this._scope = options.scope || 'snsapi_userinfo';
this._passReqToCallback = options.passReqToCallback;
}
/**
* Inherit from 'passort.Strategy'
*/
util.inherits(WechatStrategy, passport.Strategy);
WechatStrategy.prototype.authenticate = function(req, options) {
if (!req._passport) {
return this.error(new Error('passport.initialize() middleware not in use'));
}
var self = this;
options = options || {};
// 获取code,并校验相关参数的合法性
// No code only state --> User has rejected send details. (Fail authentication request).
if (req.query && req.query.state && !req.query.code) {
return self.fail(401);
}
// Documentation states that if user rejects userinfo only state will be sent without code
// In reality code equals "authdeny". Handle this case like the case above. (Fail authentication request).
if (req.query && req.query.code === 'authdeny') {
return self.fail(401);
}
// 获取code授权成功
if (req.query && req.query.code) {
var code = req.query.code;
debug('wechat callback -> \n %s', req.url);
self._oauth.getAccessToken(code, function(err, response) {
// 校验完成信息
function verified(err, user, info) {
if (err) {
return self.error(err);
}
if (!user) {
return self.fail(info);
}
self.success(user, info);
}
if (err) {
return self.error(err);
}
debug('fetch accessToken -> \n %s', JSON.stringify(response.data, null, ' '));
var params = response.data;
if (~params.scope.indexOf('snsapi_base')) {
var profile = {
openid: params['openid'],
unionid: params['unionid']
};
try {
if (self._passReqToCallback) {
self._verify(req, params['access_token'], params['refresh_token'], profile, params['expires_in'], verified);
} else {
self._verify(params['access_token'], params['refresh_token'], profile, params['expires_in'], verified);
}
} catch (ex) {
return self.error(ex);
}
} else {
self._oauth.getUser({
openid: params['openid'],
lang: self._lang
}, function(err, profile) {
if (err) {
debug('fetch userinfo by openid error ->', err.message);
return self.error(err);
}
debug('fetch userinfo -> \n %s', JSON.stringify(profile, null, ' '));
// merge params
params = extend(params, profile);
try {
if (self._passReqToCallback) {
self._verify(req, params['access_token'], params['refresh_token'], profile, params['expires_in'], verified);
} else {
self._verify(params['access_token'], params['refresh_token'], profile, params['expires_in'], verified);
}
} catch (ex) {
return self.error(ex);
}
});
}
});
} else {
var defaultURL = req.protocol + '://' + req.get('Host') + req.originalUrl;
// 兼容web微信登陆和公众账号的微信登陆
var state = options.state || self._state,
callbackURL = options.callbackURL || self._callbackURL || defaultURL,
scope = options.scope || self._scope;
var methodName = (this._client === 'wechat') ? 'getAuthorizeURL' : 'getAuthorizeURLForWebsite';
var location = self._oauth[methodName](callbackURL, state, scope);
debug('redirect -> \n%s', location);
self.redirect(location, 302);
}
};
module.exports = WechatStrategy;