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; |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [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 | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 62 | const accountDetails: Partial<AccountDetails> = { 'Account.archivePassword': password }; |
| 63 | if (isJams) { |
| 64 | accountDetails['Account.managerUri'] = 'https://jams.savoirfairelinux.com/'; |
| 65 | accountDetails['Account.managerUsername'] = username; |
| 66 | } |
| 67 | const { accountId, state } = await jamid.addAccount(accountDetails); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 68 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 69 | if (isJams) { |
| 70 | if (state === 'ERROR_GENERIC') { |
| 71 | jamid.removeAccount(accountId); |
| 72 | res.status(HttpStatusCode.Unauthorized).send('Invalid JAMS credentials'); |
| 73 | return; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 74 | } |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 75 | } else { |
| 76 | const state = await jamid.registerUsername(accountId, username, ''); |
| 77 | if (state !== 0) { |
| 78 | jamid.removeAccount(accountId); |
| 79 | if (state === 2) { |
| 80 | res.status(HttpStatusCode.BadRequest).send('Invalid username or password'); |
| 81 | } else if (state === 3) { |
| 82 | res.status(HttpStatusCode.Conflict).send('Username already exists'); |
| 83 | } else { |
| 84 | throw new Error(`Unhandled state ${state}`); |
| 85 | } |
| 86 | return; |
| 87 | } |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 88 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 89 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 90 | accounts.set(username, hashedPassword, isJams); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 91 | await accounts.save(); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 92 | |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 93 | res.sendStatus(HttpStatusCode.Created); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 94 | }) |
| 95 | ); |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 96 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 97 | authRouter.post( |
| 98 | '/login', |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 99 | asyncHandler( |
| 100 | async (req: Request<ParamsDictionary, { accessToken: string } | string, Partial<Credentials>>, res, _next) => { |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 101 | const { username, password, isJams } = req.body; |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 102 | if (username === undefined || password === undefined) { |
| 103 | res.status(HttpStatusCode.BadRequest).send('Missing username or password in body'); |
| 104 | return; |
| 105 | } |
Issam E. Maghni | 0ef4a36 | 2022-10-05 23:20:16 +0000 | [diff] [blame] | 106 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 107 | // Check if the account is stored stored on this daemon instance |
| 108 | const accountId = jamid.getAccountIdFromUsername(username); |
| 109 | if (accountId === undefined) { |
| 110 | res.status(HttpStatusCode.NotFound).send('Username not found'); |
| 111 | return; |
| 112 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 113 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame^] | 114 | const hashedPassword = accounts.get(username, isJams); |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 115 | if (hashedPassword === undefined) { |
| 116 | res |
| 117 | .status(HttpStatusCode.NotFound) |
| 118 | .send('Password not found (the account does not have a password set on the server)'); |
| 119 | return; |
| 120 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 121 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 122 | const isPasswordVerified = await argon2.verify(hashedPassword, password); |
| 123 | if (!isPasswordVerified) { |
| 124 | res.status(HttpStatusCode.Unauthorized).send('Incorrect password'); |
| 125 | return; |
| 126 | } |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 127 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 128 | const jwt = await signJwt(accountId); |
| 129 | res.send({ accessToken: jwt }); |
| 130 | } |
| 131 | ) |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 132 | ); |