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 | */ |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 18 | import axios from 'axios'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 19 | import { passwordStrength } from 'check-password-strength'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 20 | import { HttpStatusCode } from 'jami-web-common'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 21 | |
simon | f929a36 | 2022-11-18 16:53:45 -0500 | [diff] [blame] | 22 | import { PasswordStrength } from '../enums/passwordStrength'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 23 | import { apiUrl } from './constants'; |
| 24 | import { InvalidPassword, UsernameNotFound } from './errors'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 25 | |
| 26 | interface PasswordStrengthResult { |
| 27 | id: number; |
| 28 | value: string; |
| 29 | contains: string[]; |
| 30 | length: number; |
| 31 | } |
| 32 | |
| 33 | export interface PasswordCheckResult { |
| 34 | strong: boolean; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 35 | valueCode: StrengthValueCode; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 36 | } |
| 37 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 38 | export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong'; |
| 39 | |
| 40 | const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong']; |
| 41 | |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 42 | export async function isNameRegistered(name: string): Promise<boolean> { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 43 | try { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 44 | await axios.get(`/ns/username/${name}`, { baseURL: apiUrl }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 45 | return true; |
| 46 | } catch (e: any) { |
| 47 | if (e.response?.status !== HttpStatusCode.NotFound) { |
| 48 | throw e; |
| 49 | } |
| 50 | return false; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 51 | } |
| 52 | } |
| 53 | |
| 54 | export function checkPasswordStrength(password: string): PasswordCheckResult { |
| 55 | const strengthResult: PasswordStrengthResult = passwordStrength(password); |
| 56 | |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 57 | return { |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 58 | strong: strengthResult.id === PasswordStrength.Strong.valueOf(), |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 59 | valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default', |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 60 | }; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 61 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 62 | |
| 63 | export async function registerUser(username: string, password: string): Promise<void> { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 64 | await axios.post('/auth/new-account', { username, password }, { baseURL: apiUrl }); |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | export async function loginUser(username: string, password: string): Promise<string> { |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 68 | try { |
Misha Krieger-Raynauld | 46e9d24 | 2022-11-12 18:02:43 -0500 | [diff] [blame] | 69 | const { data } = await axios.post('/auth/login', { username, password }, { baseURL: apiUrl }); |
simon | 94fe53e | 2022-11-10 12:51:58 -0500 | [diff] [blame] | 70 | return data.accessToken; |
| 71 | } catch (e: any) { |
| 72 | switch (e.response?.status) { |
| 73 | case HttpStatusCode.NotFound: |
| 74 | throw new UsernameNotFound(); |
| 75 | case HttpStatusCode.Unauthorized: |
| 76 | throw new InvalidPassword(); |
| 77 | default: |
| 78 | throw e; |
| 79 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 80 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 81 | } |
| 82 | |
Michelle Sepkap Sime | dd82cbf | 2022-11-17 23:31:49 -0500 | [diff] [blame] | 83 | export function getAccessToken(): string | undefined { |
| 84 | return localStorage.getItem('accessToken') ?? undefined; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 85 | } |
| 86 | |
| 87 | export function setAccessToken(accessToken: string): void { |
| 88 | localStorage.setItem('accessToken', accessToken); |
| 89 | } |