From 4f616696e251bac8a38d97643ce199941267c4ee Mon Sep 17 00:00:00 2001 From: Gao Sun Date: Fri, 2 Jul 2021 21:14:18 +0800 Subject: [PATCH] feat: find user account from db, close #4 --- packages/core/package.json | 4 ++-- packages/core/src/database/types.ts | 0 packages/core/src/init/oidc.ts | 8 +++++--- packages/core/src/queries/user.ts | 13 +++++++++++++ packages/core/yarn.lock | 16 ++++++++-------- 5 files changed, 28 insertions(+), 13 deletions(-) delete mode 100644 packages/core/src/database/types.ts create mode 100644 packages/core/src/queries/user.ts diff --git a/packages/core/package.json b/packages/core/package.json index d5e7ee208..21f7bdb16 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -12,8 +12,8 @@ "dev": "tsc-watch --onSuccess \"node ./build/index.js\"" }, "dependencies": { - "@logto/essentials": "^1.0.2", - "@logto/schemas": "^1.0.0", + "@logto/essentials": "^1.0.5", + "@logto/schemas": "^1.0.1", "dayjs": "^1.10.5", "dotenv": "^10.0.0", "koa": "^2.13.1", diff --git a/packages/core/src/database/types.ts b/packages/core/src/database/types.ts deleted file mode 100644 index e69de29bb..000000000 diff --git a/packages/core/src/init/oidc.ts b/packages/core/src/init/oidc.ts index 5244db7f6..bbedcf513 100644 --- a/packages/core/src/init/oidc.ts +++ b/packages/core/src/init/oidc.ts @@ -6,6 +6,7 @@ import postgresAdapter from '../oidc/adapter'; import { fromKeyLike } from 'jose/jwk/from_key_like'; import { getEnv } from '../utils'; +import { findUserById } from '../queries/user'; export default async function initOidc(app: Koa, port: number): Promise { const privateKey = crypto.createPrivateKey( @@ -35,11 +36,12 @@ export default async function initOidc(app: Koa, port: number): Promise { ], features: { revocation: { enabled: true }, introspection: { enabled: true } }, clientBasedCORS: (ctx, origin) => { - console.log('rogin', origin); + console.log('origin', origin); return origin.startsWith('http://localhost:3000'); }, - findAccount: (ctx, sub) => { - console.log('finding account'); + findAccount: async (ctx, sub) => { + await findUserById(sub); + return { accountId: sub, claims: async (use, scope, claims) => { diff --git a/packages/core/src/queries/user.ts b/packages/core/src/queries/user.ts new file mode 100644 index 000000000..19a9b8eed --- /dev/null +++ b/packages/core/src/queries/user.ts @@ -0,0 +1,13 @@ +import { UserDBEntry, Users } from '@logto/schemas'; +import { sql } from 'slonik'; +import pool from '../database/pool'; +import { convertToIdentifiers } from '../database/utils'; + +const { table, fields } = convertToIdentifiers(Users); + +export const findUserById = async (id: string) => + pool.one(sql` + select ${sql.join(Object.values(fields), sql`,`)} + from ${table} + where id=${id} +`); diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock index 28e33a410..1a0580ccc 100644 --- a/packages/core/yarn.lock +++ b/packages/core/yarn.lock @@ -388,18 +388,18 @@ dependencies: vary "^1.1.2" -"@logto/essentials@^1.0.2": - version "1.0.2" - resolved "https://registry.yarnpkg.com/@logto/essentials/-/essentials-1.0.2.tgz#02a8c878b4079a63709c35ce8c143370f259bb13" - integrity sha512-GN6m1c5ll7EhOgJbmFwRESkeWGjdoihst8O1aVIxvEOXhLe/79bph4hejsdm59piX3jgKWakEHimpqcBT1kO+Q== +"@logto/essentials@^1.0.5": + version "1.0.5" + resolved "https://registry.yarnpkg.com/@logto/essentials/-/essentials-1.0.5.tgz#edc1a376cf82e8829adcf26e8b98db1235cfbfcd" + integrity sha512-zft8VodNOtkhyyQTHiOlksk6UonQBY68dON4cQaUOlpN5JS9z5/zchi4I0j+XB6EeQ7l8ZtVXct2VZBp8m03kw== dependencies: lodash.orderby "^4.6.0" lodash.pick "^4.4.0" -"@logto/schemas@^1.0.0": - version "1.0.0" - resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.0.tgz#5dbeec9caa9b1882b00b7507b8f6f337c8665ca9" - integrity sha512-+HQOra9CQgco4PwC3bAuzwAbEtD+Vf6tRLMWbzES5BXM3TMTdUasSQnbG2lRwWdudvmCwYM08Z0ZXipwmEstIg== +"@logto/schemas@^1.0.1": + version "1.0.1" + resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.1.tgz#786866a280568b275b9c55c2b884b5864dc42a06" + integrity sha512-SwAz/rOE61RDBU3TzsDqO30vkLRgD/Jcc3H3tC40gl39wTfxWiTrXgGtWTxAhrhdTwstEMa5+5ozczP/LvopwQ== "@mrmlnc/readdir-enhanced@^2.2.1": version "2.2.1"