-
Notifications
You must be signed in to change notification settings - Fork 460
PLT 155 - Introduce jwt signing js backend #1786
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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,7 @@ | ||
| --- | ||
| '@clerk/backend': patch | ||
| --- | ||
|
|
||
| Added new function `signJwt(payload, key, options)` for JWT token signing. | ||
| Also updated the existing `hasValidSignature` and `verifyJwt` method to handle PEM-formatted keys directly (previously they had to be converted to jwks). | ||
| For key compatibility, support is specifically confined to `RSA` types and formats `jwk, pkcs8, spki`. |
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
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,28 @@ | ||
| const algToHash: Record<string, string> = { | ||
| RS256: 'SHA-256', | ||
| RS384: 'SHA-384', | ||
| RS512: 'SHA-512', | ||
| }; | ||
| const RSA_ALGORITHM_NAME = 'RSASSA-PKCS1-v1_5'; | ||
|
|
||
| const jwksAlgToCryptoAlg: Record<string, string> = { | ||
| RS256: RSA_ALGORITHM_NAME, | ||
| RS384: RSA_ALGORITHM_NAME, | ||
| RS512: RSA_ALGORITHM_NAME, | ||
| }; | ||
|
|
||
| export const algs = Object.keys(algToHash); | ||
|
|
||
| export function getCryptoAlgorithm(algorithmName: string): RsaHashedImportParams { | ||
| const hash = algToHash[algorithmName]; | ||
| const name = jwksAlgToCryptoAlg[algorithmName]; | ||
|
|
||
| if (!hash || !name) { | ||
| throw new Error(`Unsupported algorithm ${algorithmName}, expected one of ${algs.join(',')}.`); | ||
| } | ||
|
|
||
| return { | ||
| hash: { name: algToHash[algorithmName] }, | ||
| name: jwksAlgToCryptoAlg[algorithmName], | ||
| }; | ||
| } |
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
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,44 @@ | ||
| import type QUnit from 'qunit'; | ||
|
|
||
| import { pemEncodedPublicKey, pemEncodedSignKey, publicJwks, signingJwks } from '../fixtures'; | ||
| import { importKey } from './cryptoKeys'; | ||
|
|
||
| export default (QUnit: QUnit) => { | ||
| const { module, test } = QUnit; | ||
|
|
||
| module('importKey(key, options)', () => { | ||
| const algorithm = { name: 'RSASSA-PKCS1-v1_5', hash: 'SHA-256' }; | ||
|
|
||
| test('imports a JWK formatted private key', async assert => { | ||
| assert.ok(await importKey(signingJwks, algorithm, 'sign')); | ||
| }); | ||
|
|
||
| test('imports a JWK formatted public key', async assert => { | ||
| assert.ok(await importKey(publicJwks, algorithm, 'verify')); | ||
| }); | ||
|
|
||
| test('imports a pkcs8 formatted secret for signing', async assert => { | ||
| assert.ok(await importKey(pemEncodedSignKey, algorithm, 'sign')); | ||
| }); | ||
|
|
||
| test('imports a pkcs8 formatted secret for verification', async assert => { | ||
| assert.ok(await importKey(pemEncodedPublicKey, algorithm, 'verify')); | ||
| }); | ||
|
|
||
| test('throws an error if the key is string and not pem formatted', async assert => { | ||
| assert.rejects(importKey('not a key', algorithm, 'sign')); | ||
| }); | ||
|
|
||
| test('throws an error if the key is not a JWK', async assert => { | ||
| assert.rejects(importKey({} as JsonWebKey, algorithm, 'sign')); | ||
| }); | ||
|
|
||
| test('throws an error if a public key is imported for signing', async assert => { | ||
| assert.rejects(importKey(pemEncodedPublicKey, algorithm, 'sign')); | ||
| }); | ||
|
|
||
| test('throws an error if a private key is imported for verification', async assert => { | ||
| assert.rejects(importKey(pemEncodedSignKey, algorithm, 'verify')); | ||
| }); | ||
| }); | ||
| }; |
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,37 @@ | ||
| import { isomorphicAtob } from '@clerk/shared'; | ||
|
|
||
| import runtime from '../../runtime'; | ||
|
|
||
| // https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/importKey#pkcs_8_import | ||
| function pemToBuffer(secret: string): ArrayBuffer { | ||
| const trimmed = secret | ||
| .replace(/-----BEGIN.*?-----/g, '') | ||
| .replace(/-----END.*?-----/g, '') | ||
| .replace(/\s/g, ''); | ||
|
|
||
| const decoded = isomorphicAtob(trimmed); | ||
|
|
||
| const buffer = new ArrayBuffer(decoded.length); | ||
| const bufView = new Uint8Array(buffer); | ||
|
|
||
| for (let i = 0, strLen = decoded.length; i < strLen; i++) { | ||
| bufView[i] = decoded.charCodeAt(i); | ||
| } | ||
|
|
||
| return bufView; | ||
| } | ||
|
|
||
| export function importKey( | ||
| key: JsonWebKey | string, | ||
| algorithm: RsaHashedImportParams, | ||
| keyUsage: 'verify' | 'sign', | ||
| ): Promise<CryptoKey> { | ||
| if (typeof key === 'object') { | ||
| return runtime.crypto.subtle.importKey('jwk', key, algorithm, false, [keyUsage]); | ||
| } | ||
|
|
||
| const keyData = pemToBuffer(key); | ||
| const format = keyUsage === 'sign' ? 'pkcs8' : 'spki'; | ||
|
|
||
| return runtime.crypto.subtle.importKey(format, keyData, algorithm, false, [keyUsage]); | ||
| } |
This file was deleted.
Oops, something went wrong.
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,3 +1,5 @@ | ||
| export { hasValidSignature, decodeJwt, verifyJwt } from './verifyJwt'; | ||
| export { signJwt } from './signJwt'; | ||
|
|
||
| export type { VerifyJwtOptions } from './verifyJwt'; | ||
| export type { SignJwtOptions } from './signJwt'; | ||
|
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. ❓ Does it make sense to prefix these options as unstable? Unlikely to be used, but only for semantics purposes. |
||
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 @@ | ||
| import type { JwtPayload } from '@clerk/types'; | ||
| import type QUnit from 'qunit'; | ||
|
|
||
| import { | ||
| mockJwtHeader, | ||
| mockJwtPayload, | ||
| pemEncodedPublicKey, | ||
| pemEncodedSignKey, | ||
| publicJwks, | ||
| signingJwks, | ||
| } from '../fixtures'; | ||
| import { signJwt } from './signJwt'; | ||
| import { verifyJwt } from './verifyJwt'; | ||
|
|
||
| export default (QUnit: QUnit) => { | ||
| const { module, test } = QUnit; | ||
|
|
||
| module('signJwt(payload, options)', hooks => { | ||
| let payload: JwtPayload; | ||
|
|
||
| hooks.beforeEach(() => { | ||
| payload = { | ||
| ...mockJwtPayload, | ||
| exp: Date.now() + 1000 * 60 * 60 * 24 * 7, | ||
| } as JwtPayload; | ||
| }); | ||
|
|
||
| test('signs a JWT with a JWK formatted secret', async assert => { | ||
| const jwt = await signJwt(payload, signingJwks, { | ||
| algorithm: mockJwtHeader.alg, | ||
| header: mockJwtHeader, | ||
| }); | ||
|
|
||
| const verifiedPayload = await verifyJwt(jwt, { key: publicJwks, issuer: mockJwtPayload.iss }); | ||
| assert.deepEqual(verifiedPayload, payload); | ||
| }); | ||
|
|
||
| test('signs a JWT with a pkcs8 formatted secret', async assert => { | ||
| const jwt = await signJwt(payload, pemEncodedSignKey, { | ||
| algorithm: mockJwtHeader.alg, | ||
| header: mockJwtHeader, | ||
| }); | ||
|
|
||
| const verifiedPayload = await verifyJwt(jwt, { key: pemEncodedPublicKey, issuer: mockJwtPayload.iss }); | ||
| assert.deepEqual(verifiedPayload, payload); | ||
| }); | ||
| }); | ||
| }; |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.