1- import * as crypto from 'node:crypto' ;
2-
31import { InvalidClient , InvalidRequest , OIDCProviderError } from '../helpers/errors.js' ;
42import * as JWT from '../helpers/jwt.js' ;
53import redirectUri from '../helpers/redirect_uri.js' ;
@@ -11,6 +9,7 @@ import sessionMiddleware from '../shared/session.js';
119import revoke from '../helpers/revoke.js' ;
1210import noCache from '../shared/no_cache.js' ;
1311import formPost from '../response_modes/form_post.js' ;
12+ import { generateXsrf , checkXsrf } from '../shared/xsrf.js' ;
1413
1514const parseBody = bodyParser . bind ( undefined , 'application/x-www-form-urlencoded' ) ;
1615
@@ -70,16 +69,14 @@ export const init = [
7069 await next ( ) ;
7170 } ,
7271
72+ generateXsrf ,
73+
7374 async function renderLogout ( ctx ) {
74- // TODO: generic xsrf middleware to remove this
75- const secret = crypto . randomBytes ( 24 ) . toString ( 'hex' ) ;
75+ const { secret } = ctx . oidc . session . state ;
7676
77- ctx . oidc . session . state = {
78- secret,
79- clientId : ctx . oidc . client ? ctx . oidc . client . clientId : undefined ,
80- state : ctx . oidc . params . state ,
81- postLogoutRedirectUri : ctx . oidc . params . post_logout_redirect_uri ,
82- } ;
77+ ctx . oidc . session . state . clientId = ctx . oidc . client ? ctx . oidc . client . clientId : undefined ;
78+ ctx . oidc . session . state . state = ctx . oidc . params . state ;
79+ ctx . oidc . session . state . postLogoutRedirectUri = ctx . oidc . params . post_logout_redirect_uri ;
8380
8481 const action = ctx . oidc . urlFor ( 'end_session_confirm' ) ;
8582
@@ -105,15 +102,7 @@ export const confirm = [
105102 paramsMiddleware . bind ( undefined , new Set ( [ 'xsrf' , 'logout' ] ) ) ,
106103 rejectDupes . bind ( undefined , { } ) ,
107104
108- async function checkLogoutToken ( ctx , next ) {
109- if ( ! ctx . oidc . session . state ) {
110- throw new InvalidRequest ( 'could not find logout details' ) ;
111- }
112- if ( ctx . oidc . session . state . secret !== ctx . oidc . params . xsrf ) {
113- throw new InvalidRequest ( 'xsrf token invalid' ) ;
114- }
115- await next ( ) ;
116- } ,
105+ checkXsrf ( 'could not find logout details' ) ,
117106
118107 async function endSession ( ctx ) {
119108 const { oidc : { session, params } } = ctx ;
0 commit comments