@@ -31,12 +31,11 @@ const PERMISSIONS = {
31
31
} ,
32
32
} ;
33
33
34
- // store is used to store the session
35
- const store = new MemoryStore ( {
34
+ const sessionStore = new MemoryStore ( {
36
35
checkPeriod : 86400000 , // prune expired entries every 24h
37
36
} ) ;
38
37
39
- // Keeps an in-memory mapping between a SCIPER (userid ) and its opened session
38
+ // Keeps an in-memory mapping between a SCIPER (userId ) and its opened session
40
39
// IDs. Needed to invalidate the sessions of a user when its role changes. The
41
40
// value is a set of sessions IDs.
42
41
const sciper2sess = new Map < number , Set < string > > ( ) ;
@@ -65,13 +64,13 @@ async function initEnforcer() {
65
64
return newEnforcer ( 'src/model.conf' , dbAdapter ) ;
66
65
}
67
66
68
- const port = process . env . PORT || 5000 ;
67
+ const serveOnPort = process . env . PORT || 5000 ;
69
68
Promise . all ( [ initEnforcer ( ) ] )
70
69
. then ( ( createdEnforcer ) => {
71
70
[ authEnforcer ] = createdEnforcer ;
72
71
console . log ( `🛡 Casbin authorization service loaded` ) ;
73
- app . listen ( port ) ;
74
- console . log ( `🚀 App is listening on port ${ port } ` ) ;
72
+ app . listen ( serveOnPort ) ;
73
+ console . log ( `🚀 App is listening on port ${ serveOnPort } ` ) ;
75
74
} )
76
75
. catch ( ( err ) => {
77
76
console . error ( '❌ failed to start:' , err ) ;
@@ -82,10 +81,11 @@ function isAuthorized(sciper: number | undefined, subject: string, action: strin
82
81
}
83
82
84
83
declare module 'express-session' {
84
+ // This overrides express-session
85
85
export interface SessionData {
86
- userid : number ;
87
- firstname : string ;
88
- lastname : string ;
86
+ userId : number ;
87
+ firstName : string ;
88
+ lastName : string ;
89
89
}
90
90
}
91
91
@@ -100,7 +100,7 @@ app.use(
100
100
saveUninitialized : true ,
101
101
cookie : { maxAge : oneDay } ,
102
102
resave : false ,
103
- store : store ,
103
+ store : sessionStore ,
104
104
} )
105
105
) ;
106
106
@@ -113,7 +113,7 @@ app.use(express.urlencoded({ extended: true }));
113
113
// app.use((req, res, next) => {
114
114
// const begin = req.url.split('?')[0];
115
115
// let role = 'everyone';
116
- // if (req.session.userid && req.session.role) {
116
+ // if (req.session.userId && req.session.role) {
117
117
// role = req.session.role;
118
118
// }
119
119
@@ -166,11 +166,11 @@ app.get('/api/control_key', (req, res) => {
166
166
const lastname = response . data . split ( '\nname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
167
167
const firstname = response . data . split ( '\nfirstname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
168
168
169
- req . session . userid = parseInt ( sciper , 10 ) ;
170
- req . session . lastname = lastname ;
171
- req . session . firstname = firstname ;
169
+ req . session . userId = parseInt ( sciper , 10 ) ;
170
+ req . session . lastName = lastname ;
171
+ req . session . firstName = firstname ;
172
172
173
- const sciperSessions = sciper2sess . get ( req . session . userid ) || new Set < string > ( ) ;
173
+ const sciperSessions = sciper2sess . get ( req . session . userId ) || new Set < string > ( ) ;
174
174
sciperSessions . add ( req . sessionID ) ;
175
175
sciper2sess . set ( sciper , sciperSessions ) ;
176
176
@@ -184,17 +184,17 @@ app.get('/api/control_key', (req, res) => {
184
184
185
185
// This endpoint serves to log out from the app by clearing the session.
186
186
app . post ( '/api/logout' , ( req , res ) => {
187
- if ( req . session . userid === undefined ) {
187
+ if ( req . session . userId === undefined ) {
188
188
res . status ( 400 ) . send ( 'not logged in' ) ;
189
189
}
190
190
191
- const { userid } = req . session ;
191
+ const { userId } = req . session ;
192
192
193
193
req . session . destroy ( ( ) => {
194
- const a = sciper2sess . get ( userid as number ) ;
194
+ const a = sciper2sess . get ( userId as number ) ;
195
195
if ( a !== undefined ) {
196
196
a . delete ( req . sessionID ) ;
197
- sciper2sess . set ( userid as number , a ) ;
197
+ sciper2sess . set ( userId as number , a ) ;
198
198
}
199
199
res . redirect ( '/' ) ;
200
200
} ) ;
@@ -224,24 +224,18 @@ function setMapAuthorization(list: string[][]): Map<String, Array<String>> {
224
224
// be logged into react. This endpoint serves to send to the client (actually to react)
225
225
// the information of the current user.
226
226
app . get ( '/api/personal_info' , ( req , res ) => {
227
- authEnforcer . getFilteredPolicy ( 0 , String ( req . session . userid ) ) . then ( ( AuthRights ) => {
227
+ authEnforcer . getFilteredPolicy ( 0 , String ( req . session . userId ) ) . then ( ( AuthRights ) => {
228
228
res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
229
- if ( req . session . userid ) {
229
+ if ( req . session . userId ) {
230
230
res . json ( {
231
- sciper : req . session . userid ,
232
- lastname : req . session . lastname ,
233
- firstname : req . session . firstname ,
234
- islogged : true ,
231
+ sciper : req . session . userId ,
232
+ lastName : req . session . lastName ,
233
+ firstName : req . session . firstName ,
234
+ isLoggedIn : true ,
235
235
authorization : Object . fromEntries ( setMapAuthorization ( AuthRights ) ) ,
236
236
} ) ;
237
237
} else {
238
- res . json ( {
239
- sciper : 0 ,
240
- lastname : '' ,
241
- firstname : '' ,
242
- islogged : false ,
243
- authorization : { } ,
244
- } ) ;
238
+ res . status ( 401 ) . send ( ) ;
245
239
}
246
240
} ) ;
247
241
} ) ;
@@ -252,7 +246,7 @@ app.get('/api/personal_info', (req, res) => {
252
246
// This call allows a user that is admin to get the list of the people that have
253
247
// a special role (not a voter).
254
248
app . get ( '/api/user_rights' , ( req , res ) => {
255
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . LIST ) ) {
249
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . LIST ) ) {
256
250
res . status ( 400 ) . send ( 'Unauthorized - only admins allowed' ) ;
257
251
return ;
258
252
}
@@ -266,7 +260,7 @@ app.get('/api/user_rights', (req, res) => {
266
260
267
261
// This call (only for admins) allow an admin to add a role to a voter.
268
262
app . post ( '/api/add_role' , ( req , res , next ) => {
269
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . ADD ) ) {
263
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . ADD ) ) {
270
264
res . status ( 400 ) . send ( 'Unauthorized - only admins allowed' ) ;
271
265
return ;
272
266
}
@@ -286,7 +280,7 @@ app.post('/api/add_role', (req, res, next) => {
286
280
// This call (only for admins) allow an admin to remove a role to a user.
287
281
288
282
app . post ( '/api/remove_role' , ( req , res , next ) => {
289
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . REMOVE ) ) {
283
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . ROLES , PERMISSIONS . ACTIONS . REMOVE ) ) {
290
284
res . status ( 400 ) . send ( 'Unauthorized - only admins allowed' ) ;
291
285
return ;
292
286
}
@@ -302,7 +296,7 @@ app.post('/api/remove_role', (req, res, next) => {
302
296
// ---
303
297
const proxiesDB = lmdb . open < string , string > ( { path : `${ process . env . DB_PATH } proxies` } ) ;
304
298
app . post ( '/api/proxies' , ( req , res ) => {
305
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . POST ) ) {
299
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . POST ) ) {
306
300
res . status ( 400 ) . send ( 'Unauthorized - only admins and operators allowed' ) ;
307
301
return ;
308
302
}
@@ -317,7 +311,7 @@ app.post('/api/proxies', (req, res) => {
317
311
} ) ;
318
312
319
313
app . put ( '/api/proxies/:nodeAddr' , ( req , res ) => {
320
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . PUT ) ) {
314
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . PUT ) ) {
321
315
res . status ( 400 ) . send ( 'Unauthorized - only admins and operators allowed' ) ;
322
316
return ;
323
317
}
@@ -354,7 +348,7 @@ app.put('/api/proxies/:nodeAddr', (req, res) => {
354
348
} ) ;
355
349
356
350
app . delete ( '/api/proxies/:nodeAddr' , ( req , res ) => {
357
- if ( ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . DELETE ) ) {
351
+ if ( ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . PROXIES , PERMISSIONS . ACTIONS . DELETE ) ) {
358
352
res . status ( 400 ) . send ( 'Unauthorized - only admins and operators allowed' ) ;
359
353
return ;
360
354
}
@@ -507,13 +501,13 @@ function sendToDela(dataStr: string, req: express.Request, res: express.Response
507
501
// Secure /api/evoting to admins and operators
508
502
app . put ( '/api/evoting/authorizations' , ( req , res ) => {
509
503
if (
510
- ! isAuthorized ( req . session . userid , PERMISSIONS . SUBJECTS . ELECTION , PERMISSIONS . ACTIONS . CREATE )
504
+ ! isAuthorized ( req . session . userId , PERMISSIONS . SUBJECTS . ELECTION , PERMISSIONS . ACTIONS . CREATE )
511
505
) {
512
506
res . status ( 400 ) . send ( 'Unauthorized' ) ;
513
507
return ;
514
508
}
515
509
const { FormID } = req . body ;
516
- authEnforcer . addPolicy ( String ( req . session . userid ) , FormID , PERMISSIONS . ACTIONS . OWN ) ;
510
+ authEnforcer . addPolicy ( String ( req . session . userId ) , FormID , PERMISSIONS . ACTIONS . OWN ) ;
517
511
} ) ;
518
512
519
513
// https://stackoverflow.com/a/1349426
@@ -528,7 +522,7 @@ function makeid(length: number) {
528
522
}
529
523
app . put ( '/api/evoting/forms/:formID' , ( req , res , next ) => {
530
524
const { formID } = req . params ;
531
- if ( ! isAuthorized ( req . session . userid , formID , PERMISSIONS . ACTIONS . OWN ) ) {
525
+ if ( ! isAuthorized ( req . session . userId , formID , PERMISSIONS . ACTIONS . OWN ) ) {
532
526
res . status ( 400 ) . send ( 'Unauthorized' ) ;
533
527
return ;
534
528
}
@@ -537,7 +531,7 @@ app.put('/api/evoting/forms/:formID', (req, res, next) => {
537
531
538
532
app . post ( '/api/evoting/services/dkg/actors' , ( req , res , next ) => {
539
533
const { FormID } = req . body ;
540
- if ( ! isAuthorized ( req . session . userid , FormID , PERMISSIONS . ACTIONS . OWN ) ) {
534
+ if ( ! isAuthorized ( req . session . userId , FormID , PERMISSIONS . ACTIONS . OWN ) ) {
541
535
res . status ( 400 ) . send ( 'Unauthorized' ) ;
542
536
return ;
543
537
}
@@ -548,23 +542,23 @@ app.post('/api/evoting/services/dkg/actors', (req, res, next) => {
548
542
} ) ;
549
543
app . use ( '/api/evoting/services/dkg/actors/:formID' , ( req , res , next ) => {
550
544
const { formID } = req . params ;
551
- if ( ! isAuthorized ( req . session . userid , formID , PERMISSIONS . ACTIONS . OWN ) ) {
545
+ if ( ! isAuthorized ( req . session . userId , formID , PERMISSIONS . ACTIONS . OWN ) ) {
552
546
res . status ( 400 ) . send ( 'Unauthorized' ) ;
553
547
return ;
554
548
}
555
549
next ( ) ;
556
550
} ) ;
557
551
app . use ( '/api/evoting/services/shuffle/:formID' , ( req , res , next ) => {
558
552
const { formID } = req . params ;
559
- if ( ! isAuthorized ( req . session . userid , formID , PERMISSIONS . ACTIONS . OWN ) ) {
553
+ if ( ! isAuthorized ( req . session . userId , formID , PERMISSIONS . ACTIONS . OWN ) ) {
560
554
res . status ( 400 ) . send ( 'Unauthorized' ) ;
561
555
return ;
562
556
}
563
557
next ( ) ;
564
558
} ) ;
565
559
app . delete ( '/api/evoting/forms/:formID' , ( req , res ) => {
566
560
const { formID } = req . params ;
567
- if ( ! isAuthorized ( req . session . userid , formID , PERMISSIONS . ACTIONS . OWN ) ) {
561
+ if ( ! isAuthorized ( req . session . userId , formID , PERMISSIONS . ACTIONS . OWN ) ) {
568
562
res . status ( 400 ) . send ( 'Unauthorized' ) ;
569
563
return ;
570
564
}
@@ -604,7 +598,7 @@ app.delete('/api/evoting/forms/:formID', (req, res) => {
604
598
. status ( 500 )
605
599
. send ( `failed to proxy request: ${ req . method } ${ uri } - ${ error . message } - ${ resp } ` ) ;
606
600
} ) ;
607
- authEnforcer . removePolicy ( String ( req . session . userid ) , formID , PERMISSIONS . ACTIONS . OWN ) ;
601
+ authEnforcer . removePolicy ( String ( req . session . userId ) , formID , PERMISSIONS . ACTIONS . OWN ) ;
608
602
} ) ;
609
603
610
604
// This API call is used redirect all the calls for DELA to the DELAs nodes.
@@ -613,7 +607,7 @@ app.delete('/api/evoting/forms/:formID', (req, res) => {
613
607
// DELA node To make this work, React has to redirect to this backend all the
614
608
// request that needs to go the DELA nodes
615
609
app . use ( '/api/evoting/*' , ( req , res ) => {
616
- if ( ! req . session . userid ) {
610
+ if ( ! req . session . userId ) {
617
611
res . status ( 400 ) . send ( 'Unauthorized' ) ;
618
612
return ;
619
613
}
@@ -627,7 +621,7 @@ app.use('/api/evoting/*', (req, res) => {
627
621
// only needed to allow users to cast multiple ballots, where only the last
628
622
// ballot is taken into account. To preserve anonymity the web-backend could
629
623
// translate UserIDs to another random ID.
630
- // bodyData.UserID = req.session.userid .toString();
624
+ // bodyData.UserID = req.session.userId .toString();
631
625
bodyData . UserID = makeid ( 10 ) ;
632
626
}
633
627
0 commit comments