Skip to content
Open
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
50 changes: 50 additions & 0 deletions src/common/exceptions/authentication.exception.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
GenericServerException,
WorkOSErrorData,
} from './generic-server.exception';

export type AuthenticationErrorCode =
| 'email_verification_required'
| 'organization_selection_required'
| 'mfa_enrollment'
| 'mfa_challenge'
| 'mfa_verification'
| 'sso_required';

export interface AuthenticationErrorData extends WorkOSErrorData {
code: AuthenticationErrorCode;
pending_authentication_token?: string;
user?: Record<string, unknown>;
organizations?: Array<{ id: string; name: string }>;
}

const AUTHENTICATION_ERROR_CODES: ReadonlySet<string> = new Set<string>([
'email_verification_required',
'organization_selection_required',
'mfa_enrollment',
'mfa_challenge',
'mfa_verification',
'sso_required',
]);

export function isAuthenticationErrorData(
data: WorkOSErrorData,
): data is AuthenticationErrorData {
return (
typeof data.code === 'string' && AUTHENTICATION_ERROR_CODES.has(data.code)
);
}
Comment thread
greptile-apps[bot] marked this conversation as resolved.

export class AuthenticationException extends GenericServerException {
readonly name = 'AuthenticationException';
readonly pendingAuthenticationToken: string | undefined;

constructor(
status: number,
readonly rawData: AuthenticationErrorData,
requestID: string,
) {
super(status, rawData.message, rawData, requestID);
this.pendingAuthenticationToken = rawData.pending_authentication_token;
}
}
10 changes: 9 additions & 1 deletion src/common/exceptions/generic-server.exception.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
import { RequestException } from '../interfaces/request-exception.interface';

export interface WorkOSErrorData {
code?: string;
message?: string;
[key: string]: unknown;
}

export class GenericServerException extends Error implements RequestException {
readonly name: string = 'GenericServerException';
readonly message: string = 'The request could not be completed.';
readonly code: string | undefined;

constructor(
readonly status: number,
message: string | undefined,
readonly rawData: unknown,
readonly rawData: WorkOSErrorData,
readonly requestID: string,
) {
super();
if (message) {
this.message = message;
}
this.code = rawData.code;
}
}
1 change: 1 addition & 0 deletions src/common/exceptions/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './api-key-required.exception';
export * from './authentication.exception';
export * from './generic-server.exception';
export * from './bad-request.exception';
export * from './no-api-key-provided.exception';
Expand Down
5 changes: 5 additions & 0 deletions src/common/interfaces/event.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -856,6 +856,11 @@ export interface VaultByokKeyVerificationCompletedEventResponse extends EventRes
data: VaultByokKeyVerificationCompletedEventResponseData;
}

export interface UnknownEvent extends EventBase {
event: string;
data: Record<string, unknown>;
}

export type Event =
| AuthenticationEmailVerificationSucceededEvent
| AuthenticationMfaSucceededEvent
Expand Down
1 change: 1 addition & 0 deletions src/common/interfaces/workos-response-error.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface WorkOSResponseError {
error?: string;
errors?: UnprocessableEntityError[];
message: string;
[key: string]: unknown;
}
6 changes: 6 additions & 0 deletions src/common/serializers/event.serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,5 +302,11 @@ export const deserializeEvent = (event: EventResponse): Event => {
event: event.event,
data: deserializeVaultByokKeyVerificationCompletedEvent(event.data),
};
default:
return {
...eventBase,
event: (event as { event: string }).event,
data: (event as { data: Record<string, unknown> }).data,
} as Event;
}
};
4 changes: 4 additions & 0 deletions src/workos.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
ApiKeyRequiredException,
AuthenticationException,
GenericServerException,
isAuthenticationErrorData,
NotFoundException,
UnauthorizedException,
UnprocessableEntityException,
Expand Down Expand Up @@ -494,6 +496,8 @@ export class WorkOS {
message,
requestID,
});
} else if (isAuthenticationErrorData(data)) {
throw new AuthenticationException(status, data, requestID);
} else {
throw new GenericServerException(
status,
Expand Down
Loading