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'; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 19 | import { HttpStatusCode } 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 | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 22 | import { apiUrl } from './constants'; |
| 23 | import { InvalidPassword, UsernameNotFound } from './errors'; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 24 | |
| 25 | interface PasswordStrengthResult { |
| 26 | id: number; |
| 27 | value: string; |
| 28 | contains: string[]; |
| 29 | length: number; |
| 30 | } |
| 31 | |
| 32 | export interface PasswordCheckResult { |
| 33 | strong: boolean; |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 34 | valueCode: StrengthValueCode; |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 37 | export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong'; |
| 38 | |
| 39 | const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong']; |
| 40 | |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 41 | export async function isNameRegistered(name: string): Promise<boolean> { |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 42 | const url = new URL(`/ns/username/${name}`, apiUrl); |
| 43 | const response = await fetch(url); |
| 44 | |
| 45 | switch (response.status) { |
| 46 | case HttpStatusCode.Ok: |
| 47 | return true; |
| 48 | case HttpStatusCode.NotFound: |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 49 | return false; |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 50 | default: |
| 51 | throw new Error(await response.text()); |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
| 55 | export function checkPasswordStrength(password: string): PasswordCheckResult { |
| 56 | const strengthResult: PasswordStrengthResult = passwordStrength(password); |
| 57 | |
| 58 | const checkResult: PasswordCheckResult = { |
| 59 | strong: strengthResult.id === PasswordStrength.Strong.valueOf(), |
Michelle Sepkap Sime | 51c0045 | 2022-10-31 21:26:38 -0400 | [diff] [blame] | 60 | valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default', |
Michelle Sepkap Sime | bff70eb | 2022-10-17 15:01:14 -0400 | [diff] [blame] | 61 | }; |
| 62 | |
| 63 | return checkResult; |
| 64 | } |
Michelle Sepkap Sime | e580f42 | 2022-10-31 23:27:04 -0400 | [diff] [blame] | 65 | |
| 66 | export async function registerUser(username: string, password: string): Promise<void> { |
| 67 | const url = new URL('/auth/new-account', apiUrl); |
| 68 | const response: Response = await fetch(url, { |
| 69 | method: 'POST', |
| 70 | headers: { |
| 71 | 'Content-Type': 'application/json', |
| 72 | }, |
| 73 | body: JSON.stringify({ username, password }), |
| 74 | }); |
| 75 | |
| 76 | if (response.status !== HttpStatusCode.Created) { |
| 77 | throw new Error(await response.text()); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | export async function loginUser(username: string, password: string): Promise<string> { |
| 82 | const url = new URL('/auth/login', apiUrl); |
| 83 | const response = await fetch(url, { |
| 84 | method: 'POST', |
| 85 | headers: { |
| 86 | 'Content-Type': 'application/json', |
| 87 | }, |
| 88 | body: JSON.stringify({ username, password }), |
| 89 | }); |
| 90 | |
| 91 | switch (response.status) { |
| 92 | case HttpStatusCode.Ok: |
| 93 | break; |
| 94 | case HttpStatusCode.NotFound: |
| 95 | throw new UsernameNotFound(); |
| 96 | case HttpStatusCode.Unauthorized: |
| 97 | throw new InvalidPassword(); |
| 98 | default: |
| 99 | throw new Error(await response.text()); |
| 100 | } |
| 101 | |
| 102 | const data: { accessToken: string } = await response.json(); |
| 103 | return data.accessToken; |
| 104 | } |
| 105 | |
| 106 | export function getAccessToken(): string { |
| 107 | const accessToken: string | null = localStorage.getItem('accessToken'); |
| 108 | return accessToken ?? ''; |
| 109 | } |
| 110 | |
| 111 | export function setAccessToken(accessToken: string): void { |
| 112 | localStorage.setItem('accessToken', accessToken); |
| 113 | } |