@@ -20,30 +20,28 @@ import {McpAuthOptions, PROTECTED_RESOURCE_URL, validateToken} from '@asgardeo/m
2020import { NextFunction , Request , Response } from 'express' ;
2121
2222export default function protectedRoute ( options : McpAuthOptions ) {
23- return async function protectedMiddleware (
24- req : Request ,
25- res : Response ,
26- next : NextFunction ,
27- ) : Promise < Response < any , Record < string , any > > | undefined > {
23+ return async function protectedMiddleware ( req : Request , res : Response , next : NextFunction ) : Promise < void > {
2824 const authHeader : string | undefined = req . headers . authorization ;
2925
3026 if ( ! authHeader ) {
3127 res . setHeader (
3228 'WWW-Authenticate' ,
3329 `Bearer resource_metadata="${ req . protocol } ://${ req . get ( 'host' ) } ${ PROTECTED_RESOURCE_URL } "` ,
3430 ) ;
35- return res . status ( 401 ) . json ( {
31+ res . status ( 401 ) . json ( {
3632 error : 'unauthorized' ,
3733 error_description : 'Missing authorization token' ,
3834 } ) ;
35+ return ;
3936 }
4037
4138 const parts : string [ ] = authHeader . split ( ' ' ) ;
4239 if ( parts . length !== 2 || parts [ 0 ] !== 'Bearer' ) {
43- return res . status ( 401 ) . json ( {
40+ res . status ( 401 ) . json ( {
4441 error : 'invalid_token' ,
4542 error_description : 'Authorization header must be in format: Bearer [token]' ,
4643 } ) ;
44+ return ;
4745 }
4846
4947 const token : string = parts [ 1 ] ;
@@ -69,9 +67,8 @@ export default function protectedRoute(options: McpAuthOptions) {
6967 try {
7068 await validateToken ( token , TOKEN_VALIDATION_CONFIG . jwksUri , TOKEN_VALIDATION_CONFIG . options ) ;
7169 next ( ) ;
72- return undefined ;
7370 } catch ( error : any ) {
74- return res . status ( 401 ) . json ( {
71+ res . status ( 401 ) . json ( {
7572 error : 'invalid_token' ,
7673 error_description : error . message || 'Invalid or expired token' ,
7774 } ) ;
0 commit comments