-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
36 lines (29 loc) · 968 Bytes
/
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
'use strict';
const authHeaderReg = /(.*) (.*)/;
function *defaultHandler (token, type) {
this.state.authToken = token;
this.state.authType = type;
}
module.exports = function authHeaderParser (options) {
const _options = Object.assign({
handlers: {
default: defaultHandler
}
}, options);
return function *authHeaderParser (next) {
const authHeader = this.get('authorization');
if (!authHeader) {
return yield next;
}
const match = authHeader.match(authHeaderReg);
if (!match || (!_options.handlers[match[1]] && (!_options.handlers || !_options.handlers.default))) {
return yield next;
}
if (_options.handlers[match[1]]) {
yield _options.handlers[match[1]].call(this, match[2], match[1]);
} else {
yield _options.handlers.default.call(this, match[2], match[1]);
}
yield next;
};
};