|
1 | 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
2 | 2 |
|
3 | 3 | import { |
| 4 | + createJwt, |
4 | 5 | mockJwks, |
5 | 6 | mockJwt, |
6 | 7 | mockJwtHeader, |
7 | 8 | mockJwtPayload, |
| 9 | + mockOAuthAccessTokenJwtPayload, |
8 | 10 | pemEncodedPublicKey, |
9 | 11 | publicJwks, |
10 | 12 | signedJwt, |
11 | 13 | someOtherPublicKey, |
12 | 14 | } from '../../fixtures'; |
| 15 | +import { mockSignedOAuthAccessTokenJwt, mockSignedOAuthAccessTokenJwtApplicationTyp } from '../../fixtures/machine'; |
13 | 16 | import { decodeJwt, hasValidSignature, verifyJwt } from '../verifyJwt'; |
14 | 17 |
|
15 | 18 | const invalidTokenError = { |
@@ -129,4 +132,89 @@ describe('verifyJwt(jwt, options)', () => { |
129 | 132 | const { errors: [error] = [] } = await verifyJwt('invalid-jwt', inputVerifyJwtOptions); |
130 | 133 | expect(error).toMatchObject(invalidTokenError); |
131 | 134 | }); |
| 135 | + |
| 136 | + it('verifies JWT with default headerType (JWT)', async () => { |
| 137 | + const inputVerifyJwtOptions = { |
| 138 | + key: mockJwks.keys[0], |
| 139 | + issuer: mockJwtPayload.iss, |
| 140 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 141 | + }; |
| 142 | + const { data } = await verifyJwt(mockJwt, inputVerifyJwtOptions); |
| 143 | + expect(data).toEqual(mockJwtPayload); |
| 144 | + }); |
| 145 | + |
| 146 | + it('verifies JWT with explicit headerType as string', async () => { |
| 147 | + const inputVerifyJwtOptions = { |
| 148 | + key: mockJwks.keys[0], |
| 149 | + issuer: mockJwtPayload.iss, |
| 150 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 151 | + headerType: 'JWT', |
| 152 | + }; |
| 153 | + const { data } = await verifyJwt(mockJwt, inputVerifyJwtOptions); |
| 154 | + expect(data).toEqual(mockJwtPayload); |
| 155 | + }); |
| 156 | + |
| 157 | + it('verifies OAuth JWT with headerType as array including at+jwt', async () => { |
| 158 | + const inputVerifyJwtOptions = { |
| 159 | + key: mockJwks.keys[0], |
| 160 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 161 | + headerType: ['at+jwt', 'application/at+jwt'], |
| 162 | + }; |
| 163 | + const { data } = await verifyJwt(mockSignedOAuthAccessTokenJwt, inputVerifyJwtOptions); |
| 164 | + expect(data).toBeDefined(); |
| 165 | + expect(data?.sub).toBe('user_2vYVtestTESTtestTESTtestTESTtest'); |
| 166 | + }); |
| 167 | + |
| 168 | + it('verifies OAuth JWT with headerType as array including application/at+jwt', async () => { |
| 169 | + const inputVerifyJwtOptions = { |
| 170 | + key: mockJwks.keys[0], |
| 171 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 172 | + headerType: ['at+jwt', 'application/at+jwt'], |
| 173 | + }; |
| 174 | + const { data } = await verifyJwt(mockSignedOAuthAccessTokenJwtApplicationTyp, inputVerifyJwtOptions); |
| 175 | + expect(data).toBeDefined(); |
| 176 | + expect(data?.sub).toBe('user_2vYVtestTESTtestTESTtestTESTtest'); |
| 177 | + }); |
| 178 | + |
| 179 | + it('rejects JWT when headerType does not match', async () => { |
| 180 | + const inputVerifyJwtOptions = { |
| 181 | + key: mockJwks.keys[0], |
| 182 | + issuer: mockJwtPayload.iss, |
| 183 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 184 | + headerType: 'at+jwt', |
| 185 | + }; |
| 186 | + const { errors: [error] = [] } = await verifyJwt(mockJwt, inputVerifyJwtOptions); |
| 187 | + expect(error).toBeDefined(); |
| 188 | + expect(error?.message).toContain('Invalid JWT type'); |
| 189 | + expect(error?.message).toContain('Expected "at+jwt"'); |
| 190 | + }); |
| 191 | + |
| 192 | + it('rejects OAuth JWT when headerType does not match', async () => { |
| 193 | + const inputVerifyJwtOptions = { |
| 194 | + key: mockJwks.keys[0], |
| 195 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 196 | + headerType: 'JWT', |
| 197 | + }; |
| 198 | + const { errors: [error] = [] } = await verifyJwt(mockSignedOAuthAccessTokenJwt, inputVerifyJwtOptions); |
| 199 | + expect(error).toBeDefined(); |
| 200 | + expect(error?.message).toContain('Invalid JWT type'); |
| 201 | + expect(error?.message).toContain('Expected "JWT"'); |
| 202 | + }); |
| 203 | + |
| 204 | + it('rejects JWT when headerType array does not include the token type', async () => { |
| 205 | + const jwtWithCustomTyp = createJwt({ |
| 206 | + header: { typ: 'custom-type', kid: 'ins_2GIoQhbUpy0hX7B2cVkuTMinXoD' }, |
| 207 | + payload: mockOAuthAccessTokenJwtPayload, |
| 208 | + }); |
| 209 | + |
| 210 | + const inputVerifyJwtOptions = { |
| 211 | + key: mockJwks.keys[0], |
| 212 | + authorizedParties: ['https://accounts.inspired.puma-74.lcl.dev'], |
| 213 | + headerType: ['at+jwt', 'application/at+jwt'], |
| 214 | + }; |
| 215 | + const { errors: [error] = [] } = await verifyJwt(jwtWithCustomTyp, inputVerifyJwtOptions); |
| 216 | + expect(error).toBeDefined(); |
| 217 | + expect(error?.message).toContain('Invalid JWT type'); |
| 218 | + expect(error?.message).toContain('Expected "at+jwt, application/at+jwt"'); |
| 219 | + }); |
132 | 220 | }); |
0 commit comments