blob: 8e48c945bac98d3121ac88b51b7dd866cf19e76b [file] [log] [blame]
Issam E. Maghnif796a092022-10-09 20:25:26 +00001/*
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-Raynauld242560f2022-10-16 19:59:58 -040018import { importPKCS8, importSPKI, KeyLike } from 'jose';
Issam E. Maghnif796a092022-10-09 20:25:26 +000019import { Service } from 'typedi';
20
21@Service()
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050022export class SigningKeys {
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040023 privateKey!: KeyLike;
24 publicKey!: KeyLike;
Issam E. Maghnif796a092022-10-09 20:25:26 +000025
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040026 async build() {
simon7d4386c2022-10-26 17:47:59 -040027 const privatekey = process.env.PRIVATE_KEY;
28 const publicKey = process.env.PUBLIC_KEY;
Misha Krieger-Raynauld242560f2022-10-16 19:59:58 -040029
Misha Krieger-Raynauldb933fbb2022-11-15 15:11:09 -050030 if (privatekey === undefined || publicKey === undefined) {
simon7d4386c2022-10-26 17:47:59 -040031 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-Raynauld242560f2022-10-16 19:59:58 -040036
37 return this;
Issam E. Maghnif796a092022-10-09 20:25:26 +000038 }
39}