|
| 1 | +/* |
| 2 | + * Copyright (c) 2025 Ping Identity Corporation. All rights reserved. |
| 3 | + * |
| 4 | + * This software may be modified and distributed under the terms |
| 5 | + * of the MIT license. See the LICENSE file for details. |
| 6 | + */ |
| 7 | +import { Effect, Console } from 'effect'; |
| 8 | +import { HttpApiBuilder, HttpServerRequest } from '@effect/platform'; |
| 9 | +import { MockApi } from '../spec.js'; |
| 10 | +import { SessionStorage } from '../services/session.service.js'; |
| 11 | + |
| 12 | +export const EndSessionHandlerMock = HttpApiBuilder.group( |
| 13 | + MockApi, |
| 14 | + 'SessionManagement', |
| 15 | + (handlers) => |
| 16 | + handlers.handle('EndSession', () => |
| 17 | + Effect.gen(function* () { |
| 18 | + const sessionStorage = yield* SessionStorage; |
| 19 | + |
| 20 | + const request = yield* HttpServerRequest.HttpServerRequest; |
| 21 | + |
| 22 | + const sessionId = request.cookies.sessionId; |
| 23 | + |
| 24 | + if (sessionId) { |
| 25 | + yield* sessionStorage.deleteSession(sessionId); |
| 26 | + } else { |
| 27 | + yield* Console.log('No active session'); |
| 28 | + } |
| 29 | + |
| 30 | + const urlParams = request.url.includes('?') |
| 31 | + ? new URLSearchParams(request.url.split('?')[1]) |
| 32 | + : new URLSearchParams(); |
| 33 | + |
| 34 | + const redirectUri = urlParams.get('post_logout_redirect_uri'); |
| 35 | + const state = urlParams.get('state'); |
| 36 | + |
| 37 | + if (redirectUri) { |
| 38 | + // For a full OIDC-compliant implementation, we would validate: |
| 39 | + // 1. If id_token_hint is provided, validate it |
| 40 | + // 2. Verify that redirectUri is registered for this client |
| 41 | + |
| 42 | + // Create a proper HTTP redirect (302 Found) response |
| 43 | + const targetUrl = state |
| 44 | + ? `${redirectUri}?state=${encodeURIComponent(state)}` |
| 45 | + : redirectUri; |
| 46 | + |
| 47 | + return { |
| 48 | + status: 302, |
| 49 | + headers: { |
| 50 | + Location: targetUrl, |
| 51 | + 'Cache-Control': 'no-store', |
| 52 | + }, |
| 53 | + body: '', |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + // Default response if no redirect |
| 58 | + return 'Logged out successfully'; |
| 59 | + }).pipe(Effect.withSpan('EndSessionHandler')), |
| 60 | + ), |
| 61 | +); |
0 commit comments