1+ /**
2+ * Copyright (c) 2025 Ping Identity Corporation.
3+ * MIT License
4+ */
15import {
2- HttpServerResponse ,
36 HttpApiMiddleware ,
47 HttpApp ,
58 HttpServerRequest ,
9+ HttpServerResponse ,
610} from '@effect/platform' ;
711import { ResponseError } from '@effect/platform/HttpServerError' ;
812import { Console , Effect , Layer } from 'effect' ;
913
10- class IncrementStepIndex extends HttpApiMiddleware . Tag < IncrementStepIndex > ( ) (
14+ // Export the tag so you can .middleware(IncrementStepIndex) in your spec if desired
15+ export class IncrementStepIndex extends HttpApiMiddleware . Tag < IncrementStepIndex > ( ) (
1116 'IncrementStepIndex' ,
1217) { }
13- const IncrementStepIndexMock = Layer . effect (
18+
19+ export const IncrementStepIndexMock = Layer . effect (
1420 IncrementStepIndex ,
1521 Effect . gen ( function * ( ) {
16- yield * Console . log ( 'In middleware ' ) ;
22+ yield * Console . log ( 'IncrementStepIndex: init ' ) ;
1723
1824 return Effect . gen ( function * ( ) {
19- // Get the current request to read cookies
25+ // Read cookies from the current request
2026 const request = yield * HttpServerRequest . HttpServerRequest ;
2127
2228 // Parse existing stepIndex cookie or default to 0
2329 const cookies = request . cookies ;
24- const currentStepIndex = cookies . stepIndex ? parseInt ( cookies . stepIndex ) : 0 ;
30+ const currentStepIndex = cookies . stepIndex ? parseInt ( cookies . stepIndex , 10 ) : 0 ;
2531
26- // Get the request URL path
27- const urlPath = request . url . split ( '?' ) [ 0 ] ;
28- // Check if this is an end-session request
29- const isEndSessionRequest = urlPath . includes ( '/end_session' ) ;
30- // Determine the new stepIndex based on the request type
32+ // Normalize URL (strip query) and detect special flows
33+ const urlPath = request . url . split ( '?' ) [ 0 ] ?? '' ;
34+ const isEndSessionRequest =
35+ urlPath . includes ( '/endSession' ) || urlPath . includes ( '/end_session' ) ;
36+ const isAuthFlowRequest = urlPath . includes ( '/authorize' ) || urlPath . includes ( '/authenticate' ) ;
37+
38+ // Decide next value
3139 let newStepIndex = currentStepIndex ;
3240 if ( isEndSessionRequest ) {
33- // Reset the stepIndex for end_session requests
3441 newStepIndex = 0 ;
35- yield * Console . log ( 'End session request detected, resetting stepIndex to: ' + newStepIndex ) ;
36- } else if ( urlPath . includes ( '/authorize' ) || urlPath . includes ( '/authenticate' ) ) {
37- // Increment the stepIndex for authorization flow requests
42+ yield * Console . log (
43+ `IncrementStepIndex: end-session detected → resetting stepIndex to ${ newStepIndex } ` ,
44+ ) ;
45+ } else if ( isAuthFlowRequest ) {
3846 newStepIndex = currentStepIndex + 1 ;
3947 yield * Console . log (
40- 'Current stepIndex: ' + currentStepIndex + ', incrementing to: ' + newStepIndex ,
48+ `IncrementStepIndex: auth flow → ${ currentStepIndex } -> ${ newStepIndex } ` ,
4149 ) ;
4250 } else {
43- // For other requests, keep the stepIndex the same
44- yield * Console . log ( 'Request to ' + urlPath + ', keeping stepIndex at: ' + currentStepIndex ) ;
51+ yield * Console . log (
52+ `IncrementStepIndex: other route ${ urlPath } → keeping stepIndex ${ currentStepIndex } ` ,
53+ ) ;
4554 }
4655
47- // Set the appropriate stepIndex cookie in the response
48- yield * HttpApp . appendPreResponseHandler ( ( request , response ) =>
49- HttpServerResponse . setCookie ( response , 'stepIndex' , String ( newStepIndex ) , {
50- // Optional cookie options
56+ // Write cookie just before the response is sent
57+ yield * HttpApp . appendPreResponseHandler ( ( req , res ) =>
58+ HttpServerResponse . setCookie ( res , 'stepIndex' , String ( newStepIndex ) , {
59+ // NOTE: mock defaults; tighten in prod (httpOnly: true, secure: true, sameSite: 'lax')
5160 httpOnly : false ,
5261 secure : false ,
5362 sameSite : 'strict' ,
63+ path : '/' ,
5464 } ) . pipe (
65+ // If cookie setting fails, convert to a typed ResponseError for consistent diagnostics
5566 Effect . catchTag (
5667 'CookieError' ,
5768 ( ) =>
5869 new ResponseError ( {
59- request,
60- response,
70+ request : req ,
71+ response : res ,
6172 reason : 'Decode' ,
6273 cause : 'error updating the stepIndex cookie' ,
6374 } ) ,
@@ -67,5 +78,3 @@ const IncrementStepIndexMock = Layer.effect(
6778 } ) ;
6879 } ) ,
6980) ;
70-
71- export { IncrementStepIndexMock } ;
0 commit comments