-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
244 lines (198 loc) · 6.61 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
// 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 oidcClient = require('openid-client');
const tm = require('./oauthtokenmanager.js');
const identityServices = require('./ciservices.js');
const app = express();
// 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('public'));
// For OIDC login
app.use(passport.initialize());
app.use(passport.session());
// check we can get OIDC information, and if we can, include that as a login option
oidcClient.Issuer.discover(process.env.CI_TENANT_ENDPOINT + "/oidc/endpoint/default/.well-known/openid-configuration")
.then((isvIssuer) => {
//console.log('isvIssuer.metadata: ' + JSON.stringify(isvIssuer.metadata));
let myClient = new isvIssuer.Client({
client_id: process.env.OIDC_CLIENT_ID,
client_secret: process.env.OIDC_CLIENT_SECRET,
redirect_uris: [ "https://"+process.env.RPID+ (process.env.LOCAL_SSL_SERVER == "true" ? (":"+process.env.LOCAL_SSL_PORT) : "") + "/callback" ],
response_types: ['code']
});
passport.use("oidc", new oidcClient.Strategy(
// see https://github.com/panva/node-openid-client/blob/main/docs/README.md#strategy
{
client: myClient,
params: {
scope: "openid profile"
},
passReqToCallback: false,
usePKCE: true
},
(tokenSet, userinfo, done) => {
var data = {
tokenSet: tokenSet,
userinfo: userinfo
};
//console.log("OIDC callback function called with: " + JSON.stringify(data));
return done(null, data);
})
);
}).then(() => {
// setup the login URL
app.use("/loginoidc", passport.authenticate("oidc"));
}).then(() => {
// set up the OIDC callback URL
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.userinfo.preferred_username;
req.session.userDisplayName = req.user.userinfo.displayName;
req.session.userSCIMId = req.user.userinfo.sub;
req.session.tokenResponse = {
expires_at_ms: (req.user.tokenSet.expires_at * 1000),
expires_in: Math.round(((new Date(req.user.tokenSet.expires_at*1000)).getTime() - (new Date()).getTime())/1000),
refresh_token: req.user.tokenSet.refresh_token,
access_token: req.user.tokenSet.access_token
};
res.redirect('/');
}
);
});
passport.serializeUser((user, next) => {
next(null, user);
});
passport.deserializeUser((obj, next) => {
next(null, obj);
});
//console.log(process.env);
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', (req, rsp) => {
rsp.sendFile(__dirname + '/views/index.html');
});
app.post('/login', (req, rsp) => {
// make sure we switch to the client_credentials OAuth client
identityServices.validateUsernamePassword(req, rsp);
});
app.get('/error', (req,rsp) => {
rsp.sendFile(__dirname + '/views/error.html');
});
app.get('/test', (req,rsp) => {
identityServices.testButton(req, rsp);
});
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('/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);
});
/*
* Start section of URLs used by the android app
*/
app.get('/.well-known/assetlinks.json', (req, rsp) => {
identityServices.androidAssetLinks(req, rsp);
});
app.post('/auth/username', (req, rsp) => {
identityServices.androidUsername(req, rsp);
});
app.post('/auth/password', (req, rsp) => {
identityServices.androidPassword(req, rsp);
});
app.post('/auth/getKeys', (req, rsp) => {
identityServices.androidGetKeys(req, rsp);
});
app.post('/auth/registerRequest', (req, rsp) => {
identityServices.androidRegisterRequest(req, rsp);
});
app.post('/auth/registerResponse', (req, rsp) => {
identityServices.androidRegisterResponse(req, rsp);
});
app.post('/auth/removeKey', (req, rsp) => {
identityServices.androidRemoveKey(req, rsp);
});
app.post('/auth/signinRequest', (req, rsp) => {
identityServices.androidSigninRequest(req, rsp);
});
app.post('/auth/signinResponse', (req, rsp) => {
identityServices.androidSigninResponse(req, rsp);
});
/*
* End section of URLs used by the android app
*/
/*
* Start section of URLs used by the FIDO2App
*/
app.get('/ivcreds', (req, rsp) => {
identityServices.fido2appIVCredsResponse(req, rsp);
});
app.post(process.env.FIDO2APP_URLPREFIX +'/attestation/options', (req, rsp) => {
identityServices.fido2appAttestationOptions(req, rsp);
});
app.post(process.env.FIDO2APP_URLPREFIX +'/attestation/result', (req, rsp) => {
identityServices.fido2appAttestationResult(req, rsp);
});
app.post(process.env.FIDO2APP_URLPREFIX +'/assertion/options', (req, rsp) => {
identityServices.fido2appAssertionOptions(req, rsp);
});
app.post(process.env.FIDO2APP_URLPREFIX +'/assertion/result', (req, rsp) => {
identityServices.fido2appAssertionResult(req, rsp);
});
/*
* End section of URLs used by the FIDO2App
*/
// listen for requests
if (process.env.LOCAL_SSL_SERVER == "true") {
https.createServer({
key: fs.readFileSync('./cifido2rp.key.pem'),
cert: fs.readFileSync('./cifido2rp.crt.pem')
}, 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);
});
}