Skip to content

Commit

Permalink
Merge pull request #251 from awinogrodzki/fix/249-merge-stack-trace-i…
Browse files Browse the repository at this point in the history
…n-token-verifier

fix(#249): merge error stack trace in token verifier to improve visibility on fetch errors
  • Loading branch information
awinogrodzki authored Sep 3, 2024
2 parents e50e4e0 + 6bce756 commit a76d4d5
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
30 changes: 30 additions & 0 deletions src/auth/error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,29 @@ function getErrorMessage(code: AuthErrorCode, customMessage?: string) {
return `${AuthErrorMessages[code]}: ${customMessage}`;
}

function mergeStackTraceAndCause(target: Error, original: unknown) {
const originalError = original as Error | undefined;
const originalErrorStack =
typeof originalError?.stack === 'string' ? originalError.stack : '';
const originalCause =
typeof originalError?.cause === 'string' ? originalError.cause : '';

target.stack = originalErrorStack + (target?.stack ?? '');
target.cause = originalCause + (target?.cause ?? '');
}

export class AuthError extends Error {
public static fromError(
error: unknown,
code: AuthErrorCode,
customMessage?: string
) {
const authError = new AuthError(code, customMessage);

mergeStackTraceAndCause(authError, error);

return authError;
}
constructor(
readonly code: AuthErrorCode,
customMessage?: string
Expand Down Expand Up @@ -70,6 +92,14 @@ const InvalidTokenMessages: Record<InvalidTokenReason, string> = {
};

export class InvalidTokenError extends Error {
public static fromError(error: unknown, reason: InvalidTokenReason) {
const invalidTokenError = new InvalidTokenError(reason);

mergeStackTraceAndCause(invalidTokenError, error);

return invalidTokenError;
}

constructor(public readonly reason: InvalidTokenReason) {
super(`${reason}: ${InvalidTokenMessages[reason]}`);
Object.setPrototypeOf(this, InvalidTokenError.prototype);
Expand Down
5 changes: 2 additions & 3 deletions src/auth/jwt/sign.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ export async function sign({
try {
key = await importPKCS8(privateKey, ALGORITHM_RS256);
} catch (e) {
const error = new AuthError(
throw AuthError.fromError(
e,
AuthErrorCode.INVALID_ARGUMENT,
'It looks like the value provided for `serviceAccount.privateKey` is incorrectly formatted. Please double-check if private key has correct format. See /~https://github.com/awinogrodzki/next-firebase-auth-edge/issues/246#issuecomment-2321559620 for details'
);
error.stack = (error?.stack ?? '') + ((e as Error)?.stack ?? '');
throw error;
}

return new SignJWT(payload)
Expand Down
14 changes: 11 additions & 3 deletions src/auth/token-verifier.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,18 +128,26 @@ export class FirebaseTokenVerifier {

private mapJoseErrorToAuthError(error: JOSEError): Error {
if (error instanceof errors.JWTExpired) {
return new AuthError(AuthErrorCode.TOKEN_EXPIRED, error.message);
return AuthError.fromError(
error,
AuthErrorCode.TOKEN_EXPIRED,
error.message
);
}

if (error instanceof errors.JWSSignatureVerificationFailed) {
return new AuthError(AuthErrorCode.INVALID_SIGNATURE);
return AuthError.fromError(error, AuthErrorCode.INVALID_SIGNATURE);
}

if (error instanceof AuthError) {
return error;
}

return new AuthError(AuthErrorCode.INTERNAL_ERROR, error.message);
return AuthError.fromError(
error,
AuthErrorCode.INTERNAL_ERROR,
error.message
);
}
}

Expand Down
4 changes: 1 addition & 3 deletions src/next/middleware.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,7 @@ export async function authMiddleware(
e instanceof AuthError &&
e.code === AuthErrorCode.NO_MATCHING_KID
) {
const error = new InvalidTokenError(InvalidTokenReason.INVALID_KID);
error.stack = e.stack;
throw error;
throw InvalidTokenError.fromError(e, InvalidTokenReason.INVALID_KID);
}

debug('Authentication failed with error', {error: e});
Expand Down

0 comments on commit a76d4d5

Please sign in to comment.