simon | 26e79f7 | 2022-10-05 22:16:08 -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 | |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 19 | /* eslint-disable no-undef */ |
| 20 | // TODO: This hides eslint errors for this file. This should be removed once this file is cleaned up. |
| 21 | |
simon | 2007698 | 2022-10-11 15:04:13 -0400 | [diff] [blame] | 22 | import { PromiseExecutor } from 'jami-web-common'; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 23 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 24 | interface AuthManagerState { |
| 25 | initialized: boolean; |
| 26 | authenticated: boolean; |
| 27 | setupComplete: boolean; |
| 28 | error: boolean; |
| 29 | } |
| 30 | |
| 31 | interface AuthManagerTask extends PromiseExecutor<Response> { |
| 32 | url: string; |
| 33 | init?: RequestInit; |
| 34 | } |
| 35 | |
| 36 | interface InitData { |
| 37 | loggedin?: true; |
| 38 | username?: string; |
| 39 | type?: string; |
| 40 | setupComplete?: boolean; |
| 41 | } |
| 42 | |
| 43 | type OnAuthChanged = (auth: AuthManagerState) => void; |
| 44 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 45 | class AuthManager { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 46 | private authenticating: boolean; |
| 47 | private readonly _state: AuthManagerState; |
| 48 | private tasks: AuthManagerTask[]; |
| 49 | private onAuthChanged: OnAuthChanged | undefined; |
| 50 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 51 | constructor() { |
| 52 | console.log('AuthManager()'); |
| 53 | this.authenticating = false; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 54 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 55 | this._state = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 56 | initialized: false, |
| 57 | authenticated: true, |
| 58 | setupComplete: true, |
| 59 | error: false, |
| 60 | }; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 61 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 62 | this.tasks = []; |
| 63 | this.onAuthChanged = undefined; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 64 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 65 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 66 | isAuthenticated() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 67 | return this._state.authenticated; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | getState() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 71 | return this._state; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 72 | } |
| 73 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 74 | setInitData(data: InitData) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 75 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 76 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 77 | if (data.username) { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 78 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 79 | authenticated: true, |
| 80 | setupComplete: true, |
| 81 | error: false, |
| 82 | user: { username: data.username, type: data.type }, |
| 83 | }); |
| 84 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 85 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | authenticated: false, |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 87 | setupComplete: data.setupComplete ?? true, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 88 | error: false, |
| 89 | }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 90 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 91 | console.log('Init ended'); |
| 92 | /*if (this.onAuthChanged) |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 93 | this.onAuthChanged(this._state)*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 94 | } |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 95 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 96 | init(cb: OnAuthChanged) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 97 | this.onAuthChanged = cb; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 98 | if (this._state.initialized || this.authenticating) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 99 | /*if (initData) { |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 100 | console.log("Using static initData") |
| 101 | this.setInitData(initData) |
| 102 | return |
| 103 | }*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 104 | this.authenticating = true; |
| 105 | fetch('/auth') |
| 106 | .then(async (response) => { |
| 107 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 108 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 109 | if (response.status === 200) { |
| 110 | this.setInitData(await response.json()); |
| 111 | } else if (response.status === 401) { |
| 112 | this.setInitData(await response.json()); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 113 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 114 | this._state.error = true; |
| 115 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 116 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 117 | }) |
| 118 | .catch((e) => { |
| 119 | this.authenticating = false; |
| 120 | console.log(e); |
| 121 | }); |
| 122 | } |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 123 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 124 | deinit() { |
| 125 | console.log('Deinit'); |
| 126 | this.onAuthChanged = undefined; |
| 127 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 128 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 129 | async setup(password: string) { |
| 130 | if (this.authenticating || this._state.setupComplete) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 131 | console.log('Starting setup'); |
| 132 | this.authenticating = true; |
| 133 | const response = await fetch(`/setup`, { |
| 134 | method: 'POST', |
| 135 | headers: { |
| 136 | Accept: 'application/json', |
| 137 | 'Content-Type': 'application/json', |
| 138 | }, |
| 139 | body: JSON.stringify({ password }), |
| 140 | }); |
| 141 | console.log(response); |
| 142 | if (response.ok) { |
| 143 | console.log('Success, going home'); |
| 144 | //navigate('/') |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 145 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 146 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 147 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 148 | this._state.setupComplete = true; |
| 149 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 150 | return response.ok; |
| 151 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 152 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 153 | authenticate(username: string, password: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 154 | if (this.authenticating) return; |
| 155 | console.log('Starting authentication'); |
| 156 | this.authenticating = true; |
| 157 | fetch(`/auth/local?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`, { |
| 158 | method: 'POST', |
| 159 | }) |
| 160 | .then((response) => { |
| 161 | console.log(response); |
| 162 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 163 | this._state.authenticated = response.ok && response.status === 200; |
| 164 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
| 165 | while (true) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 166 | const task = this.tasks.shift(); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 167 | if (!task) { |
| 168 | break; |
| 169 | } |
| 170 | if (this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 171 | fetch(task.url, task.init) |
| 172 | .then((res) => task.resolve(res)) |
| 173 | .catch((e) => console.log('Error executing pending task: ' + e)); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 174 | } else { |
| 175 | task.reject(new Error('Authentication failed')); |
| 176 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 177 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 178 | }) |
| 179 | .catch((e) => { |
| 180 | this.authenticating = false; |
| 181 | console.log(e); |
| 182 | }); |
| 183 | } |
| 184 | |
| 185 | disconnect() { |
| 186 | console.log('Disconnect'); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 187 | this._state.authenticated = false; |
| 188 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 189 | } |
| 190 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 191 | fetch(url: string, init?: RequestInit): Promise<Response> { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 192 | console.log(`fetch ${url}`); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 193 | if (!this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 194 | if (!init || !init.method || init.method === 'GET') { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 195 | return new Promise<Response>((resolve, reject) => this.tasks.push({ url, init, resolve, reject })); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 196 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 197 | return new Promise<Response>((resolve, reject) => reject('Not authenticated')); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 198 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 199 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 200 | return fetch(url, init).then((response) => { |
| 201 | if (response.status === 401) { |
| 202 | this.disconnect(); |
| 203 | return this.fetch(url, init); |
| 204 | } |
| 205 | return response; |
| 206 | }); |
| 207 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 208 | } |
| 209 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 210 | export default new AuthManager(); |