@@ -7,8 +7,10 @@ import {
77 mockInvalidSignatureJwt ,
88 mockJwks ,
99 mockJwt ,
10+ mockJwtHeader ,
1011 mockJwtPayload ,
1112 mockM2MJwtPayload ,
13+ pemEncodedPublicKey ,
1214 signingJwks ,
1315} from '../../fixtures' ;
1416import {
@@ -19,6 +21,7 @@ import {
1921} from '../../fixtures/machine' ;
2022import { signJwt } from '../../jwt/signJwt' ;
2123import { server } from '../../mock-server' ;
24+ import { getCookieSuffix } from '../../util/shared' ;
2225import type { AuthReason } from '../authStatus' ;
2326import { AuthErrorReason , AuthStatus } from '../authStatus' ;
2427import { JWT_CATEGORY_JWT_TEMPLATE , JWT_CATEGORY_M2M_TOKEN , JWT_CATEGORY_SESSION_TOKEN } from '../jwtCategories' ;
@@ -509,6 +512,122 @@ describe('tokens.authenticateRequest(options)', () => {
509512 vi . clearAllMocks ( ) ;
510513 } ) ;
511514
515+ describe ( 'agent Session JWT authentication' , ( ) => {
516+ const identity = { session_id : 'protocol-session' , client_id : 'https://agent.example' , user_id : 'pseudonym' } ;
517+
518+ it . each ( [
519+ { name : 'valid bearer token' , cookie : false , expired : false } ,
520+ { name : 'valid session cookie' , cookie : true , expired : false } ,
521+ { name : 'expired bearer token' , cookie : false , expired : true } ,
522+ ] ) ( 'uses normal JWT verification for $name without a backend call' , async ( { cookie, expired } ) => {
523+ const { data : jwt } = await signJwt (
524+ {
525+ ...mockJwtPayload ,
526+ ...( expired ? { exp : mockJwtPayload . iat - 10 } : { } ) ,
527+ agt : { id : identity . client_id , poppy : identity } ,
528+ } ,
529+ signingJwks ,
530+ { algorithm : 'RS256' , header : { ...mockJwtHeader } } ,
531+ ) ;
532+ const fetch = vi . spyOn ( globalThis , 'fetch' ) ;
533+ const request = cookie
534+ ? mockRequestWithCookies ( { } , { __session : jwt , __client_uat : mockJwtPayload . iat } )
535+ : mockRequestWithHeaderAuth ( { authorization : `Bearer ${ jwt } ` } ) ;
536+ const state = await authenticateRequest (
537+ request ,
538+ mockOptions ( { jwtKey : pemEncodedPublicKey , publishableKey : PK_LIVE } ) ,
539+ ) ;
540+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
541+ expect ( state . isAuthenticated ) . toBe ( ! expired ) ;
542+ if ( expired ) {
543+ expect ( state . status ) . toBe ( AuthStatus . Handshake ) ;
544+ expect ( state . toAuth ( ) ) . toBeNull ( ) ;
545+ } else {
546+ expect ( state . toAuth ( ) ) . toMatchObject ( {
547+ userId : mockJwtPayload . sub ,
548+ sessionId : mockJwtPayload . sid ,
549+ agent : {
550+ type : 'agent' ,
551+ sub : identity . client_id ,
552+ } ,
553+ } ) ;
554+ }
555+ fetch . mockRestore ( ) ;
556+ } ) ;
557+
558+ it . each ( [
559+ { production : true , discovery : true } ,
560+ { production : true , discovery : false } ,
561+ { production : false , discovery : true } ,
562+ { production : false , discovery : false } ,
563+ ] ) (
564+ 'accepts a signed agent handshake with production=$production and discovery=$discovery' ,
565+ async ( { production, discovery } ) => {
566+ const publishableKey = production ? PK_LIVE : PK_TEST ;
567+ const suffix = await getCookieSuffix ( publishableKey ) ;
568+ const { data : sessionToken } = await signJwt (
569+ { ...mockJwtPayload , agt : { id : identity . client_id , reg : identity . client_id , poppy : identity } } ,
570+ signingJwks ,
571+ { algorithm : 'RS256' , header : { ...mockJwtHeader , cat : JWT_CATEGORY_SESSION_TOKEN } } ,
572+ ) ;
573+ const cookies = {
574+ __client_uat : String ( mockJwtPayload . iat ) ,
575+ [ `__client_uat_${ suffix } ` ] : String ( mockJwtPayload . iat ) ,
576+ [ `__session_${ suffix } ` ] : sessionToken ! ,
577+ ...( ! production ? { [ `__clerk_db_jwt_${ suffix } ` ] : 'dev-browser' } : { } ) ,
578+ } ;
579+ const directives = Object . entries ( cookies ) . map (
580+ ( [ name , value ] ) => `${ name } =${ value } ; Path=/; SameSite=Lax; Secure` ,
581+ ) ;
582+ if ( production ) {
583+ directives . unshift ( '__clerk_handshake=; Path=/; Max-Age=0' ) ;
584+ }
585+ const { data : handshake } = await signJwt ( { handshake : directives } , signingJwks , {
586+ algorithm : 'RS256' ,
587+ header : { ...mockJwtHeader , cat : JWT_CATEGORY_SESSION_TOKEN } ,
588+ } ) ;
589+ const url = new URL ( 'https://example.com/' ) ;
590+ if ( ! production ) {
591+ url . searchParams . set ( '__clerk_handshake' , handshake ! ) ;
592+ url . searchParams . set ( '__clerk_db_jwt' , 'dev-browser' ) ;
593+ }
594+ const fetch = vi . spyOn ( globalThis , 'fetch' ) ;
595+ const options = mockOptions ( { jwtKey : pemEncodedPublicKey , publishableKey, poppy : discovery } ) ;
596+ const state = await authenticateRequest (
597+ mockRequestWithCookies ( { } , production ? { __clerk_handshake : handshake ! } : { } , url . toString ( ) ) ,
598+ options ,
599+ ) ;
600+ const expectedAuth = {
601+ userId : mockJwtPayload . sub ,
602+ sessionId : mockJwtPayload . sid ,
603+ agent : { type : 'agent' , sub : identity . client_id } ,
604+ } ;
605+ expect ( state . isAuthenticated ) . toBe ( true ) ;
606+ expect ( state . toAuth ( ) ) . toMatchObject ( expectedAuth ) ;
607+ expect ( state . headers . getSetCookie ( ) ) . toEqual ( directives ) ;
608+ expect ( state . headers . get ( 'location' ) ) . toBe ( production ? null : 'https://example.com/' ) ;
609+ const nextRequest = await authenticateRequest (
610+ mockRequestWithCookies ( { } , cookies , 'https://example.com/' ) ,
611+ options ,
612+ ) ;
613+ expect ( nextRequest . isAuthenticated ) . toBe ( true ) ;
614+ expect ( nextRequest . toAuth ( ) ) . toMatchObject ( expectedAuth ) ;
615+ expect ( fetch ) . not . toHaveBeenCalled ( ) ;
616+ fetch . mockRestore ( ) ;
617+ } ,
618+ ) ;
619+
620+ it ( 'does not interpret an application cookie as authenticated agent identity' , async ( ) => {
621+ const state = await authenticateRequest (
622+ mockRequestWithCookies ( { } , { '__Host-poppy_session' : 'untrusted' } ) ,
623+ mockOptions ( { jwtKey : pemEncodedPublicKey , publishableKey : PK_LIVE , poppy : true } ) ,
624+ ) ;
625+ expect ( state . isAuthenticated ) . toBe ( false ) ;
626+ expect ( state . toAuth ( ) ) . toMatchObject ( { userId : null , sessionId : null } ) ;
627+ expect ( state . toAuth ( ) ) . not . toHaveProperty ( 'agent' ) ;
628+ } ) ;
629+ } ) ;
630+
512631 //
513632 // HTTP Authorization exists
514633 //
0 commit comments