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 | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 22 | import { PromiseExecutor } from '../../model/util'; |
| 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; |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 64 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 65 | // @ts-ignore |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 66 | if (initData) { |
| 67 | console.log('Using static initData'); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 68 | // @ts-ignore |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 69 | this.setInitData(initData); |
| 70 | return; |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 71 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 72 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 73 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 74 | isAuthenticated() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 75 | return this._state.authenticated; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | getState() { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 79 | return this._state; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 80 | } |
| 81 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 82 | setInitData(data: InitData) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 83 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 84 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 85 | if (data.username) { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 86 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 87 | authenticated: true, |
| 88 | setupComplete: true, |
| 89 | error: false, |
| 90 | user: { username: data.username, type: data.type }, |
| 91 | }); |
| 92 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 93 | Object.assign(this._state, { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 94 | authenticated: false, |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 95 | setupComplete: data.setupComplete ?? true, |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 96 | error: false, |
| 97 | }); |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 98 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 99 | console.log('Init ended'); |
| 100 | /*if (this.onAuthChanged) |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 101 | this.onAuthChanged(this._state)*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 102 | } |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 103 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 104 | init(cb: OnAuthChanged) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 105 | this.onAuthChanged = cb; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 106 | if (this._state.initialized || this.authenticating) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 107 | /*if (initData) { |
Adrien Béraud | e5cad98 | 2021-06-07 10:05:50 -0400 | [diff] [blame] | 108 | console.log("Using static initData") |
| 109 | this.setInitData(initData) |
| 110 | return |
| 111 | }*/ |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 112 | this.authenticating = true; |
| 113 | fetch('/auth') |
| 114 | .then(async (response) => { |
| 115 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 116 | this._state.initialized = true; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 117 | if (response.status === 200) { |
| 118 | this.setInitData(await response.json()); |
| 119 | } else if (response.status === 401) { |
| 120 | this.setInitData(await response.json()); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 121 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 122 | this._state.error = true; |
| 123 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 124 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 125 | }) |
| 126 | .catch((e) => { |
| 127 | this.authenticating = false; |
| 128 | console.log(e); |
| 129 | }); |
| 130 | } |
Adrien Béraud | e74741b | 2021-04-19 13:22:54 -0400 | [diff] [blame] | 131 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 132 | deinit() { |
| 133 | console.log('Deinit'); |
| 134 | this.onAuthChanged = undefined; |
| 135 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 136 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 137 | async setup(password: string) { |
| 138 | if (this.authenticating || this._state.setupComplete) return; |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 139 | console.log('Starting setup'); |
| 140 | this.authenticating = true; |
| 141 | const response = await fetch(`/setup`, { |
| 142 | method: 'POST', |
| 143 | headers: { |
| 144 | Accept: 'application/json', |
| 145 | 'Content-Type': 'application/json', |
| 146 | }, |
| 147 | body: JSON.stringify({ password }), |
| 148 | }); |
| 149 | console.log(response); |
| 150 | if (response.ok) { |
| 151 | console.log('Success, going home'); |
| 152 | //navigate('/') |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 153 | } |
simon | 80b7b3b | 2022-09-28 17:50:10 -0400 | [diff] [blame] | 154 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 155 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 156 | this._state.setupComplete = true; |
| 157 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 158 | return response.ok; |
| 159 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 160 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 161 | authenticate(username: string, password: string) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 162 | if (this.authenticating) return; |
| 163 | console.log('Starting authentication'); |
| 164 | this.authenticating = true; |
| 165 | fetch(`/auth/local?username=${encodeURIComponent(username)}&password=${encodeURIComponent(password)}`, { |
| 166 | method: 'POST', |
| 167 | }) |
| 168 | .then((response) => { |
| 169 | console.log(response); |
| 170 | this.authenticating = false; |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 171 | this._state.authenticated = response.ok && response.status === 200; |
| 172 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
| 173 | while (true) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 174 | const task = this.tasks.shift(); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 175 | if (!task) { |
| 176 | break; |
| 177 | } |
| 178 | if (this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 179 | fetch(task.url, task.init) |
| 180 | .then((res) => task.resolve(res)) |
| 181 | .catch((e) => console.log('Error executing pending task: ' + e)); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 182 | } else { |
| 183 | task.reject(new Error('Authentication failed')); |
| 184 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 185 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 186 | }) |
| 187 | .catch((e) => { |
| 188 | this.authenticating = false; |
| 189 | console.log(e); |
| 190 | }); |
| 191 | } |
| 192 | |
| 193 | disconnect() { |
| 194 | console.log('Disconnect'); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 195 | this._state.authenticated = false; |
| 196 | if (this.onAuthChanged) this.onAuthChanged(this._state); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 197 | } |
| 198 | |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 199 | fetch(url: string, init?: RequestInit): Promise<Response> { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 200 | console.log(`fetch ${url}`); |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 201 | if (!this._state.authenticated) { |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 202 | if (!init || !init.method || init.method === 'GET') { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 203 | return new Promise<Response>((resolve, reject) => this.tasks.push({ url, init, resolve, reject })); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 204 | } else { |
simon | 218d3d1 | 2022-10-01 17:27:01 -0400 | [diff] [blame] | 205 | return new Promise<Response>((resolve, reject) => reject('Not authenticated')); |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 206 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 207 | } |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 208 | return fetch(url, init).then((response) => { |
| 209 | if (response.status === 401) { |
| 210 | this.disconnect(); |
| 211 | return this.fetch(url, init); |
| 212 | } |
| 213 | return response; |
| 214 | }); |
| 215 | } |
Adrien Béraud | 6ecaa40 | 2021-04-06 17:37:25 -0400 | [diff] [blame] | 216 | } |
| 217 | |
simon | d47ef9e | 2022-09-28 22:24:28 -0400 | [diff] [blame] | 218 | export default new AuthManager(); |