-
Notifications
You must be signed in to change notification settings - Fork 22
/
index.js
90 lines (78 loc) · 2.25 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
const parseCookie = require('cookie').parse;
const decodeCookie = require('cookie-parser').signedCookie;
const getCookie = (serialized_cookies, key) => parseCookie(serialized_cookies)[key] || false;
module.exports = opts => {
try {
if (!opts) {
opts = {
cookie: false,
};
}
const queryKey = opts.queryKey || 'access_token';
const bodyKey = opts.bodyKey || 'access_token';
const headerKey = opts.headerKey || 'Bearer';
const reqKey = opts.reqKey || 'token';
const cookie = opts.cookie;
if (cookie && !cookie.key) {
cookie.key = 'access_token';
}
if (cookie && cookie.signed && !cookie.secret) {
throw new Error(
'[express-bearer-token]: You must provide a secret token to cookie attribute, or disable signed property'
);
}
return (req, res, next) => {
let token;
let error;
// query
if (req.query && req.query[queryKey]) {
token = req.query[queryKey];
}
// body
if (req.body && req.body[bodyKey]) {
if (token) {
error = true;
}
token = req.body[bodyKey];
}
// headers
if (req.headers) {
// authorization header
if (req.headers.authorization) {
const parts = req.headers.authorization.split(' ');
if (parts.length === 2 && parts[0] === headerKey) {
if (token) {
error = true;
}
token = parts[1];
}
}
// cookie
if (cookie && req.headers.cookie) {
const plainCookie = getCookie(req.headers.cookie || '', cookie.key); // seeks the key
if (plainCookie) {
const cookieToken = cookie.signed
? decodeCookie(plainCookie, cookie.secret)
: plainCookie;
if (cookieToken) {
if (token) {
error = true;
}
token = cookieToken;
}
}
}
}
// RFC6750 states the access_token MUST NOT be provided
// in more than one place in a single request.
if (error) {
res.status(400).send();
} else {
req[reqKey] = token;
next();
}
};
} catch (e) {
console.error(e);
}
};