-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
234 lines (195 loc) · 7.01 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
// server.js
// where your node app starts
// init project
const express = require('express');
const session = require('express-session');
const https = require('https');
const fs = require('fs');
const passport = require('passport');
const cookieParser = require("cookie-parser");
const oidcStrategy = require('passport-openidconnect').Strategy;
const tm = require('./oauthtokenmanager.js');
const identityServices = require('./ciservices.js');
const logger = require('./logging.js');
const app = express();
const authsvc = require('./authsvc.js');
const adminsvc = require('./adminsvc.js');
const request = require('request');
// set to ignore ssl cert errors when making requests
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = 0;
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: true
}));
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.use(cookieParser());
// http://expressjs.com/en/starter/static-files.html
app.use('/static', express.static('static'));
// For OIDC login
app.use(passport.initialize());
app.use(passport.session());
passport.use("oidc", new oidcStrategy({
issuer: process.env.CI_TENANT_ENDPOINT + "/oidc/endpoint/default",
authorizationURL: process.env.CI_TENANT_ENDPOINT + "/oidc/endpoint/default/authorize",
tokenURL: process.env.CI_TENANT_ENDPOINT + "/oidc/endpoint/default/token",
userInfoURL: process.env.CI_TENANT_ENDPOINT + "/oidc/endpoint/default/userinfo",
clientID: process.env.OIDC_CLIENT_ID,
clientSecret: process.env.OIDC_CLIENT_SECRET,
callbackURL: "https://"+process.env.RPID+ (process.env.LOCAL_SSL_SERVER == "true" ? (":"+process.env.LOCAL_SSL_PORT) : "") +"/callback",
scope: "openid profile"
},
(issuer, sub, profile, accessToken, refreshToken, done) => {
let data = {
issuer: issuer,
sub: sub,
profile: profile,
accessToken: accessToken,
refreshToken: refreshToken
};
console.log("OIDC callback function called with: " + JSON.stringify(data));
return done(null, data);
}
));
passport.serializeUser((user, next) => {
next(null, user);
});
passport.deserializeUser((obj, next) => {
next(null, obj);
});
app.use("/loginoidc", passport.authenticate("oidc"));
app.use("/callback",
passport.authenticate("oidc", { failureRedirect: "/error" }),
(req, res) => {
console.log("Callback post-authentication function called with req.user: " + JSON.stringify(req.user));
req.session.username = req.user.profile._json.preferred_username;
req.session.userDisplayName = req.user.profile.displayName;
req.session.userSCIMId = req.user.profile.id;
// this is redundant, but doesn't hurt
req.user.username = req.user.profile._json.preferred_username;
req.user.userDisplayName = req.user.profile.displayName;
req.user.userSCIMId = req.user.profile.id;
identityServices.validateUserAccount(req)
.then(() => {
return identityServices.getUserAccessToken(req, false);
}).then((uat) => {
req.session.userAccessToken = uat;
res.redirect('/');
}).catch((e) => {
logger.logWithTS("OIDC login failed with error. e: " +
e + " stringify(e): " + (e != null ? JSON.stringify(e): "null"));
res.redirect('/error');
});
});
// This permits optional authentication of requests via user access tokens
app.use(authsvc.validateAccessToken());
// This checks that certain requests are only made by an admin
app.use(adminsvc.validateAdminAccess());
//console.log(process.env);
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', (req, rsp) => {
rsp.sendFile(__dirname + '/views/index.html');
});
/*
* Glitch doesn't allow dumping of images in a directory other than it's content delivery network
* So we try to detect these local files and serve them from local content if they exist, otherwise
* fallback to a location where I have uploaded them on Glitch.
*/
app.get('/favicon.ico', (req, rsp) => {
//rsp.sendFile(__dirname + '/static/favicon.ico');
fs.access(__dirname + '/static/favicon.ico', fs.constants.F_OK, (err) => {
if (err == null) {
rsp.sendFile(__dirname + '/static/favicon.ico');
} else {
request.get("https://cdn.glitch.com/8035aca4-73b7-4374-abd0-7779d1378069%2Ffavicon.ico?v=1585551659111").pipe(rsp);
}
});
});
app.get('/static/ibm-logo.png', (req, rsp) => {
fs.access(__dirname + '/static/ibm-logo.png', fs.constants.F_OK, (err) => {
if (err == null) {
rsp.sendFile(__dirname + '/static/ibm-logo.png');
} else {
request.get("https://cdn.glitch.com/8035aca4-73b7-4374-abd0-7779d1378069%2Fibm-logo.png?v=1585551692987").pipe(rsp);
}
});
});
app.get('/static/ibm_logo.jpg', (req, rsp) => {
fs.access(__dirname + '/static/ibm_logo.jpg', fs.constants.F_OK, (err) => {
if (err == null) {
rsp.sendFile(__dirname + '/static/ibm_logo.jpg');
} else {
request.get("https://cdn.glitch.com/8035aca4-73b7-4374-abd0-7779d1378069%2Fibm_logo.jpg?v=1585552484835").pipe(rsp);
}
});
});
app.get('/error', (req,rsp) => {
rsp.sendFile(__dirname + '/views/error.html');
});
app.get('/logout', (req, rsp) => {
req.logout();
req.session.destroy();
rsp.json({"authenticated": false});
});
app.get('/me', (req, rsp) => {
identityServices.sendUserResponse(req, rsp);
});
app.get('/metadata', (req, rsp) => {
identityServices.sendMetadata(req, rsp);
});
app.get('/newAccessToken', (req, rsp) => {
identityServices.newAccessToken(req, rsp);
});
app.get('/registrationDetails', (req, rsp) => {
identityServices.registrationDetails(req, rsp);
});
app.post('/deleteRegistration', (req, rsp) => {
identityServices.deleteRegistration(req, rsp);
});
app.post('/attestation/options', (req, rsp) => {
identityServices.proxyFIDO2ServerRequest(req,rsp,true,false);
});
app.post('/attestation/result', (req, rsp) => {
identityServices.proxyFIDO2ServerRequest(req,rsp,false,false);
});
app.post('/assertion/options', (req, rsp) => {
identityServices.proxyFIDO2ServerRequest(req,rsp,true,true);
});
app.post('/assertion/result', (req, rsp) => {
identityServices.proxyFIDO2ServerRequest(req,rsp,false,false);
});
app.post('/assertion/login', (req, rsp) => {
identityServices.validateFIDO2Login(req,rsp);
});
// admin operations
app.get('/admin', (req, rsp) => {
rsp.sendFile(__dirname + '/views/admin.html');
});
app.get('/admin/users', (req, rsp) => {
identityServices.adminGetUsers(req,rsp);
});
app.get('/admin/registrations', (req, rsp) => {
identityServices.adminGetRegistrations(req,rsp);
});
app.post('/admin/deleteRegistration', (req, rsp) => {
identityServices.adminDeleteRegistration(req,rsp);
});
// the photo verifier page
app.post('/photo_verifier', (req, rsp) => {
identityServices.photoVerifier(req,rsp);
});
// listen for requests
if (process.env.LOCAL_SSL_SERVER == "true") {
https.createServer({
key: fs.readFileSync('./server.key'),
cert: fs.readFileSync('./server.cert')
}, app)
.listen(process.env.LOCAL_SSL_PORT, function() {
console.log('Your SSL app is listening on port ' + process.env.LOCAL_SSL_PORT);
});
} else {
const listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});
}