-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.js
468 lines (444 loc) · 12.2 KB
/
server.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
const express = require('express');
const https = require('https');
const http = require('http');
const bodyParser = require('body-parser');
const session = require('express-session');
const promise = require('bluebird');
const pg = require('pg-promise')(
{ promiseLib: promise }
);
const fs = require('fs');
const config = require('./config.json');
const jwt = require('jsonwebtoken');
const jwkToPem = require('jwk-to-pem');
const url = require('url');
const querystring = require('querystring');
const database = pg(config.database);
const jwtOptions = {
audience: config.google.clientId,
algorithms: ["HS256", "HS384", "HS512", "RS256", "RS384", "RS512", "ES256", "ES384", "ES512"]
};
const rsa = {
private: fs.readFileSync(config.jwtCreation.key.private),
public: fs.readFileSync(config.jwtCreation.key.public)
};
Error.stackTraceLimit = Infinity;
function httpsGet(url, done){
https.get(url, function(res){
var body = '';
res.on('data', function(chunk){
body += chunk;
});
res.on('end', function(){
done(false, JSON.parse(body));
});
res.on('close',function(){
done(new Error('Failed to load resource: ' + url));
});
});
}
function getKeys(gConf, done){
httpsGet('https://www.googleapis.com/identitytoolkit/v3/relyingparty/publicKeys', function(idError, idKeys){
if(idError){
console.warn('failed to get google openid key');
done(idError);
return;
}
httpsGet(gConf.jwks_uri, function(jwkError, jwks){
if(jwkError){
console.warn('failed to get google openid key');
done(jwlError);
}
gConf.jwks = jwks;
Object.keys(idKeys).forEach(function(k){
jwks.keys.push(idKeys[k]);
});
console.log('KEYS: ' + JSON.stringify(gConf.jwks.keys));
done(false, gConf);
});
});
}
function getOpenIdConfig(done){
httpsGet(config.google.openId, function(closed, res) {
if(closed){
console.warn('failed to get open id config');
process.exit();
}
console.log(res);
getKeys(res, done);
});
}
function verifyIdToken(gConf, options){
return function(req, res, next){
var rawToken = req.openid.rawToken;
if(!rawToken){
res.send(400, 'missing id token');
next(Error('id token missing'));
return;
}
if(!options){
options = jwtOptions;
}
console.log(rawToken);
var decoded = jwt.decode(rawToken, { complete: true });
var token = decoded.payload;
var header = decoded.header;
function verify(){
var jwks = gConf.jwks.keys;
for(var i = 0; i < jwks.length; ++i){
var jwk = jwks[i];
var key = (typeof jwk === 'string') ? jwk : jwkToPem(jwk);
console.log(key);
try {
jwt.verify(rawToken, key, options)
} catch(err) {
if(err instanceof jwt.TokenExpiredError){
res.send(400, 'token expired');
console.log('token expired');
return true;
}
if(err instanceof jwt.JsonWebTokenError){
console.log(err);
continue;
} else {
res.send(400, err.message);
next(err);
return true;
}
}
console.log('VERIFIED');
console.log(token);
req.openid.token = token;
next();
return true;
}
return false;
}
if(verify()){
return;
}
console.log('failed to verify signature, refreshing keys');
getKeys(gConf, function(closed){
if(closed){
res.send(400,'failed to verify token');
next(new jwt.JsonWebTokenError('failed to refresh google key'));
return;
}
if(verify()){
return;
}
res.send(400,'failed to verify token');
next(new jwt.JsonWebTokenError('failed to verify token'));
});
}
}
getOpenIdConfig(function(googleConfErr, googleConfig){
if(googleConfErr){
throw googleConfErr;
}
const connection = [
function(req, res, next){ // extract and authenticate token
console.log('connection request: ' + JSON.stringify(req.query));
res.header('Access-Control-Allow-Origin','*');
if(req.query.token){
var rawToken = req.query.token;
var token = jwt.decode(rawToken);
if(token.aud !== 'https://auth.thepathfinder.xyz'){
res.send(401, 'token requires aud = "https://auth.thepathfinder.xyz"');
return;
}
database.one('select name, key from application where id = $1',[token.iss])
.then(function(key){
try {
jwt.verify(rawToken, key, {algorithms: ["RS256"]});
} catch(e){
next(e);
res.send(401, e.message)
return;
}
if(token.expires < Date.now() / 1000){
next(jwt.JsonWebTokenError('Token Expired'));
res.send(401, 'token expired');
return;
}
req.pathfinder = {
sub:token.sub,
email:token.email,
aud:'https://api.thepathfinder.xyz',
appId:token.iss,
status:'Authenticated',
iss:'https://auth.thepathfinder.xyz'
};
next();
})
.catch(function(e){
next(e);
res.sendStatus(500);
});
} else if(req.query.id_token){
var token = jwt.decode(req.query.id_token);
var appId = req.query.application_id;
var connectionId = req.query.connection_id;
var email = token.email;
console.log('Receive id token: '+req.query.id_token);
if(!appId){
res.send(400, 'application_id required');
return;
}
if(!connectionId){
res.send(400, 'connection_id required');
return;
}
if(token.iss === googleConfig.issuer || token.iss === 'accounts.google.com' || true){
var connection = req.query.connection
req.openid = {
rawToken: req.query.id_token
};
var options = {algorithms: jwtOptions.algorithms};
verifyIdToken(googleConfig, options)(req,res,function(err){
if(err){
next(err);
res.send(400, 'could not verify id token: '+ err.message);
return;
}
console.log('google id token verified');
req.pathfinder = {
sub:connectionId,
email:email,
aud:'https://api.thepathfinder.xyz',
appId:appId,
status:'Authenticated',
iss:'https://auth.thepathfinder.xyz'
};
next()
});
} else {
res.send(400, 'invalid issuer for id token: ' + token.iss);
console.log('invalid issuer for id token: ' + token.iss);
}
} else {
res.send(400, 'No id token sent');
console.log('No id token sent');
}
}, function(req, res, next){ // get permissions
var pf = req.pathfinder;
var email = pf.email;
var appId = pf.appId
database.one('select permissions from permission where email=$1 and application_id=$2;',[email, appId])
.then(function(data){
req.pathfinder.permissions = data.permissions;
next();
})
.catch(function(e){
if(e instanceof pg.QueryResultError){
res.send(404, 'user not found');
console.log('user not found:' + email);
} else {
res.send(500,'Error reading database for permissions with email: ' + email);
next(e);
}
});
}, function(req, res, next){ // assemble and save token
var token = req.pathfinder;
var tokenStr;
try {
tokenStr = jwt.sign(token, rsa.private, config.jwtCreation);
} catch(e){
next(e);
res.send(500, 'Internal Server Error');
return;
}
database.none(
'insert into connection (id, token) values ($1, $2);',
[token.sub, tokenStr]
).then(function(){
res.sendStatus(204);
}).catch(function(e){
if(e.toString().indexOf('error: duplicate key value violates unique constraint') !== -1) { // this is a generic Error so we need to check the message
database.none(
'update connection set token=$2 where id=$1;',
[token.sub, tokenStr]
).then(function(){
res.sendStatus(204);
}).catch(function(e){
next(e);
res.sendStatus(500);
});
} else {
next(e);
res.sendStatus(500);
}
});
}
];
const redirect_uri = url.format({
protocol: 'https',
hostname: config.server.host,
pathname: '/auth/google/return'
});
jwtOptions.issuer = googleConfig.issuer;
server = express();
server.use(session({
saveUninitialized: false,
resave: false,
cookie: { secure: true },
secret: config.session.secret
}));
server.use(bodyParser.json({limit:'50mb'}));
server.use(bodyParser.urlencoded());
server.get('/auth/google',
function(req, res, next){
req.session.return_token = req.query.return_token;
req.session.url_encode = req.query.url_encode;
if(!(req.session.return_token || req.query.return_url)){
res.status(400).send('no return url specified');
return;
}
req.session.return_url = req.query.return_url;
req.session.application = req.query.application;
if(req.query.application){
database.one("select id from application where id = $1",[req.query.application])
.then(function(data){
next();
})
.catch(function(error){
switch(error.name){
case 'QueryResultError':
res.send(404,
'no application with id: ' + req.query.application
);
break;
default:
res.statusStatus(500);
next(error);
break;
}
});
} else {
next();
}
},
function(req, res){
var authUrl = url.parse(googleConfig.authorization_endpoint);
authUrl.query = {
redirect_uri: redirect_uri,
response_type: 'code',
scope: 'openid email',
state: req.session.id,
client_id: config.google.clientId
};
res.redirect(authUrl.format());
}
);
server.get('/auth/google/return',
function(req, res, next){
if(req.session.id !== req.query.state){
res.send(401, 'invalid session state');
return;
}
if(req.query.error){
console.warn(req.query.error);
res.send(400, req.query.error);
return;
}
if(!req.query.code){
res.send(401, 'code parameter missing');
return;
}
var body = querystring.stringify({
code: req.query.code,
client_id: config.google.clientId,
client_secret: config.google.secret,
redirect_uri: redirect_uri,
grant_type: 'authorization_code'
});
var options = url.parse(googleConfig.token_endpoint);
options.method = 'POST';
options.headers = {
'Content-Type': 'application/x-www-form-urlencoded'
};
var post = https.request(options, function(result){
var data = '';
result.on('data', function(chunk){
data += chunk;
});
result.on('end', function(){
req.openid = {
rawToken: JSON.parse(data).id_token
};
next();
});
result.on('close', function(){
res.sendStatus(500, 'Error getting tokens');
});
});
post.on('error', function(e){
res.send(500, 'Error getting tokens');
throw e;
});
post.end(body);
},
verifyIdToken(googleConfig),
function(req, res, next){
var token = req.openid.token;
var email = token.email;
var appId = req.session.application;
if(appId){
database.none('insert into permission (email, application_id, permissions) values ($1, $2, $3);',[email, appId, '{}'])
.then(next).catch(function(e){
if(e.toString().indexOf('error: duplicate key value violates unique constraint') !== -1) { // this is a generic Error so we need to check the message
next()
} else {
res.sendStatus(500);
next(e);
}
});
} else {
next();
}
}, function(req, res, next){
if(req.session.return_token){
if(req.session.url_encode){
res.send(encodeURIComponent(req.openid.rawToken));
} else {
res.send(req.openid.rawToken);
}
} else {
var obj = url.parse(req.session.return_url);
obj.query = obj.query || {};
obj.query.id_token = req.openid.rawToken;
res.redirect(url.format(obj));
}
}
);
server.get('/connection', function(req, res, next){
if(config.apiServer.address && req.connection.remoteAddress !== config.apiServer.address){
res.sendStatus(401);
return;
}
var connectionId = req.query.connection_id;
if(!connectionId){
res.send(400, 'connection_id parameter not set');
}
database.one('delete from connection where id = $1 returning token;',[connectionId])
.then(function(data){
var jwt = new Buffer(data.token).toString('utf8');
console.log('sending jwt');
console.log(jwt);
res.send(jwt);
})
.catch(function(e){
next(e);
res.sendStatus(500);
});
});
server.post('/connection', connection);
server.get('/certificate', function(req, res){
res.set('Content-Type','text/plain');
res.send(rsa.public);
});
server.get('/:stuff',function(req,res){
res.sendStatus(404);
});
http.createServer(server).listen(config.server.port);
});