-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathmodel.js
More file actions
184 lines (154 loc) · 5.37 KB
/
model.js
File metadata and controls
184 lines (154 loc) · 5.37 KB
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
var JWT = require('jsonwebtoken');
var model = module.exports;
// based on https://github.com/thomseddon/node-oauth2-server/tree/master/examples/memory
var JWT_ISSUER = 'thisdemo';
var JWT_SECRET_FOR_ACCESS_TOKEN = 'XT6PRpRuehFsyMa2';
var JWT_SECRET_FOR_REFRESH_TOKEN = 'JWPVzFWkqGxoE2C2';
// the expiry times should be consistent between the oauth2-server settings
// and the JWT settings (not essential, but makes sense)
model.JWT_ACCESS_TOKEN_EXPIRY_SECONDS = 1800; // 30 minutes
model.JWT_REFRESH_TOKEN_EXPIRY_SECONDS = 1209600; // 14 days
// In-memory datastores
var oauthClients = [{
clientId : 'thom',
clientSecret : 'nightworld',
redirectUri : ''
}];
// key is grant_type
// value is the array of authorized clientId's
var authorizedClientIds = {
password: [
'thom'
],
refresh_token: [
'thom'
]
};
// current registered users
var users = [ {
id : '123',
username: 'thomseddon',
password: 'nightworld'
}
];
// Functions required to implement the model for oauth2-server
// generateToken
// This generateToken implementation generates a token with JWT.
// the token output is the Base64 encoded string.
model.generateToken = function(type, req, callback) {
var token;
var secret;
var user = req.user;
var exp = new Date();
var payload = {
// public claims
iss: JWT_ISSUER, // issuer
// exp: exp, // the expiry date is set below - expiry depends on type
// jti: '', // unique id for this token - needed if we keep an store of issued tokens?
// private claims
userId: user.id
};
var options = {
algorithms: ['HS256'] // HMAC using SHA-256 hash algorithm
};
if (type === 'accessToken') {
secret = JWT_SECRET_FOR_ACCESS_TOKEN;
exp.setSeconds(exp.getSeconds() + model.JWT_ACCESS_TOKEN_EXPIRY_SECONDS);
} else {
secret = JWT_SECRET_FOR_REFRESH_TOKEN;
exp.setSeconds(exp.getSeconds() + model.JWT_REFRESH_TOKEN_EXPIRY_SECONDS);
}
payload.exp = exp.getTime();
token = JWT.sign(payload, secret, options);
callback(false, token);
};
// The bearer token is a JWT, so we decrypt and verify it. We get a reference to the
// user in this function which oauth2-server puts into the req object
model.getAccessToken = function (bearerToken, callback) {
return JWT.verify(bearerToken, JWT_SECRET_FOR_ACCESS_TOKEN, function(err, decoded) {
if (err) {
return callback(err, false); // the err contains JWT error data
}
// other verifications could be performed here
// eg. that the jti is valid
// we could pass the payload straight out we use an object with the
// mandatory keys expected by oauth2-server, plus any other private
// claims that are useful
return callback(false, {
expires: new Date(decoded.exp),
user: getUserById(decoded.userId)
});
});
};
// As we're using JWT there's no need to store the token after it's generated
model.saveAccessToken = function (accessToken, clientId, expires, userId, callback) {
return callback(false);
};
// The bearer token is a JWT, so we decrypt and verify it. We get a reference to the
// user in this function which oauth2-server puts into the req object
model.getRefreshToken = function (bearerToken, callback) {
return JWT.verify(bearerToken, JWT_SECRET_FOR_REFRESH_TOKEN, function(err, decoded) {
if (err) {
return callback(err, false);
}
// other verifications could be performed here
// eg. that the jti is valid
// instead of passing the payload straight out we use an object with the
// mandatory keys expected by oauth2-server plus any other private
// claims that are useful
return callback(false, {
expires: new Date(decoded.exp),
user: getUserById(decoded.userId)
});
});
};
// required for grant_type=refresh_token
// As we're using JWT there's no need to store the token after it's generated
model.saveRefreshToken = function (refreshToken, clientId, expires, userId, callback) {
return callback(false);
};
// authenticate the client specified by id and secret
model.getClient = function (clientId, clientSecret, callback) {
for(var i = 0, len = oauthClients.length; i < len; i++) {
var elem = oauthClients[i];
if(elem.clientId === clientId &&
(clientSecret === null || elem.clientSecret === clientSecret)) {
return callback(false, elem);
}
}
callback(false, false);
};
// determine whether the client is allowed the requested grant type
model.grantTypeAllowed = function (clientId, grantType, callback) {
callback(false, authorizedClientIds[grantType] &&
authorizedClientIds[grantType].indexOf(clientId.toLowerCase()) >= 0);
};
// authenticate a user
// for grant_type password
model.getUser = function (username, password, callback) {
for (var i = 0, len = users.length; i < len; i++) {
var elem = users[i];
if(elem.username === username && elem.password === password) {
return callback(false, elem);
}
}
callback(false, false);
};
var getUserById = function(userId) {
for (var i = 0, len = users.length; i < len; i++) {
var elem = users[i];
if(elem.id === userId) {
return elem;
}
}
return null;
};
// for grant_type client_credentials
// given client credentials
// authenticate client
// lookup user
// return that user...
// oauth replies with access token and renewal token
//model.getUserFromClient = function(clientId, clientSecret, callback) {
//
//};