From ded7c1e718c38ebebe17315b3c93301aa2b36a40 Mon Sep 17 00:00:00 2001 From: Roland Bernard <rolbernard@unibz.it> Date: Thu, 20 May 2021 14:32:10 +0200 Subject: [PATCH] Simplified development setup requirements --- client/src/adapters/auth.ts | 2 +- client/src/config.ts | 8 +++++++- server/src/config.ts | 1 + server/src/keys.ts | 19 +++++++++++++++++++ server/src/knexconfig.ts | 2 +- server/src/v1/auth.ts | 18 +++++++++++++----- 6 files changed, 42 insertions(+), 8 deletions(-) diff --git a/client/src/adapters/auth.ts b/client/src/adapters/auth.ts index 5dbde55..48198a6 100644 --- a/client/src/adapters/auth.ts +++ b/client/src/adapters/auth.ts @@ -37,7 +37,7 @@ async function extendAccessToken() { if (response.ok) { const json = await response.json(); setToken(json.token); - } else if (response.status === 403) { + } else { clearToken(); } } catch(e) { diff --git a/client/src/config.ts b/client/src/config.ts index 38c8d39..f7959fd 100644 --- a/client/src/config.ts +++ b/client/src/config.ts @@ -1,3 +1,9 @@ -export const apiRoot = `${window.location.origin}/v1`; +export let apiRoot: string; + +if (process.env.NODE_ENV === 'production') { + apiRoot = `${window.location.origin}/v1`; +} else { + apiRoot = `http://localhost:8000/v1`; +} diff --git a/server/src/config.ts b/server/src/config.ts index 26fd98f..0e758a1 100644 --- a/server/src/config.ts +++ b/server/src/config.ts @@ -6,6 +6,7 @@ export const port = env.PORT ?? 8000; export const keys = { private: './keys/cert.key', public: './keys/cert.pem', + secret: 'SECRET', }; export const allowedOrigins = [ "*" ]; diff --git a/server/src/keys.ts b/server/src/keys.ts index 96e0edf..0c28f6b 100644 --- a/server/src/keys.ts +++ b/server/src/keys.ts @@ -3,6 +3,25 @@ import { readFileBuffer } from './util'; import { env } from 'process'; import { keys } from './config'; +let hasKeys: boolean | null = null; + +export async function usePublicAndPrivate(): Promise<boolean> { + if (hasKeys === null) { + try { + await getPrivateKey(); + await getPublicKey(); + hasKeys = true; + } catch(e) { + hasKeys = false; + } + } + return hasKeys; +} + +export function getSecret(): string { + return keys.secret; +} + let privateKey: string; export async function getPrivateKey(): Promise<string> { diff --git a/server/src/knexconfig.ts b/server/src/knexconfig.ts index 7f5a503..66f1238 100644 --- a/server/src/knexconfig.ts +++ b/server/src/knexconfig.ts @@ -3,7 +3,7 @@ import { env } from 'process'; import { join } from 'path'; import { parse } from 'pg-connection-string'; -const pgconfig: any = parse(env.DATABASE_URL ?? ''); +const pgconfig: any = parse(env.DATABASE_URL ?? 'postgresql://postgres@localhost/ryoko'); export default { development: { diff --git a/server/src/v1/auth.ts b/server/src/v1/auth.ts index eb667d2..01418d9 100644 --- a/server/src/v1/auth.ts +++ b/server/src/v1/auth.ts @@ -6,7 +6,7 @@ import { sign, verify } from 'jsonwebtoken'; import database from '../database'; import { isOfType, asyncify } from '../util'; -import { getPublicKey, getPrivateKey } from '../keys'; +import { getPublicKey, getPrivateKey, getSecret, usePublicAndPrivate } from '../keys'; const auth = express(); @@ -31,9 +31,13 @@ export async function tokenVerification(req: Request, _res: Response, next: Next if (token) { delete req.body.token; try { - const decoded = await asyncify(verify, token, await getPublicKey(), { algorithms: ["ES384"] }); - if (isOfType<Token>(decoded, [['id', 'string'], ['type', 'string']]) && decoded.type === authTokenType) { - req.body.token = decoded; + if (await usePublicAndPrivate()) { + const decoded = await asyncify(verify, token, await getPublicKey(), { algorithms: ["ES384"] }); + if (isOfType<Token>(decoded, [['id', 'string'], ['type', 'string']]) && decoded.type === authTokenType) { + req.body.token = decoded; + } + } else { + return asyncify(verify, token, getSecret(), { algorithms: ["HS384"] }); } } catch (err) { /* Token has already been deleted */ } next(); @@ -58,7 +62,11 @@ async function generateAuthToken(id: string) { id: id, type: authTokenType, }; - return asyncify(sign, token, await getPrivateKey(), { algorithm: "ES384", expiresIn: 60 * 60 }); + if (await usePublicAndPrivate()) { + return asyncify(sign, token, await getPrivateKey(), { algorithm: "ES384", expiresIn: 60 * 60 }); + } else { + return asyncify(sign, token, getSecret(), { algorithm: "HS384", expiresIn: 60 * 60 }); + } } interface RegisterBody { -- GitLab