Create JWT auth middleware and sample authenticated routes for /account
Changes:
- Create new middleware/auth.ts middleware to authenticate JWT
- Make vault.ts privateKey and publicKey fields to access them without await
- Remove @Service from auth router in auth-router.ts
- Create new AccountRouter with /account routes
Change-Id: Ie08651de7dbbce5d7596d80eba344707eb47d460
diff --git a/server/src/vault.ts b/server/src/vault.ts
index a0ae67c..45548db 100644
--- a/server/src/vault.ts
+++ b/server/src/vault.ts
@@ -17,18 +17,22 @@
*/
import { readFile } from 'node:fs/promises';
-import { importPKCS8, importSPKI } from 'jose';
+import { importPKCS8, importSPKI, KeyLike } from 'jose';
import { Service } from 'typedi';
@Service()
export class Vault {
- async privKey() {
- const privKeyBuf = await readFile('privkey.pem');
- return importPKCS8(privKeyBuf.toString(), 'EdDSA');
- }
+ privateKey!: KeyLike;
+ publicKey!: KeyLike;
- async pubKey() {
- const pubKeyBuf = await readFile('pubkey.pem');
- return importSPKI(pubKeyBuf.toString(), 'EdDSA');
+ // TODO: Convert to environment variables and check if defined
+ async build() {
+ const privateKeyBuffer = await readFile('privkey.pem');
+ this.privateKey = await importPKCS8(privateKeyBuffer.toString(), 'EdDSA');
+
+ const publicKeyBuffer = await readFile('pubkey.pem');
+ this.publicKey = await importSPKI(publicKeyBuffer.toString(), 'EdDSA');
+
+ return this;
}
}