Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [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 | */ |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 18 | import { importPKCS8, importSPKI, KeyLike } from 'jose'; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 19 | import { Service } from 'typedi'; |
| 20 | |
| 21 | @Service() |
| 22 | export class Vault { |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 23 | privateKey!: KeyLike; |
| 24 | publicKey!: KeyLike; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 25 | |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 26 | async build() { |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 27 | const privatekey = process.env.PRIVATE_KEY; |
| 28 | const publicKey = process.env.PUBLIC_KEY; |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 29 | |
simon | 7d4386c | 2022-10-26 17:47:59 -0400 | [diff] [blame] | 30 | if (!privatekey || !publicKey) { |
| 31 | throw new Error('Missing private or public key environment variables. Try running "npm run genkeys"'); |
| 32 | } |
| 33 | |
| 34 | this.privateKey = await importPKCS8(privatekey, 'EdDSA'); |
| 35 | this.publicKey = await importSPKI(publicKey, 'EdDSA'); |
Misha Krieger-Raynauld | 242560f | 2022-10-16 19:59:58 -0400 | [diff] [blame] | 36 | |
| 37 | return this; |
Issam E. Maghni | f796a09 | 2022-10-09 20:25:26 +0000 | [diff] [blame] | 38 | } |
| 39 | } |