Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [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 | import { readFileSync } from 'node:fs'; |
| 19 | import { writeFile } from 'node:fs/promises'; |
| 20 | |
| 21 | import { Service } from 'typedi'; |
| 22 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 23 | interface AccountsFormat { |
| 24 | local: Record<string, string>; |
| 25 | jams: Record<string, string>; |
| 26 | } |
| 27 | |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 28 | @Service() |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 29 | export class Accounts { |
| 30 | private readonly filename = 'accounts.json'; |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 31 | private accounts: AccountsFormat; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 32 | |
| 33 | constructor() { |
| 34 | let buffer: Buffer; |
| 35 | |
| 36 | try { |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 37 | buffer = readFileSync(this.filename); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 38 | } catch (e) { |
Issam E. Maghni | 5bc33a3 | 2022-11-22 21:36:19 +0000 | [diff] [blame] | 39 | buffer = Buffer.from('{"local":{},"jams":{}}'); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 40 | } |
| 41 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 42 | this.accounts = JSON.parse(buffer.toString()); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 45 | get(username: string, isJams = false): string | undefined { |
| 46 | return this.accounts[isJams ? 'jams' : 'local'][username]; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 47 | } |
| 48 | |
Issam E. Maghni | b05ad99 | 2022-11-20 23:50:48 +0000 | [diff] [blame] | 49 | set(username: string, password: string, isJams = false): void { |
| 50 | this.accounts[isJams ? 'jams' : 'local'][username] = password; |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 51 | } |
| 52 | |
Misha Krieger-Raynauld | b933fbb | 2022-11-15 15:11:09 -0500 | [diff] [blame] | 53 | async save(): Promise<void> { |
| 54 | await writeFile(this.filename, JSON.stringify(this.accounts, null, 2) + '\n'); |
Michelle Sepkap Sime | 6967fb9 | 2022-11-08 08:39:36 -0500 | [diff] [blame] | 55 | } |
| 56 | } |