@@ -18,14 +18,14 @@ import { beforeEach, describe, expect, it, vi } from 'vitest'
1818const {
1919 mockMergeSubblockStateWithValues,
2020 mockMergeSubBlockValues,
21- mockValidateAuthToken ,
21+ mockReadDeploymentAuthToken ,
2222 mockSetDeploymentAuthCookie,
2323 mockIsEmailAllowed,
2424 mockCheckRateLimitDirect,
2525} = vi . hoisted ( ( ) => ( {
2626 mockMergeSubblockStateWithValues : vi . fn ( ) . mockReturnValue ( { } ) ,
2727 mockMergeSubBlockValues : vi . fn ( ) . mockReturnValue ( { } ) ,
28- mockValidateAuthToken : vi . fn ( ) . mockReturnValue ( false ) ,
28+ mockReadDeploymentAuthToken : vi . fn ( ) . mockResolvedValue ( null ) ,
2929 mockSetDeploymentAuthCookie : vi . fn ( ) ,
3030 mockIsEmailAllowed : vi . fn ( ) ,
3131 mockCheckRateLimitDirect : vi . fn ( ) . mockResolvedValue ( { allowed : true } ) ,
@@ -57,7 +57,7 @@ vi.mock('@sim/workflow-persistence/subblocks', () => ({
5757vi . mock ( '@/lib/core/security/encryption' , ( ) => encryptionMock )
5858
5959vi . mock ( '@/lib/core/security/deployment' , ( ) => ( {
60- validateAuthToken : mockValidateAuthToken ,
60+ readDeploymentAuthToken : mockReadDeploymentAuthToken ,
6161 setDeploymentAuthCookie : mockSetDeploymentAuthCookie ,
6262 isEmailAllowed : mockIsEmailAllowed ,
6363 deploymentAuthCookieName : ( prefix : string , id : string ) => `${ prefix } _auth_${ id } ` ,
@@ -84,7 +84,7 @@ describe('Chat API Utils', () => {
8484
8585 describe ( 'Auth token utils' , ( ) => {
8686 it ( 'should accept valid auth cookie via validateChatAuth' , async ( ) => {
87- mockValidateAuthToken . mockReturnValue ( true )
87+ mockReadDeploymentAuthToken . mockResolvedValue ( { } )
8888
8989 const deployment = {
9090 id : 'chat-id' ,
@@ -100,15 +100,15 @@ describe('Chat API Utils', () => {
100100 } as any
101101
102102 const result = await validateChatAuth ( 'request-id' , deployment , mockRequest )
103- expect ( mockValidateAuthToken ) . toHaveBeenCalledWith ( {
103+ expect ( mockReadDeploymentAuthToken ) . toHaveBeenCalledWith ( {
104104 token : 'valid-token' ,
105105 resource : deployment ,
106106 } )
107107 expect ( result . authorized ) . toBe ( true )
108108 } )
109109
110110 it ( 'should reject invalid auth cookie via validateChatAuth' , async ( ) => {
111- mockValidateAuthToken . mockReturnValue ( false )
111+ mockReadDeploymentAuthToken . mockResolvedValue ( null )
112112
113113 const deployment = {
114114 id : 'chat-id' ,
@@ -126,10 +126,32 @@ describe('Chat API Utils', () => {
126126 const result = await validateChatAuth ( 'request-id' , deployment , mockRequest )
127127 expect ( result . authorized ) . toBe ( false )
128128 } )
129+
130+ it ( 'returns the authenticated email carried by a valid email-auth cookie' , async ( ) => {
131+ mockReadDeploymentAuthToken . mockResolvedValue ( {
132+ authenticatedEmail : 'person@example.com' ,
133+ } )
134+
135+ const deployment = {
136+ id : 'chat-id' ,
137+ authType : 'email' ,
138+ }
139+ const mockRequest = {
140+ method : 'POST' ,
141+ cookies : {
142+ get : vi . fn ( ) . mockReturnValue ( { value : 'valid-token' } ) ,
143+ } ,
144+ } as any
145+
146+ await expect ( validateChatAuth ( 'request-id' , deployment , mockRequest ) ) . resolves . toEqual ( {
147+ authorized : true ,
148+ authenticatedEmail : 'person@example.com' ,
149+ } )
150+ } )
129151 } )
130152
131153 describe ( 'Cookie handling' , ( ) => {
132- it ( 'should delegate to setDeploymentAuthCookie' , ( ) => {
154+ it ( 'should delegate to setDeploymentAuthCookie' , async ( ) => {
133155 const mockResponse = {
134156 cookies : { set : vi . fn ( ) } ,
135157 } as unknown as NextResponse
@@ -139,7 +161,7 @@ describe('Chat API Utils', () => {
139161 authType : 'password' ,
140162 password : 'encrypted-password' ,
141163 }
142- setChatAuthCookie ( mockResponse , deployment )
164+ await setChatAuthCookie ( mockResponse , deployment )
143165
144166 expect ( mockSetDeploymentAuthCookie ) . toHaveBeenCalledWith ( {
145167 response : mockResponse ,
@@ -148,6 +170,26 @@ describe('Chat API Utils', () => {
148170 verifiedEmail : undefined ,
149171 } )
150172 } )
173+
174+ it ( 'forwards an authenticated email into the signed deployment cookie' , async ( ) => {
175+ const mockResponse = {
176+ cookies : { set : vi . fn ( ) } ,
177+ } as unknown as NextResponse
178+
179+ const deployment = {
180+ id : 'test-chat-id' ,
181+ authType : 'email' ,
182+ allowedEmails : [ 'person@example.com' ] ,
183+ }
184+ await setChatAuthCookie ( mockResponse , deployment , 'person@example.com' )
185+
186+ expect ( mockSetDeploymentAuthCookie ) . toHaveBeenCalledWith ( {
187+ response : mockResponse ,
188+ cookiePrefix : 'chat' ,
189+ resource : deployment ,
190+ verifiedEmail : 'person@example.com' ,
191+ } )
192+ } )
151193 } )
152194
153195 describe ( 'Chat auth validation' , ( ) => {
@@ -429,14 +471,17 @@ describe('Chat API Utils', () => {
429471 } )
430472
431473 it ( 'authorizes execution when session email is allowlisted' , async ( ) => {
432- mockGetSession . mockResolvedValue ( { user : { email : 'user@example .com' } } )
474+ mockGetSession . mockResolvedValue ( { user : { email : 'User@Example .com' } } )
433475 mockIsEmailAllowed . mockReturnValue ( true )
434476
435477 const result = await validateChatAuth ( 'request-id' , ssoDeployment , postRequest , {
436478 input : 'hello' ,
437479 } )
438480
439- expect ( result . authorized ) . toBe ( true )
481+ expect ( result ) . toEqual ( {
482+ authorized : true ,
483+ authenticatedEmail : 'user@example.com' ,
484+ } )
440485 } )
441486
442487 it ( 'rejects execution when session email is not allowlisted' , async ( ) => {
0 commit comments