Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2022 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU Affero General Public License as |
| 6 | * published by the Free Software Foundation; either version 3 of the |
| 7 | * License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU Affero General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Affero General Public |
| 15 | * License along with this program. If not, see |
| 16 | * <https://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | import argon2 from 'argon2'; |
| 19 | import { Router } from 'express'; |
| 20 | import asyncHandler from 'express-async-handler'; |
| 21 | import { ParamsDictionary, Request } from 'express-serve-static-core'; |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 22 | import { AccessToken, AdminCredentials, HttpStatusCode } from 'jami-web-common'; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 23 | import { Container } from 'typedi'; |
| 24 | |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 25 | import { checkAdminSetup } from '../middleware/setup.js'; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 26 | import { AdminAccount } from '../storage/admin-account.js'; |
| 27 | import { signJwt } from '../utils/jwt.js'; |
| 28 | |
| 29 | const adminAccount = Container.get(AdminAccount); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 30 | |
| 31 | export const setupRouter = Router(); |
| 32 | |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 33 | setupRouter.get('/check', (_req, res) => { |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 34 | const isSetupComplete = adminAccount.get() !== undefined; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 35 | res.send({ isSetupComplete }); |
| 36 | }); |
| 37 | |
| 38 | setupRouter.post( |
| 39 | '/admin/create', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 40 | asyncHandler(async (req: Request<ParamsDictionary, string, Partial<AdminCredentials>>, res) => { |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 41 | const { password } = req.body; |
| 42 | if (password === undefined) { |
| 43 | res.status(HttpStatusCode.BadRequest).send('Missing password in body'); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 44 | return; |
| 45 | } |
| 46 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 47 | if (password === '') { |
| 48 | res.status(HttpStatusCode.BadRequest).send('Password may not be empty'); |
| 49 | return; |
| 50 | } |
| 51 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 52 | const isAdminCreated = adminAccount.get() !== undefined; |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 53 | if (isAdminCreated) { |
| 54 | res.status(HttpStatusCode.Conflict).send('Admin already exists'); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 55 | return; |
| 56 | } |
| 57 | |
| 58 | const hashedPassword = await argon2.hash(password, { type: argon2.argon2id }); |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 59 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 60 | adminAccount.set(hashedPassword); |
| 61 | await adminAccount.save(); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 62 | |
| 63 | res.sendStatus(HttpStatusCode.Created); |
| 64 | }) |
| 65 | ); |
| 66 | |
| 67 | // Every request handler after this line will be submitted to this middleware |
| 68 | // in order to ensure that the admin account is set up before proceeding with |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 69 | // setup-related requests |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 70 | setupRouter.use(checkAdminSetup); |
| 71 | |
| 72 | setupRouter.post( |
| 73 | '/admin/login', |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 74 | asyncHandler(async (req: Request<ParamsDictionary, AccessToken | string, Partial<AdminCredentials>>, res) => { |
| 75 | const { password } = req.body; |
| 76 | if (password === undefined) { |
| 77 | res.status(HttpStatusCode.BadRequest).send('Missing password in body'); |
| 78 | return; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 79 | } |
Misha Krieger-Raynauld | cfa4430 | 2022-11-30 18:36:36 -0500 | [diff] [blame] | 80 | |
| 81 | const hashedPassword = adminAccount.get(); |
| 82 | if (hashedPassword === undefined) { |
| 83 | res.status(HttpStatusCode.InternalServerError).send('Admin password not found'); |
| 84 | return; |
| 85 | } |
| 86 | |
| 87 | const isPasswordVerified = await argon2.verify(hashedPassword, password); |
| 88 | if (!isPasswordVerified) { |
| 89 | res.status(HttpStatusCode.Forbidden).send('Incorrect password'); |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | const jwt = await signJwt('admin'); |
| 94 | res.send({ accessToken: jwt }); |
| 95 | }) |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 96 | ); |