-
Notifications
You must be signed in to change notification settings - Fork 460
fix(clerk-sdk-node): Inherit verifyToken options from clerkClient #3296
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
Merged
panteliselef
merged 8 commits into
main
from
elef/sdk-1698-node-sdk-failed-to-resolve-jwk-during-verification
May 10, 2024
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9a7208b
fix(clerk-sdk-node): Inherit verifyToken options from clerkClient
panteliselef 558dab6
Merge branch 'main' into elef/sdk-1698-node-sdk-failed-to-resolve-jwk…
panteliselef 1dec021
fix(backend): Drop the InvalidSecretKey error
panteliselef 9d852ad
fix(clerk-sdk-node): Support verifyToken with optional 2nd param
panteliselef cabc23a
Merge branch 'main' into elef/sdk-1698-node-sdk-failed-to-resolve-jwk…
panteliselef b9c92b8
test(clerk-sdk-node): Add test to verify the change
panteliselef 1afcdcd
Merge branch 'main' into elef/sdk-1698-node-sdk-failed-to-resolve-jwk…
panteliselef 5782b00
Merge branch 'main' into elef/sdk-1698-node-sdk-failed-to-resolve-jwk…
panteliselef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| '@clerk/clerk-sdk-node': patch | ||
| '@clerk/backend': patch | ||
| --- | ||
|
|
||
| Inherit verifyToken options from clerkClient. | ||
| The below code now works as expected: (requires CLERK_SECRET_KEY env var to have been set) | ||
| ```ts | ||
| import { clerkClient } from "@clerk/clerk-sdk-node"; | ||
|
|
||
| // Use the default settings from the already instanciated clerkClient | ||
| clerkClient.verifyToken(token) | ||
| // or provide overrides the options | ||
| clerkClient.verifyToken(token, { | ||
| secretKey: 'xxxx' | ||
| }) | ||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| const verifyTokenMock = jest.fn(); | ||
|
|
||
| jest.mock('@clerk/backend', () => ({ | ||
| ...jest.requireActual('@clerk/backend'), | ||
| verifyToken: verifyTokenMock, | ||
| })); | ||
|
|
||
| import { createClerkClient } from '../clerkClient'; | ||
|
|
||
| afterEach(() => { | ||
| verifyTokenMock.mockReset(); | ||
| }); | ||
|
|
||
| describe('verifyToken', () => { | ||
| it('correctly use the predefined options of clerkClient', async () => { | ||
| const clerkClient = createClerkClient({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }); | ||
|
|
||
| await clerkClient.verifyToken('token'); | ||
| expect(verifyTokenMock).toHaveBeenCalledWith( | ||
| 'token', | ||
| expect.objectContaining({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }), | ||
| ); | ||
| }); | ||
|
|
||
| it('correctly use the passed options in verifyToken', async () => { | ||
| const clerkClient = createClerkClient({ | ||
| secretKey: '123', | ||
| jwtKey: '456', | ||
| }); | ||
|
|
||
| await clerkClient.verifyToken('token', { | ||
| secretKey: '987', | ||
| }); | ||
| expect(verifyTokenMock).toHaveBeenCalledWith( | ||
| 'token', | ||
| expect.objectContaining({ | ||
| secretKey: '987', | ||
| jwtKey: '456', | ||
| }), | ||
| ); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,25 @@ | ||
| import type { ClerkOptions } from '@clerk/backend'; | ||
| import { createClerkClient as _createClerkClient, verifyToken } from '@clerk/backend'; | ||
| import type { ClerkOptions, VerifyTokenOptions } from '@clerk/backend'; | ||
| import { createClerkClient as _createClerkClient, verifyToken as _verifyToken } from '@clerk/backend'; | ||
|
|
||
| import { createClerkExpressRequireAuth } from './clerkExpressRequireAuth'; | ||
| import { createClerkExpressWithAuth } from './clerkExpressWithAuth'; | ||
| import { loadApiEnv, loadClientEnv } from './utils'; | ||
|
|
||
| type MakeOptionalSecondArgument<T> = T extends (a: string, b: infer U) => infer R ? (a: string, b?: U) => R : never; | ||
| type VerifyTokenWithOptionalSecondArgument = MakeOptionalSecondArgument<typeof _verifyToken>; | ||
|
|
||
| type ClerkClient = ReturnType<typeof _createClerkClient> & { | ||
| expressWithAuth: ReturnType<typeof createClerkExpressWithAuth>; | ||
| expressRequireAuth: ReturnType<typeof createClerkExpressRequireAuth>; | ||
| verifyToken: typeof verifyToken; | ||
| verifyToken: VerifyTokenWithOptionalSecondArgument; | ||
| }; | ||
|
|
||
| const buildVerifyToken = (params: VerifyTokenOptions) => { | ||
| return (...args: Parameters<VerifyTokenWithOptionalSecondArgument>) => | ||
| _verifyToken(args[0], { | ||
| ...params, | ||
| ...args[1], | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔧 Could you add 2 tests (with options, without options) to verify that everything works as expected? |
||
| }); | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -24,7 +35,7 @@ export function createClerkClient(options: ClerkOptions): ClerkClient { | |
| return Object.assign(clerkClient, { | ||
| expressWithAuth, | ||
| expressRequireAuth, | ||
| verifyToken, | ||
| verifyToken: buildVerifyToken(options), | ||
| }); | ||
| } | ||
|
|
||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dimkl fyi, Just cleaning this up