mirror of
https://github.com/logto-io/logto.git
synced 2025-04-07 23:01:25 -05:00
refactor: move assign/get connector session to interaction path (#2706)
Co-authored-by: simeng-li <simeng@silverhand.io>
This commit is contained in:
parent
a7f217873c
commit
1b709db854
5 changed files with 44 additions and 44 deletions
|
@ -9,13 +9,13 @@ import RequestError from '#src/errors/RequestError/index.js';
|
|||
import { assignInteractionResults } from '#src/libraries/session.js';
|
||||
import koaAuditLog from '#src/middleware/koa-audit-log.js';
|
||||
import koaGuard from '#src/middleware/koa-guard.js';
|
||||
import { assignConnectorSessionResult } from '#src/routes/session/utils.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
|
||||
import type { AnonymousRouter } from '../types.js';
|
||||
import submitInteraction from './actions/submit-interaction.js';
|
||||
import { sendPasscodePayloadGuard, socialAuthorizationUrlPayloadGuard } from './types/guard.js';
|
||||
import {
|
||||
assignConnectorSessionResult,
|
||||
getInteractionStorage,
|
||||
storeInteractionResult,
|
||||
mergeIdentifiers,
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
import type { ConnectorSession } from '@logto/connector-kit';
|
||||
import { connectorSessionGuard } from '@logto/connector-kit';
|
||||
import type { Event, Profile } from '@logto/schemas';
|
||||
import type { Context } from 'koa';
|
||||
import type { Provider } from 'oidc-provider';
|
||||
import { z } from 'zod';
|
||||
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
|
@ -126,3 +129,37 @@ export const clearInteractionStorage = async (ctx: Context, provider: Provider)
|
|||
await provider.interactionResult(ctx.req, ctx.res, {});
|
||||
}
|
||||
};
|
||||
|
||||
export const assignConnectorSessionResult = async (
|
||||
ctx: Context,
|
||||
provider: Provider,
|
||||
connectorSession: ConnectorSession
|
||||
) => {
|
||||
const details = await provider.interactionDetails(ctx.req, ctx.res);
|
||||
await provider.interactionResult(ctx.req, ctx.res, {
|
||||
...details.result,
|
||||
connectorSession,
|
||||
});
|
||||
};
|
||||
|
||||
export const getConnectorSessionResult = async (
|
||||
ctx: Context,
|
||||
provider: Provider
|
||||
): Promise<ConnectorSession> => {
|
||||
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
|
||||
|
||||
const signInResult = z
|
||||
.object({
|
||||
connectorSession: connectorSessionGuard,
|
||||
})
|
||||
.safeParse(result);
|
||||
|
||||
assertThat(result && signInResult.success, 'session.connector_validation_session_not_found');
|
||||
|
||||
const { connectorSession, ...rest } = result;
|
||||
await provider.interactionResult(ctx.req, ctx.res, {
|
||||
...rest,
|
||||
});
|
||||
|
||||
return signInResult.data.connectorSession;
|
||||
};
|
||||
|
|
|
@ -10,7 +10,7 @@ import type { Provider } from 'oidc-provider';
|
|||
|
||||
import RequestError from '#src/errors/RequestError/index.js';
|
||||
import { verifyUserPassword } from '#src/libraries/user.js';
|
||||
import { getConnectorSessionResult } from '#src/routes/session/utils.js';
|
||||
import { getConnectorSessionResult } from '#src/routes/interaction/utils/interaction.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
|
||||
import type {
|
||||
|
|
|
@ -25,16 +25,15 @@ import {
|
|||
updateUserById,
|
||||
findUserByIdentity,
|
||||
} from '#src/queries/user.js';
|
||||
import {
|
||||
assignConnectorSessionResult,
|
||||
getConnectorSessionResult,
|
||||
} from '#src/routes/interaction/utils/interaction.js';
|
||||
import assertThat from '#src/utils/assert-that.js';
|
||||
import { maskUserInfo } from '#src/utils/format.js';
|
||||
|
||||
import type { AnonymousRouterLegacy } from '../types.js';
|
||||
import {
|
||||
checkRequiredProfile,
|
||||
getRoutePrefix,
|
||||
assignConnectorSessionResult,
|
||||
getConnectorSessionResult,
|
||||
} from './utils.js';
|
||||
import { checkRequiredProfile, getRoutePrefix } from './utils.js';
|
||||
|
||||
export const registerRoute = getRoutePrefix('register', 'social');
|
||||
export const signInRoute = getRoutePrefix('sign-in', 'social');
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
import type { ConnectorSession } from '@logto/connector-kit';
|
||||
import { connectorSessionGuard } from '@logto/connector-kit';
|
||||
import type { PasscodeType, SignInExperience, User } from '@logto/schemas';
|
||||
import { SignInIdentifier } from '@logto/schemas';
|
||||
import type { LogPayload, LogType } from '@logto/schemas/lib/types/log-legacy.js';
|
||||
|
@ -157,40 +155,6 @@ export const getContinueSignInResult = async (
|
|||
return rest;
|
||||
};
|
||||
|
||||
export const assignConnectorSessionResult = async (
|
||||
ctx: Context,
|
||||
provider: Provider,
|
||||
connectorSession: ConnectorSession
|
||||
) => {
|
||||
const details = await provider.interactionDetails(ctx.req, ctx.res);
|
||||
await provider.interactionResult(ctx.req, ctx.res, {
|
||||
...details.result,
|
||||
connectorSession,
|
||||
});
|
||||
};
|
||||
|
||||
export const getConnectorSessionResult = async (
|
||||
ctx: Context,
|
||||
provider: Provider
|
||||
): Promise<ConnectorSession> => {
|
||||
const { result } = await provider.interactionDetails(ctx.req, ctx.res);
|
||||
|
||||
const signInResult = z
|
||||
.object({
|
||||
connectorSession: connectorSessionGuard,
|
||||
})
|
||||
.safeParse(result);
|
||||
|
||||
assertThat(result && signInResult.success, 'session.connector_validation_session_not_found');
|
||||
|
||||
const { connectorSession, ...rest } = result;
|
||||
await provider.interactionResult(ctx.req, ctx.res, {
|
||||
...rest,
|
||||
});
|
||||
|
||||
return signInResult.data.connectorSession;
|
||||
};
|
||||
|
||||
export const isUserPasswordSet = ({
|
||||
passwordEncrypted,
|
||||
identities,
|
||||
|
|
Loading…
Add table
Reference in a new issue