0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-02-17 22:04:19 -05:00

refactor(core): move connector session read/write to social verification (#3072)

This commit is contained in:
Darcy Ye 2023-02-09 09:09:18 +08:00 committed by GitHub
parent 514930eb21
commit eea02cca70
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 41 deletions

View file

@ -1,5 +1,3 @@
import type { ConnectorSession } from '@logto/connector-kit';
import { connectorSessionGuard } from '@logto/connector-kit';
import type { Profile } from '@logto/schemas'; import type { Profile } from '@logto/schemas';
import { InteractionEvent } from '@logto/schemas'; import { InteractionEvent } from '@logto/schemas';
import { assert } from '@silverhand/essentials'; import { assert } from '@silverhand/essentials';
@ -7,7 +5,6 @@ import type { Context } from 'koa';
import { errors } from 'oidc-provider'; import { errors } from 'oidc-provider';
import type { InteractionResults } from 'oidc-provider'; import type { InteractionResults } from 'oidc-provider';
import type Provider from 'oidc-provider'; import type Provider from 'oidc-provider';
import { z } from 'zod';
import RequestError from '#src/errors/RequestError/index.js'; import RequestError from '#src/errors/RequestError/index.js';
import assertThat from '#src/utils/assert-that.js'; import assertThat from '#src/utils/assert-that.js';
@ -135,40 +132,6 @@ export const clearInteractionStorage = async (ctx: Context, provider: Provider)
} }
}; };
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;
};
/** /**
* The following three methods (`getInteractionFromProviderByJti`, `assignResultToInteraction` * The following three methods (`getInteractionFromProviderByJti`, `assignResultToInteraction`
* and `epochTime`) refer to implementation in * and `epochTime`) refer to implementation in

View file

@ -1,12 +1,12 @@
import type { ConnectorSession, SocialUserInfo } from '@logto/connector-kit'; import type { ConnectorSession, SocialUserInfo } from '@logto/connector-kit';
import { connectorSessionGuard } from '@logto/connector-kit';
import type { SocialConnectorPayload } from '@logto/schemas'; import type { SocialConnectorPayload } from '@logto/schemas';
import { ConnectorType } from '@logto/schemas'; import { ConnectorType } from '@logto/schemas';
import type { Context } from 'koa';
import type Provider from 'oidc-provider';
import { z } from 'zod';
import type { WithLogContext } from '#src/middleware/koa-audit-log.js'; import type { WithLogContext } from '#src/middleware/koa-audit-log.js';
import {
assignConnectorSessionResult,
getConnectorSessionResult,
} from '#src/routes/interaction/utils/interaction.js';
import type TenantContext from '#src/tenants/TenantContext.js'; import type TenantContext from '#src/tenants/TenantContext.js';
import assertThat from '#src/utils/assert-that.js'; import assertThat from '#src/utils/assert-that.js';
@ -73,3 +73,37 @@ export const verifySocialIdentity = async (
return userInfo; return userInfo;
}; };
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,
});
};
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;
};