Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 470
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
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| 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`. |
| 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], | ||
| }; | ||
| } |
| 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')); | ||
| }); | ||
| }); | ||
| }; |
| 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.
Uh oh!
There was an error while loading. Please reload this page.
| 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. | ||
| 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); | ||
| }); | ||
| }); | ||
| }; |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.