| 18 | import { group } from '../config/models/group.js'; |
| 19 | |
| 20 | export default function authRouter(envConfig, passport) { |
| 21 | router.route('/logout') |
| 22 | .get(function (req, res) { |
| 23 | if (req.session) { |
| 24 | req.session.destroy(); |
| 25 | } |
| 26 | res.writeHead(302, { |
| 27 | 'Location': '/logout', |
| 28 | }); |
| 29 | res.end(); |
| 30 | }); |
| 31 | |
| 32 | router.route('/login') |
| 33 | /** |
| 34 | * When in production mode, the "Sign In" button does a GET request to /auth/login. |
| 35 | * The GET request includes a returnPath parameter which is saved in the session |
| 36 | * in order to return the user to their original page after authentication is complete. |
| 37 | */ |
| 38 | .get(function (req, res) { |
| 39 | req.session.returnPath = req.query.returnPath; |
| 40 | if (envConfig.state === 'development') { |
| 41 | res.writeHead(302, { |
| 42 | 'Location': '/login' |
| 43 | }); |
| 44 | } else { |
| 45 | res.writeHead(302, { |
| 46 | 'Location': envConfig.sso_url, |
| 47 | }); |
| 48 | } |
| 49 | res.end(); |
| 50 | }) |
| 51 | /** |
| 52 | * When in development mode, the "Sign In" button does a POST request with the username |
| 53 | * and password to /auth/login. Therefore, the code assumes that a POST is an attempt to |
| 54 | * authenticate against the local credentials set in the config parameters. In local mode, |
| 55 | * the default user is given admin privileges. This code sets up the session in the same |
| 56 | * manner as the passport workflow for consistency. |
| 57 | */ |
| 58 | .post(function (req, res) { |
| 59 | /* |
| 60 | * This section only applies when run in development mode. |
| 61 | */ |
| 62 | if (envConfig.state === 'production' || envConfig.state === 'stage') { |
| 63 | res.status(500).json({ |
| 64 | 'message': 'Local authentication is only supported in development mode.', |
| 65 | }); |
| 66 | return; |
| 67 | } |
| 68 | if (req.body.username === 'marinus' && |
| 69 | req.body.password === envConfig.localAdminPassword) { |
| 70 | let sess = req.session; |
| 71 | sess.passport = { |
| 72 | 'user': { |
| 73 | 'firstName': 'Administrator', |
| 74 | 'userid': 'marinus', |
| 75 | }, |
| 76 | }; |
| 77 | sess.localAuth = true; |