|
| 1 | +/** |
| 2 | + * Copyright (c) 2026, WSO2 LLC. (https://www.wso2.com). |
| 3 | + * |
| 4 | + * WSO2 LLC. licenses this file to you under the Apache License, |
| 5 | + * Version 2.0 (the "License"); you may not use this file except |
| 6 | + * in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, |
| 12 | + * software distributed under the License is distributed on an |
| 13 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | + * KIND, either express or implied. See the License for the |
| 15 | + * specific language governing permissions and limitations |
| 16 | + * under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +import {exportJWK, generateKeyPair, SignJWT} from 'jose'; |
| 20 | +import {DefaultCrypto} from '../DefaultCrypto'; |
| 21 | +import TokenConstants from '../constants/TokenConstants'; |
| 22 | +import {JWKInterface} from '../models/crypto'; |
| 23 | + |
| 24 | +describe('DefaultCrypto.verifyJwt with ML-DSA (AKP JWKs)', (): void => { |
| 25 | + it.each(['ML-DSA-44', 'ML-DSA-65', 'ML-DSA-87'] as const)( |
| 26 | + 'verifies an id_token signed with %s against its AKP JWK', |
| 27 | + async (alg): Promise<void> => { |
| 28 | + const {publicKey, privateKey} = await generateKeyPair(alg, {extractable: true}); |
| 29 | + const jwk = (await exportJWK(publicKey)) as unknown as JWKInterface; |
| 30 | + jwk.alg = alg; |
| 31 | + jwk.kid = `test-${alg}`; |
| 32 | + jwk.use = 'sig'; |
| 33 | + |
| 34 | + const idToken: string = await new SignJWT({}) |
| 35 | + .setProtectedHeader({alg, kid: jwk.kid}) |
| 36 | + .setIssuedAt() |
| 37 | + .setIssuer('https://issuer.example') |
| 38 | + .setAudience('client-id') |
| 39 | + .setSubject('user-id') |
| 40 | + .setExpirationTime('1h') |
| 41 | + .sign(privateKey); |
| 42 | + |
| 43 | + const crypto = new DefaultCrypto(); |
| 44 | + const result: boolean = await crypto.verifyJwt( |
| 45 | + idToken, |
| 46 | + jwk, |
| 47 | + TokenConstants.SignatureValidation.SUPPORTED_ALGORITHMS as unknown as string[], |
| 48 | + 'client-id', |
| 49 | + 'https://issuer.example', |
| 50 | + 'user-id', |
| 51 | + ); |
| 52 | + |
| 53 | + expect(result).toBe(true); |
| 54 | + }, |
| 55 | + ); |
| 56 | + |
| 57 | + it('rejects an ML-DSA id_token with a tampered signature', async (): Promise<void> => { |
| 58 | + const alg = 'ML-DSA-65'; |
| 59 | + const {publicKey, privateKey} = await generateKeyPair(alg, {extractable: true}); |
| 60 | + const jwk = (await exportJWK(publicKey)) as unknown as JWKInterface; |
| 61 | + jwk.alg = alg; |
| 62 | + jwk.kid = 'test-tampered'; |
| 63 | + jwk.use = 'sig'; |
| 64 | + |
| 65 | + const idToken: string = await new SignJWT({}) |
| 66 | + .setProtectedHeader({alg, kid: jwk.kid}) |
| 67 | + .setIssuedAt() |
| 68 | + .setIssuer('https://issuer.example') |
| 69 | + .setAudience('client-id') |
| 70 | + .setSubject('user-id') |
| 71 | + .setExpirationTime('1h') |
| 72 | + .sign(privateKey); |
| 73 | + |
| 74 | + const parts: string[] = idToken.split('.'); |
| 75 | + const tamperedSignature: string = parts[2].slice(0, -4) + (parts[2].endsWith('AAAA') ? 'BBBB' : 'AAAA'); |
| 76 | + const tamperedToken = `${parts[0]}.${parts[1]}.${tamperedSignature}`; |
| 77 | + |
| 78 | + const crypto = new DefaultCrypto(); |
| 79 | + await expect( |
| 80 | + crypto.verifyJwt( |
| 81 | + tamperedToken, |
| 82 | + jwk, |
| 83 | + TokenConstants.SignatureValidation.SUPPORTED_ALGORITHMS as unknown as string[], |
| 84 | + 'client-id', |
| 85 | + 'https://issuer.example', |
| 86 | + 'user-id', |
| 87 | + ), |
| 88 | + ).rejects.toThrow(); |
| 89 | + }); |
| 90 | +}); |
0 commit comments