@@ -64,6 +64,18 @@ vi.mock(import("../../../utils/getClaimsSchema.js"), () => ({
6464 getClaimsSchema : mockGetClaimsSchema ,
6565} ) ) ;
6666
67+ const mockJourneys = vi . fn ( ) ;
68+
69+ // @ts -expect-error
70+ vi . mock ( import ( "../../../journeys/utils/config.js" ) , ( ) => ( {
71+ journeys : new Proxy (
72+ { } ,
73+ {
74+ get : ( ) => mockJourneys ,
75+ } ,
76+ ) ,
77+ } ) ) ;
78+
6779let verifyJwt : typeof verifyJwtForType ;
6880
6981describe ( "verifyJwt" , ( ) => {
@@ -102,11 +114,16 @@ describe("verifyJwt", () => {
102114 } ) ;
103115
104116 it ( "verifies JWT with ES256 algorithm" , async ( ) => {
105- const payload = { sub : "user123" , aud : "test-client" } ;
106- const claimsSchema = v . object ( { sub : v . string ( ) , aud : v . string ( ) } ) ;
117+ const payload = { sub : "user123" , aud : "test-client" , scope : "openid" } ;
118+ const claimsSchema = v . object ( {
119+ sub : v . string ( ) ,
120+ aud : v . string ( ) ,
121+ scope : v . string ( ) ,
122+ } ) ;
107123
108124 mockJwtVerify . mockResolvedValue ( { payload } ) ;
109125 mockGetClaimsSchema . mockReturnValue ( claimsSchema ) ;
126+ mockJourneys . mockResolvedValue ( { requiredClaims : [ ] } ) ;
110127
111128 const result = await verifyJwt (
112129 reply ,
@@ -441,12 +458,55 @@ describe("verifyJwt", () => {
441458 ) ;
442459 } ) ;
443460
461+ it ( "returns ErrorResponse when required claims are missing" , async ( ) => {
462+ const payload = { sub : "user123" , aud : "test-client" , scope : "openid" } ;
463+ const claimsSchema = v . object ( {
464+ sub : v . string ( ) ,
465+ aud : v . string ( ) ,
466+ scope : v . string ( ) ,
467+ } ) ;
468+
469+ mockJwtVerify . mockResolvedValue ( { payload } ) ;
470+ mockGetClaimsSchema . mockReturnValue ( claimsSchema ) ;
471+ mockJourneys . mockResolvedValue ( {
472+ requiredClaims : [ "account_management_api_access_token" ] ,
473+ } ) ;
474+
475+ const result = await verifyJwt (
476+ reply ,
477+ signedJwt ,
478+ client ,
479+ redirectUri ,
480+ scope ,
481+ state ,
482+ ) ;
483+
484+ expect ( result ) . toBeInstanceOf ( ErrorResponse ) ;
485+
486+ assert . ok ( result instanceof ErrorResponse ) ;
487+
488+ expect ( vi . mocked ( result . reply . redirect ) . mock . calls [ 0 ] ?. [ 0 ] ) . toContain (
489+ "error=invalid_request" ,
490+ ) ;
491+ expect ( vi . mocked ( result . reply . redirect ) . mock . calls [ 0 ] ?. [ 0 ] ) . toContain (
492+ "error_description=E1013" ,
493+ ) ;
494+ expect ( addAuthorizeErrorMetric ) . toHaveBeenCalledWith (
495+ "RequiredClaimsMissing" ,
496+ ) ;
497+ } ) ;
498+
444499 it ( "works without state parameter" , async ( ) => {
445- const payload = { sub : "user123" , aud : "test-client" } ;
446- const claimsSchema = v . object ( { sub : v . string ( ) , aud : v . string ( ) } ) ;
500+ const payload = { sub : "user123" , aud : "test-client" , scope : "openid" } ;
501+ const claimsSchema = v . object ( {
502+ sub : v . string ( ) ,
503+ aud : v . string ( ) ,
504+ scope : v . string ( ) ,
505+ } ) ;
447506
448507 mockJwtVerify . mockResolvedValue ( { payload } ) ;
449508 mockGetClaimsSchema . mockReturnValue ( claimsSchema ) ;
509+ mockJourneys . mockResolvedValue ( { requiredClaims : [ ] } ) ;
450510
451511 const result = await verifyJwt (
452512 reply ,
0 commit comments