Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authentication events #998

Merged
merged 15 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions src/common/interfaces/event.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {
} from '../../organizations/interfaces';
import { Connection, ConnectionResponse } from '../../sso/interfaces';
import {
AuthenticationEvent,
AuthenticationEventResponse,
EmailVerificationEvent,
EmailVerificationEventResponse,
InvitationEvent,
Expand Down Expand Up @@ -44,6 +46,95 @@ interface EventResponseBase {
created_at: string;
}

export interface AuthenticationEmailVerificationSucceededEvent
extends EventBase {
event: 'authentication.email_verification_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationEmailVerificationSucceededEventResponse
extends EventResponseBase {
event: 'authentication.email_verification_succeeded';
data: AuthenticationEventResponse;
}

export interface AuthenticationMagicAuthFailedEvent extends EventBase {
event: 'authentication.magic_auth_failed';
data: AuthenticationEvent;
}

export interface AuthenticationMagicAuthFailedEventResponse
extends EventResponseBase {
event: 'authentication.magic_auth_failed';
data: AuthenticationEventResponse;
}

export interface AuthenticationMagicAuthSucceededEvent extends EventBase {
event: 'authentication.magic_auth_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationMagicAuthSucceededEventResponse
extends EventResponseBase {
event: 'authentication.magic_auth_succeeded';
data: AuthenticationEventResponse;
}

export interface AuthenticationMfaSucceededEvent extends EventBase {
event: 'authentication.mfa_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationMfaSucceededEventResponse
extends EventResponseBase {
event: 'authentication.mfa_succeeded';
data: AuthenticationEventResponse;
}

export interface AuthenticationOAuthSucceededEvent extends EventBase {
event: 'authentication.oauth_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationOAuthSucceededEventResponse
extends EventResponseBase {
event: 'authentication.oauth_succeeded';
data: AuthenticationEventResponse;
}

export interface AuthenticationPasswordFailedEvent extends EventBase {
event: 'authentication.password_failed';
data: AuthenticationEvent;
}

export interface AuthenticationPasswordFailedEventResponse
extends EventResponseBase {
event: 'authentication.password_failed';
data: AuthenticationEventResponse;
}

export interface AuthenticationPasswordSucceededEvent extends EventBase {
event: 'authentication.password_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationPasswordSucceededEventResponse
extends EventResponseBase {
event: 'authentication.password_succeeded';
data: AuthenticationEventResponse;
}

export interface AuthenticationSSOSucceededEvent extends EventBase {
event: 'authentication.sso_succeeded';
data: AuthenticationEvent;
}

export interface AuthenticationSSOSucceededEventResponse
extends EventResponseBase {
event: 'authentication.sso_succeeded';
data: AuthenticationEventResponse;
}

export interface ConnectionActivatedEvent extends EventBase {
event: 'connection.activated';
data: Connection;
Expand Down Expand Up @@ -398,6 +489,14 @@ export interface SessionCreatedEventResponse extends EventResponseBase {
}

export type Event =
| AuthenticationEmailVerificationSucceededEvent
| AuthenticationMfaSucceededEvent
| AuthenticationOAuthSucceededEvent
| AuthenticationSSOSucceededEvent
| AuthenticationPasswordFailedEvent
| AuthenticationPasswordSucceededEvent
| AuthenticationMagicAuthFailedEvent
| AuthenticationMagicAuthSucceededEvent
| ConnectionActivatedEvent
| ConnectionDeactivatedEvent
| ConnectionDeletedEvent
Expand Down Expand Up @@ -432,6 +531,14 @@ export type Event =
| OrganizationDeletedEvent;

export type EventResponse =
| AuthenticationEmailVerificationSucceededEventResponse
| AuthenticationMagicAuthFailedEventResponse
| AuthenticationMagicAuthSucceededEventResponse
| AuthenticationMfaSucceededEventResponse
| AuthenticationOAuthSucceededEventResponse
| AuthenticationPasswordFailedEventResponse
| AuthenticationPasswordSucceededEventResponse
| AuthenticationSSOSucceededEventResponse
| ConnectionActivatedEventResponse
| ConnectionDeactivatedEventResponse
| ConnectionDeletedEventResponse
Expand Down
14 changes: 14 additions & 0 deletions src/common/serializers/event.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import { deserializeOrganization } from '../../organizations/serializers';
import { deserializeConnection } from '../../sso/serializers';
import {
deserializeAuthenticationEvent,
deserializeEmailVerificationEvent,
deserializeInvitationEvent,
deserializeMagicAuthEvent,
Expand All @@ -27,6 +28,19 @@ export const deserializeEvent = (event: EventResponse): Event => {
};

switch (event.event) {
case 'authentication.email_verification_succeeded':
case 'authentication.magic_auth_failed':
case 'authentication.magic_auth_succeeded':
case 'authentication.mfa_succeeded':
case 'authentication.oauth_succeeded':
case 'authentication.password_failed':
case 'authentication.password_succeeded':
case 'authentication.sso_succeeded':
return {
...eventBase,
event: event.event,
data: deserializeAuthenticationEvent(event.data),
};
case 'connection.activated':
case 'connection.deactivated':
case 'connection.deleted':
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
interface AuthenticationEventError {
code: string;
message: string;
}

type AuthenticationEventType =
| 'sso'
| 'password'
| 'oauth'
| 'mfa'
| 'magic_auth'
| 'email_verification';

type AuthenticationEventStatus = 'failed' | 'succeeded';

export type AuthenticationEvent = {
email: string | null;
error?: AuthenticationEventError;
ipAddress: string | null;
status: AuthenticationEventStatus;
type: AuthenticationEventType;
userAgent: string | null;
userId: string | null;
};

export interface AuthenticationEventResponse {
email: string | null;
error?: AuthenticationEventError;
ip_address: string | null;
status: AuthenticationEventStatus;
type: AuthenticationEventType;
user_agent: string | null;
user_id: string | null;
}
1 change: 1 addition & 0 deletions src/user-management/interfaces/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './authentication-event.interface';
export * from './authenticate-with-magic-auth-options.interface';
export * from './authenticate-with-password-options.interface';
export * from './authenticate-with-code-options.interface';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {
AuthenticationEvent,
AuthenticationEventResponse,
} from '../interfaces';

export const deserializeAuthenticationEvent = (
authenticationEvent: AuthenticationEventResponse,
): AuthenticationEvent => ({
email: authenticationEvent.email,
error: authenticationEvent.error,
ipAddress: authenticationEvent.ip_address,
status: authenticationEvent.status,
type: authenticationEvent.type,
userAgent: authenticationEvent.user_agent,
userId: authenticationEvent.user_id,
});
1 change: 1 addition & 0 deletions src/user-management/serializers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export * from './authenticate-with-magic-auth-options.serializer';
export * from './authenticate-with-password-options.serializer';
export * from './authenticate-with-refresh-token.options.serializer';
export * from './authenticate-with-totp-options.serializer';
export * from './authentication-event.serializer';
export * from './authentication-response.serializer';
export * from './create-magic-auth-options.serializer';
export * from './create-password-reset-options.serializer';
Expand Down