-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.js
222 lines (167 loc) · 5.7 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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*jshint node:true */
'use strict';
var oauth2orize = require('oauth2orize')
, passport = require('passport')
, assert = require('assert')
, slide = require('slide');
var BasicStrategy = require('passport-http').BasicStrategy
, BearerStrategy = require('passport-http-bearer').Strategy;
function isFunction(fn) {
return typeof fn == 'function';
}
// create OAuth 2.0 server
var server = oauth2orize.createServer()
, db = {
users: null
, tokens: null
, clients: null
};
function checkInitOptions(options) {
var client;
if (!options.dbUsers) { throw new Error('Jab OAuth2 server options should contain dbUsers option!'); }
if (!options.dbTokens) { throw new Error('Jab OAuth2 server options should contain dbTokens option!'); }
db.users = options.dbUsers;
checkdbUsers(db.users);
db.tokens = options.dbTokens;
checkdbTokens(db.tokens);
if (!options.dbClients) {
if (!options.client_id || !options.client_secret) {
throw new Error('Jab OAuth2 server options should contain either dbClients option or pair client_id + client_secret options');
}
options.dbClients = require('./default_db_clients');
client = new options.dbClients(options.client_id, options.client_secret);
client.save();
}
db.clients = options.dbClients;
checkdbClients(db.clients);
}
function checkdbClients(DBClients) {
assert(isFunction(DBClients.findOneByClientId), 'DBClients must implement findOneByClientId');
var client = new DBClients();
assert(isFunction(client.clientSecretCompare), 'client instance must implement clientSecretCompare');
}
function checkdbUsers(DBUsers) {
assert(isFunction(DBUsers.findOneByUsername), 'DBUsers must implement findOneByUsername');
assert(isFunction(DBUsers.findOneById), 'DBUsers must implement findOneById');
var user = new DBUsers();
assert(isFunction(user.comparePassword), 'user instance must implement comparePassword');
assert(isFunction(user.toObject), 'user instance must implement toObject');
}
function checkdbTokens(DBTokens) {
assert(isFunction(DBTokens.createByParams), 'DBTokens must implement save');
assert(isFunction(DBTokens.findOneByToken), 'DBTokens must implement findOneByToken');
}
/**
* Module dependencies.
*/
function attach(app, options) {
options = options || {};
checkInitOptions(options);
app.use(passport.initialize());
app.use(passport.session());
// app.use(app.router);
authenticateRoute(app);
logoutRoute(app);
}
function exchangePassword(client, username, password, scope, done) {
db.users.findOneByUsername(username, function(err, user) {
if (err) { return done(err); }
if (!user) { return done(null, false); }
if (user.isActive && !user.isActive()) { return done(null, false); }
user.comparePassword(password, function(err, isMatched) {
if (err) { return done(err); }
if (!isMatched) { return done(null, false); }
// var token = serializer.stringify([user.id, client.client_id, +new Date(), count]);
db.tokens.createByParams(user.id, client.client_id, function(err, token) {
if (err) { return done(err); }
done(null, token.token);
});
});
});
}
server.exchange(oauth2orize.exchange.password(exchangePassword));
/**
* [basicStrategyCheck description]
* @param {String} clientId [description]
* @param {String} clientSecret [description]
* @param {Function} done [description]
* @return {void} [description]
*/
function basicStrategyCheck(clientId, clientSecret, done) {
db.clients.findOneByClientId(clientId, function(err, client) {
if (err) { return done(err); }
if (client && client.clientSecretCompare(clientSecret)) {
return done(null, client);
}
return done(null, false);
});
}
passport.use(new BasicStrategy(basicStrategyCheck));
/**
* BearerStrategy
*
* This strategy is used to authenticate users based on an access token (aka a
* bearer token). The user must have previously authorized a client
* application, which is issued an access token to make requests on behalf of
* the authorizing user.
*/
function bearerStrategyCheck(accessToken, done) {
var chain = slide.chain
, findToken = db.tokens.findOneByToken.bind(db.tokens)
, findUser = db.users.findOneById.bind(db.users);
chain([
[findToken, accessToken]
, [checkToken, chain.last]
, [findUser, chain.last]
], function (err, res) {
var user;
if (err) { return done(err); }
user = res[res.length - 1];
if (!user) { return done(null, false); }
if (user.isActive && !user.isActive()) { return done(null, false); }
// user = user.toObject();
// delete user.password;
// delete user.salt;
user.access_token = accessToken;
// to keep this example simple, restricted scopes are not implemented,
// and this is just for illustrative purposes
var info = { scope: '*' };
done(null, user, info);
});
}
function checkToken(token, cb) {
if (!token) { return cb(null, false); }
cb(null, token.user_id);
}
passport.use(new BearerStrategy(bearerStrategyCheck));
/**
* routes definition
*
*
* /oauth/token to get
*
*/
function authenticateRoute(app) {
app.post('/oauth/token', [
passport.authenticate('basic', { session: false })
, server.token()
, server.errorHandler()
]);
}
function logoutRoute(app) {
app.delete('/oauth/token',
passport.authenticate('bearer', {session: false})
, function(req, res, next) {
db.tokens.remove({ token: req.user.access_token }, function(err) {
if (err) { return next(err); }
req.logout();
res.send(true);
});
}
, server.errorHandler()
);
}
module.exports = {
attach: attach
, passport: passport
};