|
| 1 | +import express from 'express'; |
| 2 | +import axios, { AxiosError } from 'axios'; |
| 3 | +import { sciper2sess } from '../session'; |
| 4 | +import { getUserPermissions, setMapAuthorization } from '../authManager'; |
| 5 | + |
| 6 | +export const authenticationRouter = express.Router(); |
| 7 | + |
| 8 | +// This is via this endpoint that the client request the tequila key, this key |
| 9 | +// will then be used for redirection on the tequila server |
| 10 | +authenticationRouter.get('/get_teq_key', (req, res) => { |
| 11 | + axios |
| 12 | + .get(`https://tequila.epfl.ch/cgi-bin/tequila/createrequest`, { |
| 13 | + params: { |
| 14 | + urlaccess: `${process.env.FRONT_END_URL}/api/control_key`, |
| 15 | + service: 'Evoting', |
| 16 | + request: 'name,firstname,email,uniqueid,allunits', |
| 17 | + }, |
| 18 | + }) |
| 19 | + .then((response) => { |
| 20 | + console.info(`[tequila Key] Received response from tequila: ${response.data}`); |
| 21 | + const key = response.data.split('\n')[0].split('=')[1]; |
| 22 | + const url = `https://tequila.epfl.ch/cgi-bin/tequila/requestauth?requestkey=${key}`; |
| 23 | + res.json({ url: url }); |
| 24 | + }) |
| 25 | + .catch((error: AxiosError) => { |
| 26 | + console.log('message:', error.message); |
| 27 | + res.status(500).send(`failed to request Tequila authentication: ${error.message}`); |
| 28 | + }); |
| 29 | +}); |
| 30 | + |
| 31 | +// Here the client will send the key he/she received from the tequila, it is |
| 32 | +// then verified on the tequila. If the key is valid, the user is then logged |
| 33 | +// in the website through this backend |
| 34 | +authenticationRouter.get('/control_key', (req, res) => { |
| 35 | + const userKey = req.query.key; |
| 36 | + const body = `key=${userKey}`; |
| 37 | + |
| 38 | + axios |
| 39 | + .post('https://tequila.epfl.ch/cgi-bin/tequila/fetchattributes', body) |
| 40 | + .then((response) => { |
| 41 | + if (!response.data.includes('status=ok')) { |
| 42 | + throw new Error('Login did not work'); |
| 43 | + } |
| 44 | + |
| 45 | + const sciper = response.data.split('uniqueid=')[1].split('\n')[0]; |
| 46 | + const lastname = response.data.split('\nname=')[1].split('\n')[0]; |
| 47 | + const firstname = response.data.split('\nfirstname=')[1].split('\n')[0]; |
| 48 | + |
| 49 | + req.session.userId = parseInt(sciper, 10); |
| 50 | + req.session.lastName = lastname; |
| 51 | + req.session.firstName = firstname; |
| 52 | + |
| 53 | + const sciperSessions = sciper2sess.get(req.session.userId) || new Set<string>(); |
| 54 | + sciperSessions.add(req.sessionID); |
| 55 | + sciper2sess.set(sciper, sciperSessions); |
| 56 | + |
| 57 | + res.redirect('/logged'); |
| 58 | + }) |
| 59 | + .catch((error) => { |
| 60 | + res.status(500).send('Login did not work'); |
| 61 | + console.log(error); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +// This endpoint serves to log out from the app by clearing the session. |
| 66 | +authenticationRouter.post('/logout', (req, res) => { |
| 67 | + if (req.session.userId === undefined) { |
| 68 | + res.status(400).send('not logged in'); |
| 69 | + } |
| 70 | + |
| 71 | + const { userId } = req.session; |
| 72 | + |
| 73 | + req.session.destroy(() => { |
| 74 | + const a = sciper2sess.get(userId as number); |
| 75 | + if (a !== undefined) { |
| 76 | + a.delete(req.sessionID); |
| 77 | + sciper2sess.set(userId as number, a); |
| 78 | + } |
| 79 | + res.redirect('/'); |
| 80 | + }); |
| 81 | +}); |
| 82 | + |
| 83 | +// As the user is logged on the app via this express but must also |
| 84 | +// be logged into react. This endpoint serves to send to the client (actually to react) |
| 85 | +// the information of the current user. |
| 86 | +authenticationRouter.get('/personal_info', async (req, res) => { |
| 87 | + if (!req.session.userId) { |
| 88 | + res.status(401).send('Unauthenticated'); |
| 89 | + return; |
| 90 | + } |
| 91 | + const userPermissions = await getUserPermissions(req.session.userId); |
| 92 | + res.set('Access-Control-Allow-Origin', '*'); |
| 93 | + res.json({ |
| 94 | + sciper: req.session.userId, |
| 95 | + lastName: req.session.lastName, |
| 96 | + firstName: req.session.firstName, |
| 97 | + isLoggedIn: true, |
| 98 | + authorization: Object.fromEntries(setMapAuthorization(userPermissions)), |
| 99 | + }); |
| 100 | +}); |
0 commit comments