-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
221 lines (188 loc) · 4.87 KB
/
index.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
/* jshint node: true */
/*
* expose
*/
module.exports = cobbler;
// backup originals
cobbler.__ = {};
/*
* cobbler
*
* @param {Stategy|String|Constructor|Prototype} strategy
* @param {Object} profile
* @param {Object} opts
* @api public
*/
function cobbler(strategy, profile, opts) {
if ('undefined' === typeof profile || null === profile) {
throw new Error('`profile` must be an object or false');
}
opts = opts || {};
var passauth = !!profile;
var _proto = proto(strategy);
var ovr = override(_proto);
if ('session' === strategy) {
ovr
.method('authenticate', (function(user) {
return function(req, options) {
// user should be the object that is sent to passport.deserializeUser method
// ex: May be a user.id for a user lookup, or can be the entire user object itself
req._passport.session.user = user;
cobbler.__.authenticate.call(this, req, options); // go about as original
};
})(profile));
} else {
ovr
.method('userProfile', userProfile(profile))
.method('authenticate', authenticate(passauth, opts));
}
this.restore = restore.bind(this, _proto);
return this;
}
/*
* backup original methods
*
* @param {Prototype} proto
* @return {Object}
* @api private
*/
function backup(proto) {
var method = function(name) {
cobbler.__[name] = proto[name];
return this;
};
return {
method: method
};
}
/*
* override method
*
* @param {Prototype}
* @return {Object}
* @api private
*/
function override(proto) {
var method = function(name, fn) {
if (!(name in proto)) {
throw new Error('No method `'+name+'` to override');
}
backup(proto).method(name);
proto[name] = fn;
return this;
};
return {
method: method
};
}
/*
* return the prototype
*
* @param {String|Strategy|Prototype} strategy
* @return {Prototype}
* @api private
*/
function proto(strategy) {
// Session
if ('session' === strategy) {
return require('passport').strategies.SessionStrategy.prototype;
}
// Strategy
if ('string' === typeof strategy) { // npm name
strategy = require(strategy).Strategy;
} else if ('Strategy' in strategy) { // exports
strategy = strategy.Strategy;
}
// is prototype or get the prototype
return ('userProfile' in strategy) ? strategy : strategy.prototype;
}
/*
* restore the original methods
*
* @param {Prototype} proto
* @api private
*/
function restore(proto) {
var methods = Object.keys(cobbler.__);
var i = 0;
var len = methods.length;
for(; i<len; i++) {
var name = methods[i];
proto[name] = cobbler.__[name];
delete cobbler.__[name];
}
}
/*
* Override `authenticate`
* https://github.com/jaredhanson/passport-oauth2/blob/master/lib/strategy.js#L118
*
* We call the original method to allow it to go through the normal process
* while mocking the OAuth2 object `this._oauth2`
*
* @param {Boolean} passauth
* @param {Object} opts
* @return {Function}
* @api private
*/
function authenticate(passauth, opts) {
/*
* mock oauth2 object
*
* - `getAuthorizeUrl` returns the authorization url
* eg. https://github.com/login/oauth/authorize
*
* We want to avoid actually hitting the OAuth endpoint and redirect directly to
* the "callback url" w/ any query params corresponding to the status of the req
*
* - `getOAuthAccessToken` retrieves the accessToken used to grab the user profile
*
* This is one of the network calls out to the internet.
*/
var mockoauth2 = {
getAuthorizeUrl: function(params) {
return params.redirect_uri+queryp(passauth);
},
getOAuthAccessToken: function(code, params, callback) {
callback(); // TODO arity and ability to define values for aguments
}
};
return function(req, options) {
options.callbackURL = opts.callbackURL || options.callbackURL;
/*
* you can overwrite the delegator to the inherited `userProfile` method
* however, `userProfile` is the method calling the actual `oauth2.get()`
*
* this._loadUserProfile = function(accessToken, done) {
* done(null, user);
* };
*/
// TODO do we need to back this up? This is instance based
this._oauth2 = mockoauth2; // mock the oauth2 object
cobbler.__.authenticate.call(this, req, options); // go about as original
};
}
/*
* Override `userProfile`
*
* Unlike the `authenticate` function, this is normally defined in
* each inheritted strategy. This is other netowkr call out to the internet.
*
* @param {Object} profile
* @return {Function}
* @api private
*/
function userProfile(profile) {
return function(accessToken, done) {
done(null, profile);
};
}
/*
* add callback url query params
*
* @param {Boolean} passauth
* @return {String}
* @api private
*/
function queryp(passauth) {
return passauth ? '?code=12345' : '?error=access_denied';
}