From 8c95b5d8564a97754de1b4d20f3cfaaf60b36970 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 9 Jul 2021 23:25:24 +0800 Subject: [PATCH] refactor: read issuer from env and fallback to localhost --- packages/core/src/consts.ts | 5 ++++- packages/core/src/index.ts | 4 +--- packages/core/src/init/index.ts | 5 +++-- packages/core/src/init/oidc.ts | 6 +++--- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/packages/core/src/consts.ts b/packages/core/src/consts.ts index c3836e33b..850ccb256 100644 --- a/packages/core/src/consts.ts +++ b/packages/core/src/consts.ts @@ -1,4 +1,4 @@ -import { assertEnv } from '@/utils/env'; +import { assertEnv, getEnv } from '@/utils/env'; const signIn = assertEnv('UI_SIGN_IN_ROUTE'); @@ -8,3 +8,6 @@ export const routes = Object.freeze({ consent: signIn + '/consent', }, }); + +export const port = Number(getEnv('PORT', '3001')); +export const oidcIssuer = getEnv('OIDC_ISSUER', `http://localhost:${port}/oidc`); diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 77f47cd8c..4c61c03ff 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -5,14 +5,12 @@ dotenv.config(); import Koa from 'koa'; import initApp from './init'; -import { getEnv } from './utils/env'; const app = new Koa(); -const port = Number(getEnv('PORT', '3001')); (async () => { try { - await initApp(app, port); + await initApp(app); } catch (error: unknown) { console.log('Error while initializing app', error); } diff --git a/packages/core/src/init/index.ts b/packages/core/src/init/index.ts index b85181884..2a507dd26 100644 --- a/packages/core/src/init/index.ts +++ b/packages/core/src/init/index.ts @@ -4,12 +4,13 @@ import koaLogger from 'koa-logger'; import koaErrorHandler from '@/middleware/koa-error-handler'; import initOidc from './oidc'; import initRouter from './router'; +import { port } from '@/consts'; -export default async function initApp(app: Koa, port: number): Promise { +export default async function initApp(app: Koa): Promise { app.use(koaErrorHandler()); app.use(koaLogger()); - const provider = await initOidc(app, port); + const provider = await initOidc(app); initRouter(app, provider); app.listen(port, () => { diff --git a/packages/core/src/init/oidc.ts b/packages/core/src/init/oidc.ts index 9241dcdf9..8d966cbd4 100644 --- a/packages/core/src/init/oidc.ts +++ b/packages/core/src/init/oidc.ts @@ -7,9 +7,9 @@ import postgresAdapter from '@/oidc/adapter'; import { fromKeyLike } from 'jose/jwk/from_key_like'; import { getEnv } from '@/utils/env'; import { findUserById } from '@/queries/user'; -import { routes } from '@/consts'; +import { oidcIssuer, routes } from '@/consts'; -export default async function initOidc(app: Koa, port: number): Promise { +export default async function initOidc(app: Koa): Promise { const privateKey = crypto.createPrivateKey( Buffer.from(getEnv('OIDC_PROVIDER_PRIVATE_KEY_BASE64'), 'base64') ); @@ -19,7 +19,7 @@ export default async function initOidc(app: Koa, port: number): Promise { console.log('OIDC error', error);