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'; |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 22 | import { AccountDetails, 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; |
Gabriel Rochon | 7057b4f | 2022-11-21 13:28:01 -0500 | [diff] [blame] | 32 | isJams: boolean; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 33 | } |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 34 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 35 | const jamid = Container.get(Jamid); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 36 | const accounts = Container.get(Accounts); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 37 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 38 | export const authRouter = Router(); |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 39 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 40 | authRouter.post( |
| 41 | '/new-account', |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 42 | asyncHandler(async (req: Request<ParamsDictionary, string, Partial<Credentials>>, res, _next) => { |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 43 | const { username, password, isJams } = req.body; |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 44 | if (username === undefined || password === undefined) { |
| 45 | res.status(HttpStatusCode.BadRequest).send('Missing username or password in body'); |
| 46 | return; |
| 47 | } |
| 48 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 49 | const isAccountAlreadyCreated = accounts.get(username, isJams) !== undefined; |
| 50 | if (isAccountAlreadyCreated) { |
| 51 | res.status(HttpStatusCode.Conflict).send('Username already exists locally'); |
| 52 | return; |
| 53 | } |
| 54 | |
Misha Krieger-Raynauld | cb11bba | 2022-11-11 18:08:33 -0500 | [diff] [blame] | 55 | if (password === '') { |
| 56 | res.status(HttpStatusCode.BadRequest).send('Password may not be empty'); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 57 | return; |
| 58 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 59 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 60 | const hashedPassword = await argon2.hash(password, { type: argon2.argon2id }); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 61 | |
Issam E. Maghni | 5bc33a3 | 2022-11-22 21:36:19 +0000 | [diff] [blame] | 62 | const accountDetails: Partial<AccountDetails> = { |
| 63 | // TODO: enable encrypted archives |
| 64 | // 'Account.archivePassword': password |
| 65 | }; |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 66 | if (isJams) { |
| 67 | accountDetails['Account.managerUri'] = 'https://jams.savoirfairelinux.com/'; |
| 68 | accountDetails['Account.managerUsername'] = username; |
| 69 | } |
| 70 | const { accountId, state } = await jamid.addAccount(accountDetails); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 71 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 72 | if (isJams) { |
| 73 | if (state === 'ERROR_GENERIC') { |
| 74 | jamid.removeAccount(accountId); |
| 75 | res.status(HttpStatusCode.Unauthorized).send('Invalid JAMS credentials'); |
| 76 | return; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 77 | } |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 78 | } else { |
| 79 | const state = await jamid.registerUsername(accountId, username, ''); |
| 80 | if (state !== 0) { |
| 81 | jamid.removeAccount(accountId); |
| 82 | if (state === 2) { |
| 83 | res.status(HttpStatusCode.BadRequest).send('Invalid username or password'); |
| 84 | } else if (state === 3) { |
| 85 | res.status(HttpStatusCode.Conflict).send('Username already exists'); |
| 86 | } else { |
| 87 | throw new Error(`Unhandled state ${state}`); |
| 88 | } |
| 89 | return; |
| 90 | } |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 91 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 92 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 93 | accounts.set(username, hashedPassword, isJams); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 94 | await accounts.save(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 95 | |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 96 | res.sendStatus(HttpStatusCode.Created); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 97 | }) |
| 98 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 99 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 100 | authRouter.post( |
| 101 | '/login', |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 102 | asyncHandler( |
| 103 | async (req: Request<ParamsDictionary, { accessToken: string } | string, Partial<Credentials>>, res, _next) => { |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 104 | const { username, password, isJams } = req.body; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 105 | if (username === undefined || password === undefined) { |
| 106 | res.status(HttpStatusCode.BadRequest).send('Missing username or password in body'); |
| 107 | return; |
| 108 | } |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 109 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 110 | // Check if the account is stored stored on this daemon instance |
| 111 | const accountId = jamid.getAccountIdFromUsername(username); |
| 112 | if (accountId === undefined) { |
| 113 | res.status(HttpStatusCode.NotFound).send('Username not found'); |
| 114 | return; |
| 115 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 116 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 117 | const hashedPassword = accounts.get(username, isJams); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 118 | if (hashedPassword === undefined) { |
| 119 | res |
| 120 | .status(HttpStatusCode.NotFound) |
| 121 | .send('Password not found (the account does not have a password set on the server)'); |
| 122 | return; |
| 123 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 124 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 125 | const isPasswordVerified = await argon2.verify(hashedPassword, password); |
| 126 | if (!isPasswordVerified) { |
| 127 | res.status(HttpStatusCode.Unauthorized).send('Incorrect password'); |
| 128 | return; |
| 129 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 130 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 131 | const jwt = await signJwt(accountId); |
| 132 | res.send({ accessToken: jwt }); |
| 133 | } |
| 134 | ) |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 135 | ); |