/
index.js
359 lines (314 loc) · 10.2 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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
'use strict';
var debug = require('debug')('koa-userauth');
var path = require('path');
var is = require('is-type-of');
var copy = require('copy-to');
var urlparse = require('url').parse;
var route = require('path-match')({
end: false,
strict: false,
sensitive: false
});
var defaultOptions = {
userField: 'user',
rootPath: '/',
loginPath: '/login',
logoutPath: '/logout'
};
/**
* User auth middleware.
*
* @param {String|Regex|Function(pathname, ctx)} match, detect which url need to check user auth.
* @param {Object} options
* - {Function(url, rootPath, ctx)} loginURLFormatter, format the login url.
* - {String} [rootPath], custom app url root path, default is '/'.
* - {String} [loginPath], default is '/login'.
* - {String} [loginCallbackPath], default is `options.loginPath + '/callback'`.
* - {String} [logoutPath], default is '/logout'.
* - {String} [userField], logined user field name on `this.session`, default is 'user', `this.session.user`.
* - {Function* (ctx)} getUser, get user function, must get user info with `req`.
* - {Function* (ctx, user)} [loginCallback], you can handle user login logic here,return [user, redirectUrl]
* - {Function(ctx)} [loginCheck], return true meaning logined. default is `true`.
* - {Function* (ctx, user)} [logoutCallback], you can handle user logout logic here.return redirectUrl
* @return {Function* (next)} userauth middleware
* @public
*/
module.exports = function (options) {
// userauth(match, options)
if (arguments.length === 2) {
options = arguments[1];
options.match = arguments[0];
}
copy(defaultOptions).to(options);
options.loginCallbackPath = options.loginCallbackPath
|| options.loginPath + '/callback';
if (options.rootPath !== '/') {
if (process.platform === 'win32') {
// rtrim last /, '/foo/' => '/foo'
var rootPath = options.rootPath.replace(/\/+$/, '');
options.loginPath = rootPath + options.loginPath;
options.logoutPath = rootPath + options.logoutPath;
options.loginCallbackPath = rootPath + options.loginCallbackPath;
} else {
options.loginPath = path.join(options.rootPath, options.loginPath);
options.logoutPath = path.join(options.rootPath, options.logoutPath);
options.loginCallbackPath = path.join(options.rootPath, options.loginCallbackPath);
}
}
// all the typos. T_T
options.loginURLFormatter = options.loginURLFormatter || options.loginURLFormater || options.loginURLForamter;
options.getUser = options.getUser;
options.redirectHandler = options.redirectHandler || defaultRedirectHandler;
var match = options.match;
var ignore = options.ignore;
// need login checker
var needLogin;
if (is.string(match)) {
needLogin = route(match);
} else if (is.regExp(match)) {
needLogin = function (path) {
return match.test(path);
};
} else if (is.function(match)) {
needLogin = match;
}
if (!is.function(needLogin)) {
if (is.string(ignore)) {
var pathMatch = route(ignore);
needLogin = function (path) {
return !pathMatch(path);
};
} else if (is.regExp(ignore)) {
needLogin = function (path) {
return !(path && path.match(ignore));
};
} else if (is.function(ignore)) {
needLogin = function (path, context) {
return !ignore(path, context);
};
} else {
// ignore all
needLogin = function () {};
debug('ignore all paths');
}
}
options.loginCallback = options.loginCallback || defaultLoginCallback;
options.logoutCallback = options.logoutCallback || defaultLogoutCallback;
options.loginCheck = options.loginCheck || defaultLoginCheck;
var loginHandler = login(options);
var loginCallbackHandler = loginCallback(options);
var logoutHandler = logout(options);
/**
* login flow:
*
* 1. unauth user, redirect to `$loginPath?redirect=$currentURL`
* 2. user visit `$loginPath`, redirect to `options.loginURLFormatter()` return login url.
* 3. user visit $loginCallbackPath, handler login callback logic.
* 4. If user login callback check success, will set `req.session[userField]`,
* and redirect to `$currentURL`.
* 5. If login check callback error, next(err).
* 6. user visit `$logoutPath`, set `req.session[userField] = null`, and redirect back.
*/
return function* userauth(next) {
var loginRequired = !!needLogin(this.path, this);
debug('url: %s, path: %s, loginPath: %s, session exists: %s, login required: %s',
this.url, this.path, options.loginPath, !!this.session, loginRequired);
if (!this.session) {
debug('this.session not exists');
// ignore not match path
if (!loginRequired) {
debug('not match needLogin path, %j', this.path);
return yield next;
}
debug('relogin again');
return yield loginHandler.call(this, next);
}
// get login path
if (this.path === options.loginPath) {
debug('match login path');
return yield loginHandler.call(this, next);
}
// get login callback
if (this.path === options.loginCallbackPath) {
debug('match login clalback path');
return yield loginCallbackHandler.call(this, next);
}
// get logout
if (this.path === options.logoutPath) {
debug('match logout path');
return yield logoutHandler.call(this, next);
}
// ignore not match path
if (!loginRequired) {
debug('ignore %j', this.path);
return yield next;
}
if (this.session[options.userField]
&& options.loginCheck(this)) {
// 4. user logined, next() handler
debug('already logined');
return yield next;
}
// try to getUser directly
var user;
try {
user = yield options.getUser(this);
} catch (err) {
console.error('[koa-userauth] options.getUser error: %s', err.stack);
}
if (!user) {
debug('can not get user');
var ctx = this;
// make next handle a generator
// so it can use yield next in redirectHandle
var nextHandler = (function* () {
var redirectURL = ctx.url;
try {
redirectURL = encodeURIComponent(redirectURL);
} catch (e) {
// URIError: URI malformed
// use source url
}
var loginURL = options.loginPath + '?redirect=' + redirectURL;
debug('redirect to %s', loginURL);
redirect(ctx, loginURL);
})();
return yield options.redirectHandler.call(this, nextHandler, next);
}
debug('get user directly');
var res = yield options.loginCallback(this, user);
var loginUser = res[0];
var redirectURL = res[1];
this.session[options.userField] = loginUser;
if (redirectURL) {
return redirect(this, redirectURL);
}
yield next;
};
};
function* defaultRedirectHandler(nextHandler) {
yield nextHandler;
}
function* defaultLoginCallback(ctx, user) {
return [user, null];
}
function* defaultLogoutCallback(ctx, user, callback) {
return null;
}
function defaultLoginCheck() {
return true;
}
/**
* Send redirect response.
*
* @param {Context} ctx
* @param {String} url, redirect URL
* @param {Number|String} status, response status code, default is `302`
*
* @api public
*/
function redirect(ctx, url, status) {
if (ctx.accepts('html', 'json') === 'json') {
ctx.set('Location', url);
ctx.status = 401;
ctx.body = {
error: '401 Unauthorized'
};
return;
}
return ctx.redirect(url, status);
}
/**
* formate referer
* @param {Context} ctx
* @param {String} pathname - login path or logout path
* @param {String} rootPath
* @return {String}
*
* @api private
*/
function formatReferer(ctx, pathname, rootPath) {
var query = ctx.query;
var referer = query.redirect || ctx.get('referer') || rootPath;
if (referer[0] !== '/') {
// ignore protocol://xxx/abc
referer = rootPath;
} else if (referer.indexOf(pathname) === 0) {
// referer start with loginPath or logoutPath, just redirect to rootPath
referer = rootPath;
}
return referer;
}
/**
* return a loginHandler
* @param {Object} options
* @return {Function}
*/
function login(options) {
var defaultHost = options.host;
return function* loginHandler() {
var loginCallbackPath = options.loginCallbackPath;
var loginPath = options.loginPath;
// this.session should be exists
if (this.session) {
this.session.userauthLoginReferer = formatReferer(this, loginPath, options.rootPath);
debug('set loginReferer into session: %s', this.session.userauthLoginReferer);
}
var host = defaultHost || this.host;
var protocol = options.protocol || this.protocol;
var currentURL = protocol + '://' + host + loginCallbackPath;
var loginURL = options.loginURLFormatter(currentURL, options.rootPath, this);
debug('login redrect to loginURL: %s', loginURL);
redirect(this, loginURL);
};
}
/**
* return a loginCallbackHandler
* @param {Object} options
* @return {Function}
*/
function loginCallback(options) {
return function* loginCallbackHandler() {
var referer = this.session.userauthLoginReferer || options.rootPath;
debug('[loginCallbackHandler] loginReferer in session: %j', this.session.userauthLoginReferer);
// cleanup the userauthLoginReferer on session
this.session.userauthLoginReferer = undefined;
var user = this.session[options.userField];
if (user) {
// already login
return redirect(this, referer);
}
user = yield options.getUser(this);
if (!user) {
return redirect(this, referer);
}
var res = yield options.loginCallback(this, user);
var loginUser = res[0];
var redirectURL = res[1];
this.session[options.userField] = loginUser;
if (redirectURL) {
referer = redirectURL;
}
redirect(this, referer);
};
}
/**
* return a logoutHandler
* @param {Object} options
* @return {Function}
*/
function logout(options) {
return function* logoutHandler() {
var referer = formatReferer(this, options.logoutPath, options.rootPath);
var user = this.session[options.userField];
if (!user) {
return redirect(this, referer);
}
var redirectURL = yield options.logoutCallback(this, user);
this.session[options.userField] = null;
if (redirectURL) {
referer = redirectURL;
}
redirect(this, referer);
};
}