Skip to content

Commit ed8b81b

Browse files
authored
Merge pull request #290 from dedis/style/improves-readability-in-backend
style: renames variables, and applies linter
2 parents a4a47b0 + c157641 commit ed8b81b

File tree

1 file changed

+38
-41
lines changed

1 file changed

+38
-41
lines changed

web/backend/src/Server.ts

+38-41
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ const app = express();
3838

3939
app.use(morgan('tiny'));
4040

41-
let enf: Enforcer;
41+
let authEnforcer: Enforcer;
4242

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
4545
// the connection string has the following format:
4646
// postgres://username:password@host:port/database
4747
// 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({
5050
dialect: 'postgres',
5151
host: process.env.DATABASE_HOST,
5252
port: parseInt(process.env.DATABASE_PORT || '5432', 10),
@@ -55,15 +55,14 @@ async function initEnf() {
5555
database: 'casbin',
5656
});
5757

58-
const enforcerLoading = newEnforcer('model.conf', a);
59-
return enforcerLoading;
58+
return newEnforcer('model.conf', dbAdapter);
6059
}
61-
const port = process.env.PORT || 5000;
6260

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`);
6766
app.listen(port);
6867
console.log(`🚀 App is listening on port ${port}`);
6968
})
@@ -72,7 +71,7 @@ Promise.all([initEnf()])
7271
});
7372

7473
function isAuthorized(sciper: number | undefined, subject: string, action: string): boolean {
75-
return enf.enforceSync(sciper, subject, action);
74+
return authEnforcer.enforceSync(sciper, subject, action);
7675
}
7776

7877
declare module 'express-session' {
@@ -151,22 +150,22 @@ app.get('/api/control_key', (req, res) => {
151150

152151
axios
153152
.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')) {
156155
throw new Error('Login did not work');
157156
}
158157

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];
162161

163162
req.session.userid = parseInt(sciper, 10);
164163
req.session.lastname = lastname;
165164
req.session.firstname = firstname;
166165

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);
170169

171170
res.redirect('/logged');
172171
})
@@ -176,7 +175,7 @@ app.get('/api/control_key', (req, res) => {
176175
});
177176
});
178177

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.
180179
app.post('/api/logout', (req, res) => {
181180
if (req.session.userid === undefined) {
182181
res.status(400).send('not logged in');
@@ -200,40 +199,39 @@ app.post('/api/logout', (req, res) => {
200199
// list[0] contains the policies so list[i][0] is the sciper
201200
// list[i][1] is the subject and list[i][2] is the action
202201
function setMapAuthorization(list: string[][]): Map<String, Array<String>> {
203-
const m = new Map<String, Array<String>>();
202+
const userRights = new Map<String, Array<String>>();
204203
for (let i = 0; i < list.length; i += 1) {
205204
const subject = list[i][1];
206205
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);
209208
} else {
210-
m.set(subject, [action]);
209+
userRights.set(subject, [action]);
211210
}
212211
}
213-
console.log(m);
214-
return m;
212+
console.log(userRights);
213+
return userRights;
215214
}
216215

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)
219218
// the information of the current user.
220219
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) => {
222221
res.set('Access-Control-Allow-Origin', '*');
223222
if (req.session.userid) {
224223
res.json({
225224
sciper: req.session.userid,
226225
lastname: req.session.lastname,
227226
firstname: req.session.firstname,
228227
islogged: true,
229-
authorization: Object.fromEntries(setMapAuthorization(list)),
228+
authorization: Object.fromEntries(setMapAuthorization(AuthRights)),
230229
});
231230
} else {
232231
res.json({
233232
sciper: 0,
234233
lastname: '',
235234
firstname: '',
236-
237235
islogged: false,
238236
authorization: {},
239237
});
@@ -244,7 +242,7 @@ app.get('/api/personal_info', (req, res) => {
244242
// ---
245243
// Users role
246244
// ---
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
248246
// a special role (not a voter).
249247
app.get('/api/user_rights', (req, res) => {
250248
if (!isAuthorized(req.session.userid, SUBJECT_ROLES, ACTION_LIST)) {
@@ -425,12 +423,10 @@ function getPayload(dataStr: string) {
425423

426424
const sign = kyber.sign.schnorr.sign(edCurve, scalar, hash);
427425

428-
const payload = {
426+
return {
429427
Payload: dataStrB64,
430428
Signature: sign.toString('hex'),
431429
};
432-
433-
return payload;
434430
}
435431

436432
// 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
493489
if (error.response) {
494490
resp = JSON.stringify(error.response.data);
495491
}
492+
console.log(error);
496493

497494
res
498495
.status(500)
@@ -507,7 +504,7 @@ app.put('/api/evoting/authorizations', (req, res) => {
507504
return;
508505
}
509506
const { FormID } = req.body;
510-
enf.addPolicy(String(req.session.userid), FormID, ACTION_OWN);
507+
authEnforcer.addPolicy(String(req.session.userid), FormID, ACTION_OWN);
511508
});
512509

513510
// https://stackoverflow.com/a/1349426
@@ -598,13 +595,13 @@ app.delete('/api/evoting/forms/:formID', (req, res) => {
598595
.status(500)
599596
.send(`failed to proxy request: ${req.method} ${uri} - ${error.message} - ${resp}`);
600597
});
601-
enf.removePolicy(String(req.session.userid), formID, ACTION_OWN);
598+
authEnforcer.removePolicy(String(req.session.userid), formID, ACTION_OWN);
602599
});
603600

604601
// This API call is used redirect all the calls for DELA to the DELAs nodes.
605602
// 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
608605
// request that needs to go the DELA nodes
609606
app.use('/api/evoting/*', (req, res) => {
610607
if (!req.session.userid) {

0 commit comments

Comments
 (0)