-
Notifications
You must be signed in to change notification settings - Fork 283
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(backend): Refactor hasValidSignature to also work with PEM keys
Also extracted some functionality to scr/tokens/jwt/algorithms so it can be reused in the future and added some spec.
- Loading branch information
Showing
6 changed files
with
129 additions
and
34 deletions.
There are no files selected for viewing
This file contains 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 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 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,65 @@ | ||
import runtime from '../../runtime'; | ||
import isomorphicAtob from '../../shared/isomorphicAtob'; | ||
|
||
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, | ||
}; | ||
|
||
const algs = Object.keys(algToHash); | ||
|
||
// 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 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], | ||
}; | ||
} | ||
|
||
export function importKey( | ||
secret: JsonWebKey | string, | ||
algorithm: RsaHashedImportParams, | ||
keyUsage: 'verify' | 'sign', | ||
): Promise<CryptoKey> { | ||
if (typeof secret === 'object') { | ||
return runtime.crypto.subtle.importKey('jwk', secret, algorithm, false, [keyUsage]); | ||
} | ||
|
||
const keyData = pemToBuffer(secret); | ||
const format = keyUsage === 'sign' ? 'pkcs8' : 'spki'; | ||
|
||
return runtime.crypto.subtle.importKey(format, keyData, algorithm, false, [keyUsage]); | ||
} |
This file contains 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,23 @@ | ||
import type QUnit from 'qunit'; | ||
|
||
import { pemEncodedPublicKey, publicJwks, signedJwt, someOtherPublicKey } from '../fixtures'; | ||
import { hasValidSignature } from '../jwt/hasValidSignature'; | ||
import { decodeJwt } from './verifyJwt'; | ||
|
||
export default (QUnit: QUnit) => { | ||
const { module, test } = QUnit; | ||
|
||
module('hasValidSignature(jwt, key)', () => { | ||
test('verifies the signature with a JWK formatted key', async assert => { | ||
assert.true(await hasValidSignature(decodeJwt(signedJwt), publicJwks)); | ||
}); | ||
|
||
test('verifies the signature with a PEM formatted key', async assert => { | ||
assert.true(await hasValidSignature(decodeJwt(signedJwt), pemEncodedPublicKey)); | ||
}); | ||
|
||
test('it returns false if the key is not correct', async assert => { | ||
assert.false(await hasValidSignature(decodeJwt(signedJwt), someOtherPublicKey)); | ||
}); | ||
}); | ||
}; |
This file contains 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 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