-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
64 lines (53 loc) · 1.8 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
var minimatch = require('minimatch');
module.exports = statelessauth;
function statelessauth(validator, options) {
return function * (next) {
if (options && options.ignorePaths) {
var ignorePathMatched = false;
var path = this.path;
options.ignorePaths.some(function (element) {
options.verbose && console.log("path=" + path);
options.verbose && console.log("ignorepath=" + element);
var match = minimatch(path, element);
if (match) {
options.verbose && console.log("matched");
ignorePathMatched = true;
return true;
}
return false;
});
//Can't yield from normal function
if (ignorePathMatched) {
yield next;
return;
}
}
var authheader = this.get("Authorization");
if (this.get("Authorization") === undefined) {
this.status = 401;
options.verbose && console.log('no header');
return;
}
if (authheader === '') {
this.status = 401;
options.verbose && console.log('empty header');
return;
}
if (!validator || !validator.validate) {
this.status = 401;
options.verbose && console.log('no validator');
return;
}
var claims = validator.validate(authheader);
if (!claims) {
this.status = 401;
options.verbose && console.log('validator failed');
return;
}
this.user = {};
this.user.name = claims.name;
this.user.email = claims.email;
this.user.role = claims.role;
yield next;
}
}