|
| 1 | +import { createDPoP, getCreateDPoPOptions, verifyDPoP } from '../dpop'; |
| 2 | + |
| 3 | +describe('dpop', () => { |
| 4 | + const alg = 'HS256'; |
| 5 | + const jwk = { kty: 'Ed25519', crv: 'Ed25519', x: '123', y: '123' }; |
| 6 | + const jwtIssuer = { alg, jwk }; |
| 7 | + const htm = 'POST'; |
| 8 | + const htu = 'https://example.com/token'; |
| 9 | + const nonce = 'nonce'; |
| 10 | + const jwtPayloadProps = { htm, htu, nonce } as const; |
| 11 | + const jwtHeaderProps = { alg, jwk, typ: 'dpop+jwt' }; |
| 12 | + const unsignedDpop = |
| 13 | + 'eyJhbGciOiJIUzI1NiIsImp3ayI6eyJrdHkiOiJFZDI1NTE5IiwiY3J2IjoiRWQyNTUxOSIsIngiOiIxMjMiLCJ5IjoiMTIzIn0sInR5cCI6ImRwb3Arand0In0.eyJodG0iOiJQT1NUIiwiaHR1IjoiaHR0cHM6Ly9leGFtcGxlLmNvbS90b2tlbiIsIm5vbmNlIjoibm9uY2UiLCJpYXQiOjE3MjIzMjcxOTQsImp0aSI6Ijk4OWNiZTc4LWI1ZTYtNDViYS1iYjMzLWQ0MGE4ZGEwZjFhYSJ9'; |
| 14 | + |
| 15 | + it('should create a dpop with valid options', async () => { |
| 16 | + const dpop = await createDPoP({ |
| 17 | + jwtIssuer, |
| 18 | + jwtPayloadProps, |
| 19 | + createJwtCallback: async (dpopJwtIssuerWithContext, jwt) => { |
| 20 | + expect(dpopJwtIssuerWithContext.alg).toEqual(alg); |
| 21 | + expect(dpopJwtIssuerWithContext.jwk).toEqual(jwk); |
| 22 | + expect(dpopJwtIssuerWithContext.dPoPSigningAlgValuesSupported).toBeUndefined(); |
| 23 | + expect(dpopJwtIssuerWithContext.type).toEqual('dpop'); |
| 24 | + |
| 25 | + expect(jwt.header).toStrictEqual(jwtHeaderProps); |
| 26 | + expect(jwt.payload).toStrictEqual({ |
| 27 | + ...jwtPayloadProps, |
| 28 | + iat: expect.any(Number), |
| 29 | + jti: expect.any(String), |
| 30 | + }); |
| 31 | + |
| 32 | + return unsignedDpop; |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + expect(unsignedDpop).toEqual(dpop); |
| 37 | + expect.assertions(7); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should create a dpop with valid createDPoPOptions', async () => { |
| 41 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 42 | + const { htm, htu, ...rest } = jwtPayloadProps; |
| 43 | + const options = getCreateDPoPOptions( |
| 44 | + { |
| 45 | + jwtIssuer, |
| 46 | + jwtPayloadProps: rest, |
| 47 | + createJwtCallback: async (dpopJwtIssuerWithContext, jwt) => { |
| 48 | + expect(dpopJwtIssuerWithContext.alg).toEqual(alg); |
| 49 | + expect(dpopJwtIssuerWithContext.jwk).toEqual(jwk); |
| 50 | + expect(dpopJwtIssuerWithContext.dPoPSigningAlgValuesSupported).toBeUndefined(); |
| 51 | + expect(dpopJwtIssuerWithContext.type).toEqual('dpop'); |
| 52 | + |
| 53 | + expect(jwt.header).toStrictEqual(jwtHeaderProps); |
| 54 | + expect(jwt.payload).toStrictEqual({ |
| 55 | + ...jwtPayloadProps, |
| 56 | + iat: expect.any(Number), |
| 57 | + jti: expect.any(String), |
| 58 | + }); |
| 59 | + |
| 60 | + return unsignedDpop; |
| 61 | + }, |
| 62 | + }, |
| 63 | + htu + '?123412341#xyaksdjfaksdjf', |
| 64 | + ); |
| 65 | + |
| 66 | + const dpop = await createDPoP(options); |
| 67 | + |
| 68 | + expect(unsignedDpop).toEqual(dpop); |
| 69 | + expect.assertions(7); |
| 70 | + }); |
| 71 | + |
| 72 | + it('verify dpop fails if jwtVerifyCallback throws an error', async () => { |
| 73 | + await expect( |
| 74 | + verifyDPoP( |
| 75 | + { |
| 76 | + headers: { dpop: unsignedDpop }, |
| 77 | + fullUrl: htu + '?123412341#xyaksdjfaksdjf', |
| 78 | + method: 'POST', |
| 79 | + }, |
| 80 | + { |
| 81 | + jwtVerifyCallback: async () => { |
| 82 | + throw new Error('jwtVerifyCallback'); |
| 83 | + }, |
| 84 | + expectedNonce: 'nonce', |
| 85 | + expectAccessToken: false, |
| 86 | + now: 1722327194, |
| 87 | + }, |
| 88 | + ), |
| 89 | + ).rejects.toThrow(); |
| 90 | + }); |
| 91 | + |
| 92 | + it('should verify a dpop with valid options', async () => { |
| 93 | + const dpop = await verifyDPoP( |
| 94 | + { |
| 95 | + headers: { dpop: unsignedDpop }, |
| 96 | + fullUrl: htu + '?123412341#xyaksdjfaksdjf', |
| 97 | + method: 'POST', |
| 98 | + }, |
| 99 | + { |
| 100 | + jwtVerifyCallback: async (jwtVerifier, jwt) => { |
| 101 | + expect(jwtVerifier.method).toEqual('jwk'); |
| 102 | + expect(jwtVerifier.jwk).toEqual(jwk); |
| 103 | + expect(jwtVerifier.type).toEqual('dpop'); |
| 104 | + expect(jwtVerifier.alg).toEqual(alg); |
| 105 | + |
| 106 | + expect(jwt.header).toStrictEqual(jwtHeaderProps); |
| 107 | + expect(jwt.payload).toStrictEqual({ |
| 108 | + ...jwtPayloadProps, |
| 109 | + iat: expect.any(Number), |
| 110 | + jti: expect.any(String), |
| 111 | + }); |
| 112 | + expect(jwt.raw).toEqual(unsignedDpop); |
| 113 | + |
| 114 | + return true; |
| 115 | + }, |
| 116 | + expectAccessToken: false, |
| 117 | + expectedNonce: 'nonce', |
| 118 | + now: 1722327194, |
| 119 | + }, |
| 120 | + ); |
| 121 | + expect(dpop).toStrictEqual(jwk); |
| 122 | + expect.assertions(6); |
| 123 | + }); |
| 124 | +}); |
0 commit comments