Skip to content
Snippets Groups Projects
Commit 7ea345e9 authored by Bernard Roland (Student Com20)'s avatar Bernard Roland (Student Com20)
Browse files

Merge branch 'deployment' into devel

parents 5b6931b1 f0b751ed
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......
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`;
}
......@@ -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 = [ "*" ];
......
......@@ -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> {
......
......@@ -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: {
......
......@@ -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,7 +31,12 @@ 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"] });
let decoded;
if (await usePublicAndPrivate()) {
decoded = await asyncify(verify, token, await getPublicKey(), { algorithms: ["ES384"] });
} else {
decoded = await asyncify(verify, token, getSecret(), { algorithms: ["HS384"] });
}
if (isOfType<Token>(decoded, [['id', 'string'], ['type', 'string']]) && decoded.type === authTokenType) {
req.body.token = decoded;
}
......@@ -58,7 +63,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 {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment