Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [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 | */ |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 18 | import argon2 from 'argon2'; |
Misha Krieger-Raynauld | 708a963 | 2022-10-14 22:55:59 -0400 | [diff] [blame] | 19 | import { Router } from 'express'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 20 | import asyncHandler from 'express-async-handler'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 21 | import { ParamsDictionary, Request } from 'express-serve-static-core'; |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 22 | import { HttpStatusCode } from 'jami-web-common'; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 23 | import { Container } from 'typedi'; |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 24 | |
Misha Krieger-Raynauld | addd6fe | 2022-10-22 12:46:04 -0400 | [diff] [blame] | 25 | import { Jamid } from '../jamid/jamid.js'; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 26 | import { Accounts } from '../storage/accounts.js'; |
| 27 | import { signJwt } from '../utils/jwt.js'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 28 | |
| 29 | interface Credentials { |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 30 | username: string; |
| 31 | password: string; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 32 | } |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 33 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 34 | const jamid = Container.get(Jamid); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 35 | const accounts = Container.get(Accounts); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 36 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 37 | export const authRouter = Router(); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 38 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 39 | authRouter.post( |
| 40 | '/new-account', |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 41 | asyncHandler(async (req: Request<ParamsDictionary, string, Partial<Credentials>>, res, _next) => { |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 42 | const { username, password } = req.body; |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 43 | if (username === undefined || password === undefined) { |
| 44 | res.status(HttpStatusCode.BadRequest).send('Missing username or password in body'); |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | if (password === '') { |
| 49 | res.status(HttpStatusCode.BadRequest).send('Password may not be empty'); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 50 | return; |
| 51 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 52 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 53 | const hashedPassword = await argon2.hash(password, { type: argon2.argon2id }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 54 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 55 | // TODO: add JAMS support |
| 56 | // managerUri: 'https://jams.savoirfairelinux.com', |
| 57 | // managerUsername: data.username, |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 58 | const accountId = await jamid.addAccount(new Map()); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 59 | |
Misha Krieger-Raynauld | 8a381da | 2022-11-03 17:37:51 -0400 | [diff] [blame] | 60 | const state = await jamid.registerUsername(accountId, username, ''); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 61 | if (state !== 0) { |
Misha Krieger-Raynauld | b6f1c32 | 2022-10-23 20:42:57 -0400 | [diff] [blame] | 62 | jamid.removeAccount(accountId); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 63 | if (state === 2) { |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 64 | res.status(HttpStatusCode.BadRequest).send('Invalid username or password'); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 65 | } else if (state === 3) { |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 66 | res.status(HttpStatusCode.Conflict).send('Username already exists'); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 67 | } else { |
| 68 | throw new Error(`Unhandled state ${state}`); |
| 69 | } |
| 70 | return; |
| 71 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 72 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 73 | accounts.set(username, hashedPassword); |
| 74 | await accounts.save(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 75 | |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 76 | res.sendStatus(HttpStatusCode.Created); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 77 | }) |
| 78 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 79 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 80 | authRouter.post( |
| 81 | '/login', |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 82 | asyncHandler( |
| 83 | async (req: Request<ParamsDictionary, { accessToken: string } | string, Partial<Credentials>>, res, _next) => { |
| 84 | const { username, password } = req.body; |
| 85 | if (username === undefined || password === undefined) { |
| 86 | res.status(HttpStatusCode.BadRequest).send('Missing username or password in body'); |
| 87 | return; |
| 88 | } |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 89 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 90 | // Check if the account is stored stored on this daemon instance |
| 91 | const accountId = jamid.getAccountIdFromUsername(username); |
| 92 | if (accountId === undefined) { |
| 93 | res.status(HttpStatusCode.NotFound).send('Username not found'); |
| 94 | return; |
| 95 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 96 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 97 | const hashedPassword = accounts.get(username); |
| 98 | if (hashedPassword === undefined) { |
| 99 | res |
| 100 | .status(HttpStatusCode.NotFound) |
| 101 | .send('Password not found (the account does not have a password set on the server)'); |
| 102 | return; |
| 103 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 104 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 105 | const isPasswordVerified = await argon2.verify(hashedPassword, password); |
| 106 | if (!isPasswordVerified) { |
| 107 | res.status(HttpStatusCode.Unauthorized).send('Incorrect password'); |
| 108 | return; |
| 109 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 110 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 111 | const jwt = await signJwt(accountId); |
| 112 | res.send({ accessToken: jwt }); |
| 113 | } |
| 114 | ) |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 115 | ); |