Skip to content

Commit 18d507e

Browse files
committed
fix(backend): Add error when secret key is invalid
fix(backend): Add error when secret key is invalid
1 parent 4eeabba commit 18d507e

4 files changed

Lines changed: 49 additions & 1 deletion

File tree

.changeset/silent-games-joke.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
---
2+
---

packages/backend/src/tokens/errors.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
export type TokenCarrier = 'header' | 'cookie';
22

3+
export enum TokenVerificationErrorCode {
4+
InvalidSecretKey = 'clerk_key_invalid',
5+
}
6+
37
export enum TokenVerificationErrorReason {
48
TokenExpired = 'token-expired',
59
TokenInvalid = 'token-invalid',
@@ -9,6 +13,7 @@ export enum TokenVerificationErrorReason {
913
TokenInvalidSignature = 'token-invalid-signature',
1014
TokenNotActiveYet = 'token-not-active-yet',
1115
TokenVerificationFailed = 'token-verification-failed',
16+
InvalidSecretKey = 'secret-key-invalid',
1217

1318
LocalJWKMissing = 'jwk-local-missing',
1419

packages/backend/src/tokens/keys.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,13 @@ import { API_URL, API_VERSION, MAX_CACHE_LAST_UPDATED_AT_SECONDS } from '../cons
44
import runtime from '../runtime';
55
import { callWithRetry } from '../util/callWithRetry';
66
import { joinPaths } from '../util/path';
7-
import { TokenVerificationError, TokenVerificationErrorAction, TokenVerificationErrorReason } from './errors';
7+
import { getErrorObjectByCode } from '../util/request';
8+
import {
9+
TokenVerificationError,
10+
TokenVerificationErrorAction,
11+
TokenVerificationErrorCode,
12+
TokenVerificationErrorReason,
13+
} from './errors';
814

915
type JsonWebKeyWithKid = JsonWebKey & { kid: string };
1016

@@ -202,6 +208,31 @@ async function fetchJWKSFromBAPI(apiUrl: string, key: string, apiVersion: string
202208
});
203209

204210
if (!response.ok) {
211+
const errors = await response.json().then(({ errors }) => {
212+
if (!errors) {
213+
return false;
214+
}
215+
216+
const invalidSecretKey = getErrorObjectByCode(errors, TokenVerificationErrorCode.InvalidSecretKey);
217+
218+
if (!invalidSecretKey) {
219+
return false;
220+
}
221+
222+
return {
223+
...invalidSecretKey,
224+
reason: TokenVerificationErrorReason.InvalidSecretKey,
225+
};
226+
});
227+
228+
if (errors) {
229+
throw new TokenVerificationError({
230+
action: TokenVerificationErrorAction.ContactSupport,
231+
message: errors.message,
232+
reason: errors.reason,
233+
});
234+
}
235+
205236
throw new TokenVerificationError({
206237
action: TokenVerificationErrorAction.ContactSupport,
207238
message: `Error loading Clerk JWKS from ${url.href} with code=${response.status}`,

packages/backend/src/util/request.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,13 @@ function getFirstValueFromHeaderValue(value?: string | null) {
7878
function getProtocolVerb(protocol: string) {
7979
return protocol?.replace(/:$/, '') || '';
8080
}
81+
82+
type ErrorFields = {
83+
message: string;
84+
long_message: string;
85+
code: string;
86+
};
87+
88+
export const getErrorObjectByCode = (errors: ErrorFields[], code: string) => {
89+
return errors.find((err: ErrorFields) => err.code === code);
90+
};

0 commit comments

Comments
 (0)