|
| 1 | +// OIDC Backchannel Logout Endpoint for SvelteKit RP |
| 2 | +// Receives POST requests from the OP with a logout_token (JWT) |
| 3 | + |
| 4 | +import { validateLogoutToken } from '../validateLogoutToken.js'; |
| 5 | +import { sendLogoutEvent } from '../sseClients.js'; |
| 6 | + |
| 7 | +const corsHeaders = { |
| 8 | + 'Access-Control-Allow-Origin': '*', |
| 9 | + 'Access-Control-Allow-Methods': 'POST, OPTIONS', |
| 10 | + 'Access-Control-Allow-Headers': 'Content-Type', |
| 11 | + 'Accept': '*' |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * POST /api/backchannel-logout |
| 16 | + * Receives a logout_token from the OP and invalidates the user session. |
| 17 | + */ |
| 18 | +export async function POST({ request, locals }) { |
| 19 | + try { |
| 20 | + const headers = { |
| 21 | + 'Content-Type': 'application/json', |
| 22 | + ...corsHeaders |
| 23 | + }; |
| 24 | + let logout_token; |
| 25 | + let data; |
| 26 | + const contentType = request.headers.get('content-type') || ''; |
| 27 | + if (contentType.includes('application/json')) { |
| 28 | + data = await request.json(); |
| 29 | + logout_token = data.logout_token; |
| 30 | + } else if (contentType.includes('application/x-www-form-urlencoded')) { |
| 31 | + const form = await request.formData(); |
| 32 | + logout_token = form.get('logout_token'); |
| 33 | + data = { logout_token }; |
| 34 | + } else { |
| 35 | + return new Response(JSON.stringify({ error: 'Unsupported content type' }), { |
| 36 | + status: 415, |
| 37 | + headers |
| 38 | + }); |
| 39 | + } |
| 40 | + console.log('Received backchannel logout request', { data }); |
| 41 | + if (!logout_token) { |
| 42 | + return new Response(JSON.stringify({ error: 'Missing logout_token' }), { |
| 43 | + status: 400, |
| 44 | + headers |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + // Validate the logout_token (JWT) according to OIDC spec |
| 49 | + let payload; |
| 50 | + try { |
| 51 | + payload = await validateLogoutToken(logout_token); |
| 52 | + } catch (e) { |
| 53 | + return new Response( |
| 54 | + JSON.stringify({ error: 'Invalid logout_token', details: e.message }), |
| 55 | + { |
| 56 | + status: 400, |
| 57 | + headers |
| 58 | + } |
| 59 | + ); |
| 60 | + } |
| 61 | + |
| 62 | + // Notify frontend via SSE if sid is present |
| 63 | + if (payload.sid) { |
| 64 | + sendLogoutEvent(payload.sid, { sub: payload.sub, sid: payload.sid, event: 'logout' }); |
| 65 | + } |
| 66 | + return new Response( |
| 67 | + JSON.stringify({ status: 'logout processed', sub: payload?.sub, sid: payload?.sid }), |
| 68 | + { |
| 69 | + status: 200, |
| 70 | + headers |
| 71 | + } |
| 72 | + ); |
| 73 | + } catch (err) { |
| 74 | + return new Response(JSON.stringify({ error: 'Invalid request', details: err.message }), { |
| 75 | + status: 400, |
| 76 | + headers |
| 77 | + }); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +// Handle preflight OPTIONS requests for CORS |
| 82 | +export async function OPTIONS() { |
| 83 | + return new Response(null, { |
| 84 | + status: 204, |
| 85 | + headers: corsHeaders |
| 86 | + }); |
| 87 | +} |
0 commit comments