From fca5482e5cfa8da602d641ae05a5980a6b005844 Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 2 Jul 2021 22:09:38 +0800 Subject: [PATCH] feat: redirect to configured sign in URL --- packages/core/src/consts.ts | 3 +++ packages/core/src/init/oidc.ts | 19 +++++++++++++++---- packages/core/src/init/router.ts | 5 +++-- packages/core/src/utils.ts | 7 +++++++ 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 packages/core/src/consts.ts diff --git a/packages/core/src/consts.ts b/packages/core/src/consts.ts new file mode 100644 index 000000000..8aa908d72 --- /dev/null +++ b/packages/core/src/consts.ts @@ -0,0 +1,3 @@ +import { assertEnv } from './utils'; + +export const signInRoute = assertEnv('SIGN_IN_ROUTE'); diff --git a/packages/core/src/init/oidc.ts b/packages/core/src/init/oidc.ts index bbedcf513..17d28f570 100644 --- a/packages/core/src/init/oidc.ts +++ b/packages/core/src/init/oidc.ts @@ -7,6 +7,7 @@ import postgresAdapter from '../oidc/adapter'; import { fromKeyLike } from 'jose/jwk/from_key_like'; import { getEnv } from '../utils'; import { findUserById } from '../queries/user'; +import { signInRoute } from '../consts'; export default async function initOidc(app: Koa, port: number): Promise { const privateKey = crypto.createPrivateKey( @@ -34,8 +35,15 @@ export default async function initOidc(app: Koa, port: number): Promise { token_endpoint_auth_method: 'none', }, ], - features: { revocation: { enabled: true }, introspection: { enabled: true } }, - clientBasedCORS: (ctx, origin) => { + features: { + revocation: { enabled: true }, + introspection: { enabled: true }, + devInteractions: { enabled: false }, + }, + interactions: { + url: (_, interaction) => `${signInRoute}?uid=${interaction.uid}`, + }, + clientBasedCORS: (_, origin) => { console.log('origin', origin); return origin.startsWith('http://localhost:3000'); }, @@ -44,8 +52,11 @@ export default async function initOidc(app: Koa, port: number): Promise { return { accountId: sub, - claims: async (use, scope, claims) => { - console.log('claims', use, scope, claims); + claims: async (use, scope, claims, rejected) => { + console.log('use:', use); + console.log('scope:', scope); + console.log('claims:', claims); + console.log('rejected:', rejected); return { sub }; }, }; diff --git a/packages/core/src/init/router.ts b/packages/core/src/init/router.ts index b7c35d86e..1b7246741 100644 --- a/packages/core/src/init/router.ts +++ b/packages/core/src/init/router.ts @@ -1,10 +1,11 @@ import Koa from 'koa'; import Router from 'koa-router'; +import { signInRoute } from '../consts'; const router = new Router(); -router.get('/callback', (ctx) => { - ctx.body = 'A callback'; +router.get(signInRoute, (ctx) => { + ctx.body = 'Signing in'; }); export default function initRouter(app: Koa): void { diff --git a/packages/core/src/utils.ts b/packages/core/src/utils.ts index 9fcaeeece..2e4f689ca 100644 --- a/packages/core/src/utils.ts +++ b/packages/core/src/utils.ts @@ -1,3 +1,5 @@ +import assert from 'assert'; + export type Optional = T | undefined; export type Falsy = 0 | undefined | null | false | ''; @@ -5,3 +7,8 @@ export const conditional = (value: T | Falsy): Optional => (value ? value export const conditionalString = (value: string | Falsy): string => (value ? value : ''); export const getEnv = (key: string, fallback = ''): string => process.env[key] ?? fallback; +export const assertEnv = (key: string): string => { + const value = process.env[key]; + assert(value, `env variable ${key} not found`); + return value; +};