blob: 8404eee85efb2d0188841487c774428910060885 [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 */
18import { passwordStrength } from 'check-password-strength';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040019import { HttpStatusCode } from 'jami-web-common';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040020
21import { PasswordStrength } from '../enums/password-strength';
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040022import { apiUrl } from './constants';
23import { InvalidPassword, UsernameNotFound } from './errors';
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040024
25interface PasswordStrengthResult {
26 id: number;
27 value: string;
28 contains: string[];
29 length: number;
30}
31
32export interface PasswordCheckResult {
33 strong: boolean;
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040034 valueCode: StrengthValueCode;
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040035}
36
Michelle Sepkap Sime51c00452022-10-31 21:26:38 -040037export type StrengthValueCode = 'default' | 'too_weak' | 'weak' | 'medium' | 'strong';
38
39const idToStrengthValueCode: StrengthValueCode[] = ['too_weak', 'weak', 'medium', 'strong'];
40
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040041export async function isNameRegistered(name: string): Promise<boolean> {
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040042 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 Sime51c00452022-10-31 21:26:38 -040049 return false;
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040050 default:
51 throw new Error(await response.text());
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040052 }
53}
54
55export 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 Sime51c00452022-10-31 21:26:38 -040060 valueCode: idToStrengthValueCode[strengthResult.id] ?? 'default',
Michelle Sepkap Simebff70eb2022-10-17 15:01:14 -040061 };
62
63 return checkResult;
64}
Michelle Sepkap Simee580f422022-10-31 23:27:04 -040065
66export 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
81export 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
106export function getAccessToken(): string {
107 const accessToken: string | null = localStorage.getItem('accessToken');
108 return accessToken ?? '';
109}
110
111export function setAccessToken(accessToken: string): void {
112 localStorage.setItem('accessToken', accessToken);
113}