blob: 822771ce999421d15e19881afe093131b8312696 [file] [log] [blame]
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -04001/*
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 */
simon94fe53e2022-11-10 12:51:58 -050018import axios from 'axios';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040019import { passwordStrength } from 'check-password-strength';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040020import { HttpStatusCode } from 'jami-web-common';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040021
22import { PasswordStrength } from '../enums/password-strength';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040023import { apiUrl } from './constants';
24import { InvalidPassword, UsernameNotFound } from './errors';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040025
26interface PasswordStrengthResult {
27 id: number;
28 value: string;
29 contains: string[];
30 length: number;
31}
32
33export interface PasswordCheckResult {
34 strong: boolean;
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040035 valueCode: StrengthValueCode;
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040036}
37
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040038export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong';
39
40const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong'];
41
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040042export async function isNameRegistered(name: string): Promise<boolean> {
simon94fe53e2022-11-10 12:51:58 -050043 try {
Misha Krieger-Raynauld46e9d242022-11-12 18:02:43 -050044 await axios.get(`/ns/username/${name}`, { baseURL: apiUrl });
simon94fe53e2022-11-10 12:51:58 -050045 return true;
46 } catch (e: any) {
47 if (e.response?.status !== HttpStatusCode.NotFound) {
48 throw e;
49 }
50 return false;
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040051 }
52}
53
54export function checkPasswordStrength(password: string): PasswordCheckResult {
55 const strengthResult: PasswordStrengthResult = passwordStrength(password);
56
simon94fe53e2022-11-10 12:51:58 -050057 return {
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040058 strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040059 valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default',
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040060 };
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040061}
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040062
63export async function registerUser(username: string, password: string): Promise<void> {
Misha Krieger-Raynauld46e9d242022-11-12 18:02:43 -050064 await axios.post('/auth/new-account', { username, password }, { baseURL: apiUrl });
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040065}
66
67export async function loginUser(username: string, password: string): Promise<string> {
simon94fe53e2022-11-10 12:51:58 -050068 try {
Misha Krieger-Raynauld46e9d242022-11-12 18:02:43 -050069 const { data } = await axios.post('/auth/login', { username, password }, { baseURL: apiUrl });
simon94fe53e2022-11-10 12:51:58 -050070 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 Simee580f422022-10-31 23:27:04 -040080 }
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040081}
82
83export function getAccessToken(): string {
84 const accessToken: string | null = localStorage.getItem('accessToken');
85 return accessToken ?? '';
86}
87
88export function setAccessToken(accessToken: string): void {
89 localStorage.setItem('accessToken', accessToken);
90}