@@ -6,48 +6,22 @@ const { isEnabled } = require('@librechat/api');
66 * Custom Middleware to handle JWT authentication, with support for OpenID token reuse
77 * Switches between JWT and OpenID authentication based on cookies and environment settings
88 */
9- const { logger } = require ( '@librechat/data-schemas' ) ;
10-
119const requireJwtAuth = ( req , res , next ) => {
1210 // Check if token provider is specified in cookies
1311 const cookieHeader = req . headers . cookie ;
1412 const tokenProvider = cookieHeader ? cookies . parse ( cookieHeader ) . token_provider : null ;
15- const hasAuthHeader = ! ! req . headers . authorization ;
16-
17- logger . info ( '[requireJwtAuth] Authentication check' , {
18- path : req . path ,
19- method : req . method ,
20- hasCookie : ! ! cookieHeader ,
21- tokenProvider,
22- hasAuthHeader,
23- authHeaderPrefix : req . headers . authorization ?. substring ( 0 , 30 ) ,
24- openidReuseTokens : isEnabled ( process . env . OPENID_REUSE_TOKENS ) ,
25- } ) ;
2613
2714 // Use OpenID authentication if token provider is OpenID and OPENID_REUSE_TOKENS is enabled
2815 if ( tokenProvider === 'openid' && isEnabled ( process . env . OPENID_REUSE_TOKENS ) ) {
29- logger . debug ( '[requireJwtAuth] Using OpenID JWT authentication' ) ;
3016 return passport . authenticate ( 'openidJwt' , { session : false } ) ( req , res , next ) ;
3117 }
3218
3319 // Default to standard JWT authentication
34- logger . debug ( '[requireJwtAuth] Using standard JWT authentication' ) ;
35-
36- // Add error handler to log authentication failures
3720 return passport . authenticate ( 'jwt' , { session : false } , ( err , user , info ) => {
3821 if ( err ) {
39- logger . error ( '[requireJwtAuth] Authentication error' , {
40- error : err . message ,
41- stack : err . stack ,
42- } ) ;
4322 return res . status ( 401 ) . json ( { error : 'Authentication failed' , message : err . message } ) ;
4423 }
4524 if ( ! user ) {
46- logger . warn ( '[requireJwtAuth] Authentication failed - no user' , {
47- info : info ?. message || 'No user returned from JWT strategy' ,
48- hasAuthHeader,
49- tokenProvider,
50- } ) ;
5125 return res . status ( 401 ) . json ( {
5226 error : 'Authentication required' ,
5327 message : 'No valid JWT token found. Make sure you are logged in and the Authorization header is sent.' ,
0 commit comments