-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathjwt.js
97 lines (78 loc) · 2.1 KB
/
jwt.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
var jwt = require("jsonwebtoken");
/**
* JWT adapter.
*
* @type {*}
*/
module.exports = function(adapterId, adapterType, config)
{
var r = {};
r.parse = function(req) {
var value = null;
if (config.header)
{
value = req.headers[config.header.toLowerCase()];
}
else if (config.cookie)
{
value = req.cookies[config.cookie.toLowerCase()];
}
// Strip Bearer from JWT if exist
if (value.substr(0, 7) === 'Bearer ')
{
value = value.substr(7)
}
if (!value)
{
return null;
}
// unpack the jwt
var trusted = config.trusted ? true : false;
var object = null;
if (config.secret)
{
object = jwt.verify(value, config.secret);
trusted = true;
}
else
{
object = jwt.decode(value);
}
// allow for the profile field to be picked off the object
// otherwise, we simply pass the entire JWT object forward
var profileField = config.profile_field;
if (!profileField) {
profileField = "profile";
}
var profile = null;
if (object[profileField]) {
profile = object[profileField];
}
if (!profile) {
profile = object;
}
// pick off user id
var user_identifier_field = config.field;
if (!user_identifier_field)
{
user_identifier_field = "preferred_username";
}
var user_identifier = profile[user_identifier_field];
// if not found, try "unique_name"
if (!user_identifier) {
user_identifier = profile["unique_name"];
}
var properties = {};
// required
properties.token = value;
properties.trusted = trusted;
if (profile) {
properties.profile = profile;
}
if (user_identifier) {
properties.user_identifier = user_identifier;
}
return properties;
};
return r;
};