-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
433 lines (377 loc) · 13.3 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
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
const express = require('express');
const { Issuer, Strategy } = require('openid-client');
const passport = require('passport');
const process = require('process');
const session = require('express-session');
const sessionStore = require('express-session-rsdb');
const request = require('request-promise-native');
const hbs = require('hbs');
const shajs = require('sha.js');
const jose = require('jose');
const { Client } = require('pg');
const basicAuth = require('express-basic-auth')
const secret = 'setec astronomy'
const API_URL = process.env.API_URL || 'https://staging-api.va.gov'
const CLIENT_ID = process.env.CLIENT_ID || 'VAMobile'
const CLIENT_SECRET = process.env.CLIENT_SECRET
const PORT = process.env.PORT || 4001;
const BASIC_AUTH_USER = process.env.BASIC_AUTH_USER;
const BASIC_AUTH_PASSWORD = process.env.BASIC_AUTH_PASSWORD;
const IAM_OAUTH_URL = process.env.OAUTH_URL || 'https://sqa.fed.eauth.va.gov/oauthe/sps/oauth/oauth20/authorize';
const IAM_CALLBACK_URL = process.env.CALLBACK_URL || 'http://localhost:' + PORT + '/auth/login-success';
const IAM_TOKEN_URL = 'https://sqa.fed.eauth.va.gov/oauthe/sps/oauth/oauth20/token'
const SIS_OAUTH_URL='https://staging.va.gov/sign-in'
const SIS_CALLBACK_URL='https://va-mobile-cutter.herokuapp.com/v0/sign_in/callback';
const SIS_TOKEN_URL = `${API_URL}/v0/sign_in/token`;
const SIS_REFRESH_URL = `${API_URL}/v0/sign_in/refresh`
const SIS_INTROSPECT_URL = `${API_URL}/mobile/v2/user`
const SIS_CLIENT_ID = 'vamobile_test';
const CODE_CHALLENGE = process.env.CODE_CHALLENGE;
const CODE_VERIFIER = process.env.CODE_VERIFIER;
function createClient(type) {
var oauth_url = null;
var callback_url = null;
var client_id = null;
var issuer_token_url = {};
switch (type) {
case ('iam'):
oauth_url = IAM_OAUTH_URL;
callback_url = IAM_CALLBACK_URL;
client_id = CLIENT_ID;
issuer_token_url = { token_endpoint: IAM_TOKEN_URL }
break;
case ('sis'):
oauth_url = SIS_OAUTH_URL;
callback_url = SIS_CALLBACK_URL;
client_id = SIS_CLIENT_ID;
break;
}
Issuer.defaultHttpOptions = { timeout: 5000 };
const ssoeIssuer = new Issuer({
issuer: 'https://sqa.fed.eauth.va.gov/oauthe/sps/oauth/oauth20/metadata/ISAMOPe',
authorization_endpoint: oauth_url,
jwks_uri: 'https://sqa.fed.eauth.va.gov/oauthe/sps/oauth/oauth20/jwks/ISAMOPeFP',
...issuer_token_url
//Advertised in metadata but seemingly not supported
// userinfo_endpoint: 'https://sqa.fed.eauth.va.gov/oauthi/sps/oauth/oauth20/userinfo',
});
return new ssoeIssuer.Client({
client_id: client_id,
client_secret: CLIENT_SECRET,
redirect_uris: [
callback_url,
],
response_types: ['code'],
});
}
function configurePassport(client, type) {
const typedPassport = new passport.Authenticator()
var params = null;
var pkce = null;
switch (type) {
case ('iam'):
params = {
scope: 'openid',
response_mode: 'query'
}
pkce = true;
break;
case ('sis'):
params = {
application: 'vamobile',
code_challenge: CODE_CHALLENGE,
code_challenge_method: 'S256',
oauth: 'true',
client_id: SIS_CLIENT_ID
}
pkce = false;
break;
}
typedPassport.serializeUser(function(user, done) {
done(null, user);
});
typedPassport.deserializeUser(function(user, done) {
done(null, user);
});
typedPassport.use('oidc', new Strategy(
{
client,
params: params,
usePKCE: pkce,
// SSOE oAuth seems to require these parameters for token exchange
// even in PKCE mode, so add them here
extras: {
exchangeBody: {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
},
}
},
(tokenset, done) => {
({ payload } = jose.JWT.decode(tokenset.id_token, { complete: true }));
user = Object.assign(payload, tokenset);
if (process.env.VERBOSE === 'true') {
console.log('access_token', tokenset.access_token);
console.log('id_token', payload);
}
console.log('user.name', user.email);
console.log('user.icn', user.fediamMVIICN);
console.log('access_token digest', new shajs.sha256().update(user.access_token).digest('hex'));
return done(null, user);
}
));
return typedPassport;
}
function requireLogin(req, res, next) {
if (req.session.user) {
return next();
} else {
res.redirect('/');
}
}
function createDbClient() {
const dbClient = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});
dbClient.connect();
return dbClient;
}
function writeToDb(statement, values) {
const db = createDbClient();
db.query(statement, values, (err, res) => {
if (err) throw err;
if (process.env.VERBOSE === 'true') {
for (let row of res.rows) {
console.log(JSON.stringify(row));
}
}
db.end();
});
}
async function findUserRecord(email) {
const db = createDbClient();
const { rows } = await db.query('SELECT * FROM tokens WHERE email = $1 LIMIT 1', [email]);
db.end();
return rows[0];
}
function saveUserRecord(type, email, accessToken, refreshToken) {
const timestamp = new Date().toISOString();
const statement = `INSERT INTO tokens (email, ${type}_access_token, ${type}_refresh_token, created_at) VALUES ($1, $2, $3, $4);`;
const values = [email, accessToken, refreshToken, timestamp];
writeToDb(statement, values);
}
function updateUserRecord(type, email, accessToken, refreshToken) {
const timestamp = new Date().toISOString();
const statement = `UPDATE tokens SET ${type}_access_token = $1, ${type}_refresh_token = $2, updated_at = $3 WHERE email = $4;`;
const values = [accessToken, refreshToken, timestamp, email];
writeToDb(statement, values);
}
async function createOrUpdateRecord(type, email, accessToken, refreshToken) {
const record = await findUserRecord(email);
if (record) {
updateUserRecord(type, email, accessToken, refreshToken);
} else {
saveUserRecord(type, email, accessToken, refreshToken);
}
}
function startApp() {
const app = express();
app.set('view engine', 'hbs');
app.set('view options', { layout: 'layout' });
app.engine('handlebars', hbs.__express);
app.use(express.static('assets'));
app.use(session({
store: new sessionStore(),
secret: secret,
resave: false,
saveUninitialized: true,
cookie: { secure: false, maxAge: 60 * 60000 },
}));
const iamClient = createClient('iam')
const iamPassport = configurePassport(iamClient, 'iam');
app.use(iamPassport.initialize());
app.use(iamPassport.session());
const sisClient = createClient('sis')
const sisPassport = configurePassport(sisClient, 'sis');
app.use(sisPassport.initialize());
app.use(sisPassport.session());
app.get('/', (req, res) => {
console.log('session id', req.session.id);
res.render('index', { user: req.session.user, activeIndex: true, header: "Welcome" });
});
app.get('/user', requireLogin, async (req, res, next) => {
const options = {
url: API_URL + '/mobile/v0/user',
headers: { 'Authorization': 'Bearer ' + req.session.user['access_token'] }
};
try {
const response = await request(options);
const output = JSON.stringify(JSON.parse(response), undefined, 2);
const locals = { userinfo: output, user: req.session.user, header: 'User Info' };
locals['activeUser'] = true;
res.render('user', locals);
} catch (error) {
res.render('error', { error: error, user: req.session.user, header: "Error" });
}
});
app.get('/messaging', requireLogin, async (req, res, next) => {
const options = {
url: API_URL + '/mobile/v0/messaging/health/folders',
headers: { 'Authorization': 'Bearer ' + req.session.user['access_token'] }
};
try {
console.log('folders request', options);
const response = await request(options);
const raw = JSON.stringify(JSON.parse(response), undefined, 2);
const { data } = JSON.parse(response);
const locals = { folders: data, raw: raw, user: req.session.user, header: 'Messaging' };
locals['activeMessaging'] = true;
res.render('folders', locals);
} catch (error) {
res.render('error', { error: error, user: req.session.user, header: "Error" });
}
});
app.get('/messaging/:folderId', requireLogin, async (req, res, next) => {
const options = {
url: API_URL + `/mobile/v0/messaging/health/folders/${req.params.folderId}/messages`,
headers: { 'Authorization': 'Bearer ' + req.session.user['access_token'] }
};
try {
console.log('folder request', options);
const response = await request(options);
const raw = JSON.stringify(JSON.parse(response), undefined, 2);
const { data } = JSON.parse(response)
const locals = { messages: data, raw: raw, user: req.session.user, header: 'Messaging' };
locals['activeMessaging'] = true;
res.render('messages', locals);
} catch (error) {
res.render('error', { error: error, user: req.session.user, header: "Error" });
}
});
app.get('/auth/iam', iamPassport.authenticate('oidc'),
function(req, res) {
req.session.user = Object.assign(req.session.user, req.user);
}
);
app.get('/auth/sis', sisPassport.authenticate('oidc'),
function(req, res) {
req.session.user = Object.assign(req.session.user, req.user);
}
);
app.get('/auth/login-success', iamPassport.authenticate('oidc'),
function(req, res) {
req.session.user = Object.assign(req.user);
const email = req.session.user.email;
const accessToken = req.session.user.access_token;
const refreshToken = req.session.user.refresh_token;
createOrUpdateRecord('iam', email, accessToken, refreshToken)
res.redirect('/');
}
);
app.get('/auth/sis/login-success', async function(req, res, next) {
try {
const tokenOptions = {
url: SIS_TOKEN_URL,
headers: { 'Content-Type': 'application/json' },
form: {
'grant_type': 'authorization_code',
'code_verifier': CODE_VERIFIER,
'code': req.query.code
}
}
const tokenResponse = await request.post(tokenOptions);
const tokenOutput = JSON.parse(tokenResponse);
const accessToken = tokenOutput.data.access_token;
const refreshToken = tokenOutput.data.refresh_token;
const userData = { access_token: accessToken, refresh_token: refreshToken }
const introspectOptions = {
url: SIS_INTROSPECT_URL,
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${userData.access_token}`,
'Authentication-Method': 'SIS',
'X-Key-Inflection': 'camel'
}
}
const introspectResponse = await request(introspectOptions);
const instrospectOutput = JSON.parse(introspectResponse);
const email = instrospectOutput.data.attributes.signinEmail;
userData['email'] = email;
req.session.user = Object.assign(userData);
createOrUpdateRecord('sis', email, accessToken, refreshToken);
next();
} catch (error) {
console.log('ERROR', error);
next();
}
}, (req, res, next) => {
res.redirect('/');
});
app.use('/auth/iam/token/:email', basicAuth({
users: { [BASIC_AUTH_USER]: BASIC_AUTH_PASSWORD }
}));
app.get('/auth/iam/token/:email', async (req, res) => {
try {
const email = req.params.email;
const record = await findUserRecord(email);
if (!record) {
res.send({ message: 'manual login required' }).status(404);
return null;
}
const extras = {
exchangeBody: {
client_id: CLIENT_ID,
client_secret: CLIENT_SECRET,
redirect_uri: IAM_CALLBACK_URL,
}
}
console.log('Refreshing with', email);
var tokenset = await iamClient.refresh(record.iam_refresh_token, extras);
updateUserRecord('iam', email, tokenset.access_token, tokenset.refresh_token);
res.send({ access_token: tokenset.access_token }).status(200);
} catch (error) {
console.log('ERROR REFRESHING IAM TOKEN', error)
res.send({ error: error }).status(500);
}
});
app.use('/auth/sis/token/:email', basicAuth({
users: { [BASIC_AUTH_USER]: BASIC_AUTH_PASSWORD }
}));
app.get('/auth/sis/token/:email', async (req, res) => {
try {
const email = req.params.email;
const record = await findUserRecord(email);
if (!record) {
res.send({ message: 'manual login required' }).status(404);
return null;
}
const options = {
url: SIS_REFRESH_URL,
headers: {
'Content-Type': 'application/json'
},
form: {
'refresh_token': record.sis_refresh_token
}
}
console.log('Refreshing with', email);
const response = await request.post(options);
const output = JSON.parse(response);
updateUserRecord('sis', email, output.data.access_token, output.data.refresh_token);
res.send({ access_token: output.data.access_token }).status(200);
} catch (error) {
console.log('ERROR REFRESHING SIS TOKEN', error)
res.send({ error: error }).status(500);
}
});
app.get('/logout', (req, res, next) => {
req.session.destroy();
res.redirect('/');
next();
});
app.listen(PORT, () => console.log(`Example app listening on port ${PORT}!`));
}
startApp();