blob: 3704fe436bd7a8b1dd9c6b82edeb69073bbef1f7 [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 {
44 await axios.get(`/ns/username/${name}`, {
45 baseURL: apiUrl,
46 });
47 return true;
48 } catch (e: any) {
49 if (e.response?.status !== HttpStatusCode.NotFound) {
50 throw e;
51 }
52 return false;
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040053 }
54}
55
56export function checkPasswordStrength(password: string): PasswordCheckResult {
57 const strengthResult: PasswordStrengthResult = passwordStrength(password);
58
simon94fe53e2022-11-10 12:51:58 -050059 return {
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040060 strong: strengthResult.id === PasswordStrength.Strong.valueOf(),
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040061 valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default',
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040062 };
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040063}
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040064
65export async function registerUser(username: string, password: string): Promise<void> {
simon94fe53e2022-11-10 12:51:58 -050066 await axios.post(
67 '/auth/new-account',
68 { username, password },
69 {
70 baseURL: apiUrl,
71 }
72 );
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040073}
74
75export async function loginUser(username: string, password: string): Promise<string> {
simon94fe53e2022-11-10 12:51:58 -050076 try {
77 const { data } = await axios.post(
78 '/auth/login',
79 { username, password },
80 {
81 baseURL: apiUrl,
82 }
83 );
84 return data.accessToken;
85 } catch (e: any) {
86 switch (e.response?.status) {
87 case HttpStatusCode.NotFound:
88 throw new UsernameNotFound();
89 case HttpStatusCode.Unauthorized:
90 throw new InvalidPassword();
91 default:
92 throw e;
93 }
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040094 }
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040095}
96
97export function getAccessToken(): string {
98 const accessToken: string | null = localStorage.getItem('accessToken');
99 return accessToken ?? '';
100}
101
102export function setAccessToken(accessToken: string): void {
103 localStorage.setItem('accessToken', accessToken);
104}