@@ -38,15 +38,15 @@ const app = express();
38
38
39
39
app . use ( morgan ( 'tiny' ) ) ;
40
40
41
- let enf : Enforcer ;
41
+ let authEnforcer : Enforcer ;
42
42
43
- // we use the postgres adapter to store the casbin policies
44
- // we initalize the adapter with the connection string and the migrate option
43
+ // we use the postgres adapter to store the Casbin policies
44
+ // we initialize the adapter with the connection string and the migrate option
45
45
// the connection string has the following format:
46
46
// postgres://username:password@host :port/database
47
47
// the migrate option is used to create the tables if they don't exist, we set it to false because we create the tables manually
48
- async function initEnf ( ) {
49
- const a = await SequelizeAdapter . newAdapter ( {
48
+ async function initEnforcer ( ) {
49
+ const dbAdapter = await SequelizeAdapter . newAdapter ( {
50
50
dialect : 'postgres' ,
51
51
host : process . env . DATABASE_HOST ,
52
52
port : parseInt ( process . env . DATABASE_PORT || '5432' , 10 ) ,
@@ -55,15 +55,14 @@ async function initEnf() {
55
55
database : 'casbin' ,
56
56
} ) ;
57
57
58
- const enforcerLoading = newEnforcer ( 'model.conf' , a ) ;
59
- return enforcerLoading ;
58
+ return newEnforcer ( 'model.conf' , dbAdapter ) ;
60
59
}
61
- const port = process . env . PORT || 5000 ;
62
60
63
- Promise . all ( [ initEnf ( ) ] )
64
- . then ( ( res ) => {
65
- [ enf ] = res ;
66
- console . log ( `🛡 Casbin loaded` ) ;
61
+ const port = process . env . PORT || 5000 ;
62
+ Promise . all ( [ initEnforcer ( ) ] )
63
+ . then ( ( createdEnforcer ) => {
64
+ [ authEnforcer ] = createdEnforcer ;
65
+ console . log ( `🛡 Casbin authorization service loaded` ) ;
67
66
app . listen ( port ) ;
68
67
console . log ( `🚀 App is listening on port ${ port } ` ) ;
69
68
} )
@@ -72,7 +71,7 @@ Promise.all([initEnf()])
72
71
} ) ;
73
72
74
73
function isAuthorized ( sciper : number | undefined , subject : string , action : string ) : boolean {
75
- return enf . enforceSync ( sciper , subject , action ) ;
74
+ return authEnforcer . enforceSync ( sciper , subject , action ) ;
76
75
}
77
76
78
77
declare module 'express-session' {
@@ -151,22 +150,22 @@ app.get('/api/control_key', (req, res) => {
151
150
152
151
axios
153
152
. post ( 'https://tequila.epfl.ch/cgi-bin/tequila/fetchattributes' , body )
154
- . then ( ( resa ) => {
155
- if ( ! resa . data . includes ( 'status=ok' ) ) {
153
+ . then ( ( response ) => {
154
+ if ( ! response . data . includes ( 'status=ok' ) ) {
156
155
throw new Error ( 'Login did not work' ) ;
157
156
}
158
157
159
- const sciper = resa . data . split ( 'uniqueid=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
160
- const lastname = resa . data . split ( '\nname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
161
- const firstname = resa . data . split ( '\nfirstname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
158
+ const sciper = response . data . split ( 'uniqueid=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
159
+ const lastname = response . data . split ( '\nname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
160
+ const firstname = response . data . split ( '\nfirstname=' ) [ 1 ] . split ( '\n' ) [ 0 ] ;
162
161
163
162
req . session . userid = parseInt ( sciper , 10 ) ;
164
163
req . session . lastname = lastname ;
165
164
req . session . firstname = firstname ;
166
165
167
- const a = sciper2sess . get ( req . session . userid ) || new Set < string > ( ) ;
168
- a . add ( req . sessionID ) ;
169
- sciper2sess . set ( sciper , a ) ;
166
+ const sciperSessions = sciper2sess . get ( req . session . userid ) || new Set < string > ( ) ;
167
+ sciperSessions . add ( req . sessionID ) ;
168
+ sciper2sess . set ( sciper , sciperSessions ) ;
170
169
171
170
res . redirect ( '/logged' ) ;
172
171
} )
@@ -176,7 +175,7 @@ app.get('/api/control_key', (req, res) => {
176
175
} ) ;
177
176
} ) ;
178
177
179
- // This endpoint serves to logout from the app by clearing the session.
178
+ // This endpoint serves to log out from the app by clearing the session.
180
179
app . post ( '/api/logout' , ( req , res ) => {
181
180
if ( req . session . userid === undefined ) {
182
181
res . status ( 400 ) . send ( 'not logged in' ) ;
@@ -200,40 +199,39 @@ app.post('/api/logout', (req, res) => {
200
199
// list[0] contains the policies so list[i][0] is the sciper
201
200
// list[i][1] is the subject and list[i][2] is the action
202
201
function setMapAuthorization ( list : string [ ] [ ] ) : Map < String , Array < String > > {
203
- const m = new Map < String , Array < String > > ( ) ;
202
+ const userRights = new Map < String , Array < String > > ( ) ;
204
203
for ( let i = 0 ; i < list . length ; i += 1 ) {
205
204
const subject = list [ i ] [ 1 ] ;
206
205
const action = list [ i ] [ 2 ] ;
207
- if ( m . has ( subject ) ) {
208
- m . get ( subject ) ?. push ( action ) ;
206
+ if ( userRights . has ( subject ) ) {
207
+ userRights . get ( subject ) ?. push ( action ) ;
209
208
} else {
210
- m . set ( subject , [ action ] ) ;
209
+ userRights . set ( subject , [ action ] ) ;
211
210
}
212
211
}
213
- console . log ( m ) ;
214
- return m ;
212
+ console . log ( userRights ) ;
213
+ return userRights ;
215
214
}
216
215
217
- // As the user is logged on the app via this express but must also be logged in
218
- // the react. This endpoint serves to send to the client (actually to react)
216
+ // As the user is logged on the app via this express but must also
217
+ // be logged into react. This endpoint serves to send to the client (actually to react)
219
218
// the information of the current user.
220
219
app . get ( '/api/personal_info' , ( req , res ) => {
221
- enf . getFilteredPolicy ( 0 , String ( req . session . userid ) ) . then ( ( list ) => {
220
+ authEnforcer . getFilteredPolicy ( 0 , String ( req . session . userid ) ) . then ( ( AuthRights ) => {
222
221
res . set ( 'Access-Control-Allow-Origin' , '*' ) ;
223
222
if ( req . session . userid ) {
224
223
res . json ( {
225
224
sciper : req . session . userid ,
226
225
lastname : req . session . lastname ,
227
226
firstname : req . session . firstname ,
228
227
islogged : true ,
229
- authorization : Object . fromEntries ( setMapAuthorization ( list ) ) ,
228
+ authorization : Object . fromEntries ( setMapAuthorization ( AuthRights ) ) ,
230
229
} ) ;
231
230
} else {
232
231
res . json ( {
233
232
sciper : 0 ,
234
233
lastname : '' ,
235
234
firstname : '' ,
236
-
237
235
islogged : false ,
238
236
authorization : { } ,
239
237
} ) ;
@@ -244,7 +242,7 @@ app.get('/api/personal_info', (req, res) => {
244
242
// ---
245
243
// Users role
246
244
// ---
247
- // This call allow a user that is admin to get the list of the people that have
245
+ // This call allows a user that is admin to get the list of the people that have
248
246
// a special role (not a voter).
249
247
app . get ( '/api/user_rights' , ( req , res ) => {
250
248
if ( ! isAuthorized ( req . session . userid , SUBJECT_ROLES , ACTION_LIST ) ) {
@@ -425,12 +423,10 @@ function getPayload(dataStr: string) {
425
423
426
424
const sign = kyber . sign . schnorr . sign ( edCurve , scalar , hash ) ;
427
425
428
- const payload = {
426
+ return {
429
427
Payload : dataStrB64 ,
430
428
Signature : sign . toString ( 'hex' ) ,
431
429
} ;
432
-
433
- return payload ;
434
430
}
435
431
436
432
// sendToDela signs the message and sends it to the dela proxy. It makes no
@@ -493,6 +489,7 @@ function sendToDela(dataStr: string, req: express.Request, res: express.Response
493
489
if ( error . response ) {
494
490
resp = JSON . stringify ( error . response . data ) ;
495
491
}
492
+ console . log ( error ) ;
496
493
497
494
res
498
495
. status ( 500 )
@@ -507,7 +504,7 @@ app.put('/api/evoting/authorizations', (req, res) => {
507
504
return ;
508
505
}
509
506
const { FormID } = req . body ;
510
- enf . addPolicy ( String ( req . session . userid ) , FormID , ACTION_OWN ) ;
507
+ authEnforcer . addPolicy ( String ( req . session . userid ) , FormID , ACTION_OWN ) ;
511
508
} ) ;
512
509
513
510
// https://stackoverflow.com/a/1349426
@@ -598,13 +595,13 @@ app.delete('/api/evoting/forms/:formID', (req, res) => {
598
595
. status ( 500 )
599
596
. send ( `failed to proxy request: ${ req . method } ${ uri } - ${ error . message } - ${ resp } ` ) ;
600
597
} ) ;
601
- enf . removePolicy ( String ( req . session . userid ) , formID , ACTION_OWN ) ;
598
+ authEnforcer . removePolicy ( String ( req . session . userid ) , formID , ACTION_OWN ) ;
602
599
} ) ;
603
600
604
601
// This API call is used redirect all the calls for DELA to the DELAs nodes.
605
602
// During this process the data are processed : the user is authenticated and
606
- // controlled. Once this is done the data are signed before the are sent to the
607
- // DELA node To make this work, react has to redirect to this backend all the
603
+ // controlled. Once this is done the data are signed before it's sent to the
604
+ // DELA node To make this work, React has to redirect to this backend all the
608
605
// request that needs to go the DELA nodes
609
606
app . use ( '/api/evoting/*' , ( req , res ) => {
610
607
if ( ! req . session . userid ) {
0 commit comments