0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

refactor(core): add user ip to webhook event payload (#5049)

This commit is contained in:
Xiao Yijun 2023-12-04 14:06:28 +08:00 committed by GitHub
parent 3bab5351a6
commit 3e92a20320
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 25 additions and 1 deletions

View file

@ -0,0 +1,6 @@
---
"@logto/schemas": patch
"@logto/core": patch
---
refactor: add user ip to webhook event payload

View file

@ -27,6 +27,7 @@ export type InteractionHookContext = {
event: InteractionEvent;
sessionId?: string;
applicationId?: string;
userIp?: string;
};
/**
@ -59,7 +60,7 @@ export const createHookLibrary = (queries: Queries) => {
userAgent?: string
) => {
const { userId } = interactionResult;
const { event, sessionId, applicationId } = interactionContext;
const { event, sessionId, applicationId, userIp } = interactionContext;
const hookEvent = eventToHook[event];
const found = await findAllHooks();
@ -84,6 +85,7 @@ export const createHookLibrary = (queries: Queries) => {
sessionId,
userAgent,
userId,
userIp,
user: user && pick(user, ...userInfoSelectFields),
application: application && pick(application, 'id', 'type', 'name', 'description'),
} satisfies Omit<HookEventPayload, 'hookId'>;

View file

@ -51,6 +51,7 @@ export const generateHookTestPayload = (hookId: string, event: HookEvent): HookE
sessionId: 'fake-session-id',
userAgent: 'fake-user-agent',
userId: fakeUserId,
userIp: 'fake-user-ip',
user: {
id: fakeUserId,
username: 'fake-user',

View file

@ -35,6 +35,7 @@ export default function koaInteractionHooks<
const {
interactionDetails,
header: { 'user-agent': userAgent },
ip,
} = ctx;
// Predefined interaction hook context
@ -42,6 +43,7 @@ export default function koaInteractionHooks<
event,
sessionId: interactionDetails.jti,
applicationId: conditionalString(interactionDetails.params.client_id),
userIp: ip,
};
// eslint-disable-next-line @silverhand/fp/no-let

View file

@ -129,6 +129,12 @@ describe('trigger hooks', () => {
const { username, password } = generateNewUserProfile({ username: true, password: true });
const userId = await registerNewUser(username, password);
type HookRequest = {
body: {
userIp?: string;
} & Record<string, unknown>;
};
// Check hook trigger log
for (const [hook, expectedResult, expectedError] of [
[hook1, LogResult.Error, 'RequestError: Invalid URL'],
@ -140,6 +146,12 @@ describe('trigger hooks', () => {
hook.id,
new URLSearchParams({ logKey, page_size: '100' })
);
// Assert user ip is in the hook request
expect(
logs.every(({ payload }) => (payload.hookRequest as HookRequest).body.userIp)
).toBeTruthy();
expect(
logs.some(
({ payload: { result, error } }) =>

View file

@ -12,6 +12,7 @@ export type HookEventPayload = {
sessionId?: string;
userAgent?: string;
userId?: string;
userIp?: string;
user?: Pick<User, (typeof userInfoSelectFields)[number]>;
application?: Pick<Application, 'id' | 'type' | 'name' | 'description'>;
} & Record<string, unknown>;