diff --git a/packages/core/package.json b/packages/core/package.json index f11372fd4..6fc6bc11a 100644 --- a/packages/core/package.json +++ b/packages/core/package.json @@ -13,7 +13,7 @@ }, "dependencies": { "@logto/essentials": "^1.0.5", - "@logto/schemas": "^1.0.5", + "@logto/schemas": "^1.0.6", "dayjs": "^1.10.5", "dotenv": "^10.0.0", "got": "^11.8.2", diff --git a/packages/core/src/oidc/adapter.ts b/packages/core/src/oidc/adapter.ts index 645c6878b..e57b72b7a 100644 --- a/packages/core/src/oidc/adapter.ts +++ b/packages/core/src/oidc/adapter.ts @@ -2,7 +2,11 @@ import dayjs from 'dayjs'; import { AdapterFactory } from 'oidc-provider'; import { IdentifierSqlTokenType, sql, ValueExpressionType } from 'slonik'; import { conditional } from '@logto/essentials'; -import { OidcModelInstances, OidcModelInstanceDBEntry } from '@logto/schemas'; +import { + OidcModelInstances, + OidcModelInstanceDBEntry, + OidcModelInstancePayload, +} from '@logto/schemas'; import pool from '@/database/pool'; import { convertToIdentifiers } from '@/database/utils'; @@ -37,43 +41,43 @@ export default function postgresAdapter(modelName: string) { return convertResult(result); }; + const findByPayloadField = async < + T extends ValueExpressionType, + Field extends keyof OidcModelInstancePayload + >( + field: Field, + value: T + ) => { + const result = await pool.maybeOne(sql` + select ${fields.payload}, ${fields.consumedAt} + from ${table} + where ${fields.modelName}=${modelName} + and ${fields.payload}->>${field}=${value} + `); + + return convertResult(result); + }; + const adapter: ReturnType = { upsert: async (id, payload, expiresIn) => { await pool.query(sql` insert into ${table} (${sql.join( - [ - fields.modelName, - fields.id, - fields.payload, - fields.expiresAt, - fields.userCode, - fields.uid, - fields.grantId, - ], + [fields.modelName, fields.id, fields.payload, fields.expiresAt], sql`, ` )}) values ( ${modelName}, ${id}, ${JSON.stringify(payload)}, - ${dayjs().add(expiresIn, 'second').unix()}, - ${payload.userCode ?? null}, - ${payload.uid ?? null}, - ${payload.grantId ?? null} + ${dayjs().add(expiresIn, 'second').unix()} ) on conflict (${fields.modelName}, ${fields.id}) do update - set ${setExcluded( - fields.payload, - fields.expiresAt, - fields.userCode, - fields.uid, - fields.grantId - )} + set ${setExcluded(fields.payload, fields.expiresAt)} `); }, find: async (id) => findByField(fields.id, id), - findByUserCode: async (userCode) => findByField(fields.userCode, userCode), - findByUid: async (uid) => findByField(fields.uid, uid), + findByUserCode: async (userCode) => findByPayloadField('userCode', userCode), + findByUid: async (uid) => findByPayloadField('uid', uid), consume: async (id) => { await pool.query(sql` update ${table} @@ -93,7 +97,7 @@ export default function postgresAdapter(modelName: string) { await pool.query(sql` delete from ${table} where ${fields.modelName}=${modelName} - and ${fields.grantId}=${grantId} + and ${fields.payload}->>'grantId'=${grantId} `); }, }; diff --git a/packages/core/yarn.lock b/packages/core/yarn.lock index 7eae37e23..ae26f4172 100644 --- a/packages/core/yarn.lock +++ b/packages/core/yarn.lock @@ -396,10 +396,10 @@ lodash.orderby "^4.6.0" lodash.pick "^4.4.0" -"@logto/schemas@^1.0.5": - version "1.0.5" - resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.5.tgz#7784ca8f58569bfe1feafb4ad5ca6f098b1a557e" - integrity sha512-JxA9uXNz2tt0NqAp8Gl0hp0L/dLhZakG5sm41JrJKfhtse2Yy2K1X7gBYkH1C9P8P+NcHhv/K4pEgY66yXMOGQ== +"@logto/schemas@^1.0.6": + version "1.0.6" + resolved "https://registry.yarnpkg.com/@logto/schemas/-/schemas-1.0.6.tgz#ef72c883b29d698a0d5b1075eee0a0de8ab80191" + integrity sha512-rQncFIl4cs3kZfXUTC6nrvDA1sdTlSn7ibQoZ0sjuXalqw6iGhpkQ/iQUYibkJMASc2KOtndMd47FwnQGISNUQ== "@mrmlnc/readdir-enhanced@^2.2.1": version "2.2.1"