Skip to content

Commit

Permalink
fix(backend): Add error when secret key is invalid
Browse files Browse the repository at this point in the history
  • Loading branch information
raptisj committed Jul 4, 2023
1 parent 18d507e commit 2ad7cf3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/hungry-radios-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clerk/backend': patch
---

Add a more descriptive error when secret key is invalid
2 changes: 0 additions & 2 deletions .changeset/silent-games-joke.md

This file was deleted.

24 changes: 6 additions & 18 deletions packages/backend/src/tokens/keys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -208,28 +208,16 @@ async function fetchJWKSFromBAPI(apiUrl: string, key: string, apiVersion: string
});

if (!response.ok) {
const errors = await response.json().then(({ errors }) => {
if (!errors) {
return false;
}

const invalidSecretKey = getErrorObjectByCode(errors, TokenVerificationErrorCode.InvalidSecretKey);
const json = await response.json();
const invalidSecretKeyError = getErrorObjectByCode(json?.errors, TokenVerificationErrorCode.InvalidSecretKey);

if (!invalidSecretKey) {
return false;
}

return {
...invalidSecretKey,
reason: TokenVerificationErrorReason.InvalidSecretKey,
};
});
if (invalidSecretKeyError) {
const reason = TokenVerificationErrorReason.InvalidSecretKey;

if (errors) {
throw new TokenVerificationError({
action: TokenVerificationErrorAction.ContactSupport,
message: errors.message,
reason: errors.reason,
message: invalidSecretKeyError.message,
reason,
});
}

Expand Down
4 changes: 4 additions & 0 deletions packages/backend/src/util/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,5 +86,9 @@ type ErrorFields = {
};

export const getErrorObjectByCode = (errors: ErrorFields[], code: string) => {
if (!errors) {
return null;
}

return errors.find((err: ErrorFields) => err.code === code);
};

0 comments on commit 2ad7cf3

Please sign in to comment.