simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 1 | /* eslint-disable no-undef */ |
| 2 | // TODO: This hides eslint errors for this file. This should be removed once this file is cleaned up. |
| 3 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 4 | import { PromiseExecutor } from '../../model/util'; |
| 5 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 6 | /* |
| 7 | * Copyright (c) 2017-2021 Savoir-faire Linux Inc. |
| 8 | * |
| 9 | * Author: Adrien Béraud <adrien.beraud@savoirfairelinux.com> |
| 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 3 of the License, or |
| 14 | * (at your option) any later version. |
| 15 | * |
| 16 | * This program is distributed in the hope that it will be useful, |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 19 | * GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License |
| 22 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 23 | */ |
| 24 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 25 | interface AuthManagerState { |
| 26 | initialized: boolean; |
| 27 | authenticated: boolean; |
| 28 | setupComplete: boolean; |
| 29 | error: boolean; |
| 30 | } |
| 31 | |
| 32 | interface AuthManagerTask extends PromiseExecutor<Response> { |
| 33 | url: string; |
| 34 | init?: RequestInit; |
| 35 | } |
| 36 | |
| 37 | interface InitData { |
| 38 | loggedin?: true; |
| 39 | username?: string; |
| 40 | type?: string; |
| 41 | setupComplete?: boolean; |
| 42 | } |
| 43 | |
| 44 | type OnAuthChanged = (auth: AuthManagerState) => void; |
| 45 | |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 46 | class AuthManager { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 47 | private authenticating: boolean; |
| 48 | private readonly _state: AuthManagerState; |
| 49 | private tasks: AuthManagerTask[]; |
| 50 | private onAuthChanged: OnAuthChanged | undefined; |
| 51 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 52 | constructor() { |
| 53 | console.log('AuthManager()'); |
| 54 | this.authenticating = false; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 55 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 56 | this._state = { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 57 | initialized: false, |
| 58 | authenticated: true, |
| 59 | setupComplete: true, |
| 60 | error: false, |
| 61 | }; |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 62 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 63 | this.tasks = []; |
| 64 | this.onAuthChanged = undefined; |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 65 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 66 | // @ts-ignore |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 67 | if (initData) { |
| 68 | console.log('Using static initData'); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 69 | // @ts-ignore |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 70 | this.setInitData(initData); |
| 71 | return; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 72 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 73 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 74 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 75 | isAuthenticated() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 76 | return this._state.authenticated; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | getState() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 80 | return this._state; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 81 | } |
| 82 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 83 | setInitData(data: InitData) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 84 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 85 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 86 | if (data.username) { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 87 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 88 | authenticated: true, |
| 89 | setupComplete: true, |
| 90 | error: false, |
| 91 | user: { username: data.username, type: data.type }, |
| 92 | }); |
| 93 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 94 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 95 | authenticated: false, |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 96 | setupComplete: data.setupComplete ?? true, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 97 | error: false, |
| 98 | }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 99 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 100 | console.log('Init ended'); |
| 101 | /*if (this.onAuthChanged) |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 102 | this.onAuthChanged(this._state)*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 103 | } |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 104 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 105 | init(cb: OnAuthChanged) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 106 | this.onAuthChanged = cb; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 107 | if (this._state.initialized || this.authenticating) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 108 | /*if (initData) { |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 109 | console.log("Using static initData") |
| 110 | this.setInitData(initData) |
| 111 | return |
| 112 | }*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 113 | this.authenticating = true; |
| 114 | fetch('/auth') |
| 115 | .then(async (response) => { |
| 116 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 117 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 118 | if (response.status === 200) { |
| 119 | this.setInitData(await response.json()); |
| 120 | } else if (response.status === 401) { |
| 121 | this.setInitData(await response.json()); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 122 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 123 | this._state.error = true; |
| 124 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 125 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 126 | }) |
| 127 | .catch((e) => { |
| 128 | this.authenticating = false; |
| 129 | console.log(e); |
| 130 | }); |
| 131 | } |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 132 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 133 | deinit() { |
| 134 | console.log('Deinit'); |
| 135 | this.onAuthChanged = undefined; |
| 136 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 137 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 138 | async setup(password: string) { |
| 139 | if (this.authenticating || this._state.setupComplete) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 140 | console.log('Starting setup'); |
| 141 | this.authenticating = true; |
| 142 | const response = await fetch(`/setup`, { |
| 143 | method: 'POST', |
| 144 | headers: { |
| 145 | Accept: 'application/json', |
| 146 | 'Content-Type': 'application/json', |
| 147 | }, |
| 148 | body: JSON.stringify({ password }), |
| 149 | }); |
| 150 | console.log(response); |
| 151 | if (response.ok) { |
| 152 | console.log('Success, going home'); |
| 153 | //navigate('/') |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 154 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 155 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 156 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 157 | this._state.setupComplete = true; |
| 158 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 159 | return response.ok; |
| 160 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 161 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 162 | authenticate(username: string, password: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 163 | if (this.authenticating) return; |
| 164 | console.log('Starting authentication'); |
| 165 | this.authenticating = true; |
| 166 | fetch(`/auth/local?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`, { |
| 167 | method: 'POST', |
| 168 | }) |
| 169 | .then((response) => { |
| 170 | console.log(response); |
| 171 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 172 | this._state.authenticated = response.ok && response.status === 200; |
| 173 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
| 174 | while (true) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 175 | const task = this.tasks.shift(); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 176 | if (!task) { |
| 177 | break; |
| 178 | } |
| 179 | if (this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 180 | fetch(task.url, task.init) |
| 181 | .then((res) => task.resolve(res)) |
| 182 | .catch((e) => console.log('Error executing pending task: ' + e)); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 183 | } else { |
| 184 | task.reject(new Error('Authentication failed')); |
| 185 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 186 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 187 | }) |
| 188 | .catch((e) => { |
| 189 | this.authenticating = false; |
| 190 | console.log(e); |
| 191 | }); |
| 192 | } |
| 193 | |
| 194 | disconnect() { |
| 195 | console.log('Disconnect'); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 196 | this._state.authenticated = false; |
| 197 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 198 | } |
| 199 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 200 | fetch(url: string, init?: RequestInit): Promise<Response> { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 201 | console.log(`fetch ${url}`); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 202 | if (!this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 203 | if (!init || !init.method || init.method === 'GET') { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 204 | return new Promise<Response>((resolve, reject) => this.tasks.push({ url, init, resolve, reject })); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 205 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 206 | return new Promise<Response>((resolve, reject) => reject('Not authenticated')); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 207 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 208 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 209 | return fetch(url, init).then((response) => { |
| 210 | if (response.status === 401) { |
| 211 | this.disconnect(); |
| 212 | return this.fetch(url, init); |
| 213 | } |
| 214 | return response; |
| 215 | }); |
| 216 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 217 | } |
| 218 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 219 | export default new AuthManager(); |