From e01042cbcd77c486afa1ee9fc2fa5c1d2df92542 Mon Sep 17 00:00:00 2001 From: wangsijie Date: Wed, 14 Sep 2022 11:35:49 +0800 Subject: [PATCH] feat(core): add ts to interaction result (#1917) --- packages/core/src/lib/session.ts | 20 +++++++++++++++---- .../src/routes/session/passwordless.test.ts | 12 +++++++---- .../core/src/routes/session/social.test.ts | 9 ++++++--- .../routes/session/username-password.test.ts | 6 ++++-- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/packages/core/src/lib/session.ts b/packages/core/src/lib/session.ts index d08a684ac..cc678b630 100644 --- a/packages/core/src/lib/session.ts +++ b/packages/core/src/lib/session.ts @@ -1,3 +1,4 @@ +import dayjs from 'dayjs'; import { Context } from 'koa'; import { InteractionResults, Provider } from 'oidc-provider'; @@ -14,20 +15,31 @@ export const assignInteractionResults = async ( // have to do it manually // refer to: https://github.com/panva/node-oidc-provider/blob/c243bf6b6663c41ff3e75c09b95fb978eba87381/lib/actions/authorization/interactions.js#L106 const details = merge ? await provider.interactionDetails(ctx.req, ctx.res) : undefined; + const ts = dayjs().unix(); + const mergedResult = { + // Merge with current result + ...details?.result, + ...result, + }; const redirectTo = await provider.interactionResult( ctx.req, ctx.res, { - // Merge with current result - ...details?.result, - ...result, + ...mergedResult, + login: mergedResult.login + ? { + ...mergedResult.login, + // Update ts(timestamp) if the accountId is been set in result + ts: result.login?.accountId ? ts : mergedResult.login.ts, + } + : undefined, }, { mergeWithLastSubmission: merge, } ); - ctx.body = { redirectTo }; + ctx.body = { redirectTo, ts }; }; export const saveUserFirstConsentedAppId = async (userId: string, applicationId: string) => { diff --git a/packages/core/src/routes/session/passwordless.test.ts b/packages/core/src/routes/session/passwordless.test.ts index a2cedc0c8..f8019d5f6 100644 --- a/packages/core/src/routes/session/passwordless.test.ts +++ b/packages/core/src/routes/session/passwordless.test.ts @@ -97,7 +97,8 @@ describe('session -> passwordlessRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'id' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'id' }) }), expect.anything() ); }); @@ -146,7 +147,8 @@ describe('session -> passwordlessRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'id' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'id' }) }), expect.anything() ); }); @@ -214,7 +216,8 @@ describe('session -> passwordlessRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); }); @@ -291,7 +294,8 @@ describe('session -> passwordlessRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); }); diff --git a/packages/core/src/routes/session/social.test.ts b/packages/core/src/routes/session/social.test.ts index 98ca8cdcf..84a52a5db 100644 --- a/packages/core/src/routes/session/social.test.ts +++ b/packages/core/src/routes/session/social.test.ts @@ -223,7 +223,8 @@ describe('session -> socialRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'id' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'id' }) }), expect.anything() ); }); @@ -309,7 +310,8 @@ describe('session -> socialRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); }); @@ -346,7 +348,8 @@ describe('session -> socialRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); }); diff --git a/packages/core/src/routes/session/username-password.test.ts b/packages/core/src/routes/session/username-password.test.ts index d241ac5a9..faf480a5e 100644 --- a/packages/core/src/routes/session/username-password.test.ts +++ b/packages/core/src/routes/session/username-password.test.ts @@ -111,7 +111,8 @@ describe('sessionRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); }); @@ -180,7 +181,8 @@ describe('sessionRoutes', () => { expect(interactionResult).toHaveBeenCalledWith( expect.anything(), expect.anything(), - expect.objectContaining({ login: { accountId: 'user1' } }), + // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment + expect.objectContaining({ login: expect.objectContaining({ accountId: 'user1' }) }), expect.anything() ); });