mirror of
https://github.com/logto-io/logto.git
synced 2025-01-06 20:40:08 -05:00
refactor: read issuer from env and fallback to localhost
This commit is contained in:
parent
d28bf9f42a
commit
10ef252630
4 changed files with 11 additions and 9 deletions
|
@ -1,4 +1,4 @@
|
||||||
import { assertEnv } from '@/utils/env';
|
import { assertEnv, getEnv } from '@/utils/env';
|
||||||
|
|
||||||
const signIn = assertEnv('UI_SIGN_IN_ROUTE');
|
const signIn = assertEnv('UI_SIGN_IN_ROUTE');
|
||||||
|
|
||||||
|
@ -8,3 +8,6 @@ export const routes = Object.freeze({
|
||||||
consent: signIn + '/consent',
|
consent: signIn + '/consent',
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
export const port = Number(getEnv('PORT', '3001'));
|
||||||
|
export const oidcIssuer = getEnv('OIDC_ISSUER', `http://localhost:${port}/oidc`);
|
||||||
|
|
|
@ -5,14 +5,12 @@ dotenv.config();
|
||||||
|
|
||||||
import Koa from 'koa';
|
import Koa from 'koa';
|
||||||
import initApp from './init';
|
import initApp from './init';
|
||||||
import { getEnv } from './utils/env';
|
|
||||||
|
|
||||||
const app = new Koa();
|
const app = new Koa();
|
||||||
const port = Number(getEnv('PORT', '3001'));
|
|
||||||
|
|
||||||
(async () => {
|
(async () => {
|
||||||
try {
|
try {
|
||||||
await initApp(app, port);
|
await initApp(app);
|
||||||
} catch (error: unknown) {
|
} catch (error: unknown) {
|
||||||
console.log('Error while initializing app', error);
|
console.log('Error while initializing app', error);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,12 +4,13 @@ import koaLogger from 'koa-logger';
|
||||||
import koaErrorHandler from '@/middleware/koa-error-handler';
|
import koaErrorHandler from '@/middleware/koa-error-handler';
|
||||||
import initOidc from './oidc';
|
import initOidc from './oidc';
|
||||||
import initRouter from './router';
|
import initRouter from './router';
|
||||||
|
import { port } from '@/consts';
|
||||||
|
|
||||||
export default async function initApp(app: Koa, port: number): Promise<void> {
|
export default async function initApp(app: Koa): Promise<void> {
|
||||||
app.use(koaErrorHandler());
|
app.use(koaErrorHandler());
|
||||||
app.use(koaLogger());
|
app.use(koaLogger());
|
||||||
|
|
||||||
const provider = await initOidc(app, port);
|
const provider = await initOidc(app);
|
||||||
initRouter(app, provider);
|
initRouter(app, provider);
|
||||||
|
|
||||||
app.listen(port, () => {
|
app.listen(port, () => {
|
||||||
|
|
|
@ -7,9 +7,9 @@ import postgresAdapter from '@/oidc/adapter';
|
||||||
import { fromKeyLike } from 'jose/jwk/from_key_like';
|
import { fromKeyLike } from 'jose/jwk/from_key_like';
|
||||||
import { getEnv } from '@/utils/env';
|
import { getEnv } from '@/utils/env';
|
||||||
import { findUserById } from '@/queries/user';
|
import { findUserById } from '@/queries/user';
|
||||||
import { routes } from '@/consts';
|
import { oidcIssuer, routes } from '@/consts';
|
||||||
|
|
||||||
export default async function initOidc(app: Koa, port: number): Promise<Provider> {
|
export default async function initOidc(app: Koa): Promise<Provider> {
|
||||||
const privateKey = crypto.createPrivateKey(
|
const privateKey = crypto.createPrivateKey(
|
||||||
Buffer.from(getEnv('OIDC_PROVIDER_PRIVATE_KEY_BASE64'), 'base64')
|
Buffer.from(getEnv('OIDC_PROVIDER_PRIVATE_KEY_BASE64'), 'base64')
|
||||||
);
|
);
|
||||||
|
@ -19,7 +19,7 @@ export default async function initOidc(app: Koa, port: number): Promise<Provider
|
||||||
path: '/',
|
path: '/',
|
||||||
signed: true,
|
signed: true,
|
||||||
} as const);
|
} as const);
|
||||||
const oidc = new Provider(`http://localhost:${port}/oidc`, {
|
const oidc = new Provider(oidcIssuer, {
|
||||||
adapter: postgresAdapter,
|
adapter: postgresAdapter,
|
||||||
renderError: (ctx, out, error) => {
|
renderError: (ctx, out, error) => {
|
||||||
console.log('OIDC error', error);
|
console.log('OIDC error', error);
|
||||||
|
|
Loading…
Reference in a new issue