Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [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 { passwordStrength } from 'check-password-strength'; |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 19 | import { HttpStatusCode, LookupResolveValue } from 'jami-web-common'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 20 | |
| 21 | import { PasswordStrength } from '../enums/password-strength'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 22 | |
| 23 | interface PasswordStrengthResult { |
| 24 | id: number; |
| 25 | value: string; |
| 26 | contains: string[]; |
| 27 | length: number; |
| 28 | } |
| 29 | |
| 30 | export interface PasswordCheckResult { |
| 31 | strong: boolean; |
| 32 | value: string; |
| 33 | } |
| 34 | |
| 35 | // TODO: Find a way to do it differently or remove this check from account creation. |
| 36 | // It doesn't work if the server has secured this path, so I tweaked the server for test. |
| 37 | // The tweak is to remove secured of apiRouter middleware in the server (app.ts). |
| 38 | export async function isNameRegistered(name: string): Promise<boolean> { |
| 39 | try { |
| 40 | const response: Response = await fetch(`api/ns/name/${name}`); |
Misha Krieger-Raynauld | 2f5d1ce | 2022-10-23 21:13:33 -0400 | [diff] [blame] | 41 | if (response.status === HttpStatusCode.Ok) { |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 42 | const data: LookupResolveValue = await response.json(); |
| 43 | return data.name === name; |
| 44 | } |
| 45 | return false; |
| 46 | } catch (err) { |
| 47 | return true; |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | export function checkPasswordStrength(password: string): PasswordCheckResult { |
| 52 | const strengthResult: PasswordStrengthResult = passwordStrength(password); |
| 53 | |
| 54 | const checkResult: PasswordCheckResult = { |
| 55 | strong: strengthResult.id === PasswordStrength.Strong.valueOf(), |
| 56 | value: strengthResult.value, |
| 57 | }; |
| 58 | |
| 59 | return checkResult; |
| 60 | } |