66import com .networknt .handler .Handler ;
77import com .networknt .handler .MiddlewareHandler ;
88import com .networknt .utility .ModuleRegistry ;
9+ import com .networknt .utility .StringUtils ;
910import io .undertow .Handlers ;
1011import io .undertow .server .HttpHandler ;
1112import io .undertow .server .HttpServerExchange ;
@@ -99,8 +100,50 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
99100 }
100101 }
101102 } else if (BEARER_PREFIX .equalsIgnoreCase (authorization .substring (0 , 6 ))) {
103+ // in the case that a bearer token is used, there are three scenarios: both jwt and swt are true, only jwt is true and only swt is true
104+ // in the first case, we need to identify if the token is jwt or swt before calling the right handler to verify it.
102105 Map <String , HttpHandler > handlers = Handler .getHandlers ();
103- if (pathPrefixAuth .isJwt ()) {
106+ if (pathPrefixAuth .isJwt () && pathPrefixAuth .isSwt ()) {
107+ // both jwt and swt are enabled.
108+ boolean isJwt = StringUtils .isJwtToken (authorization );
109+ if (logger .isTraceEnabled ()) logger .trace ("Both jwt and swt are true and check token is jwt = {}" , isJwt );
110+ if (isJwt ) {
111+ JwtVerifyHandler handler = (JwtVerifyHandler ) handlers .get (JWT );
112+ if (handler == null ) {
113+ logger .error ("Cannot find JwtVerifyHandler with alias name jwt." );
114+ setExchangeStatus (exchange , HANDLER_NOT_FOUND , "com.networknt.openapi.JwtVerifyHandler@jwt" );
115+ exchange .endExchange ();
116+ return ;
117+ } else {
118+ // get the jwkServiceIds list.
119+ if (handler .handleJwt (exchange , pathPrefixAuth .getPathPrefix (), reqPath , pathPrefixAuth .getJwkServiceIds ())) {
120+ // verification is passed, go to the next handler in the chain.
121+ break ;
122+ } else {
123+ // verification is not passed and an error is returned. Don't call the next handler.
124+ return ;
125+ }
126+ }
127+ } else {
128+ SwtVerifyHandler handler = (SwtVerifyHandler ) handlers .get (SWT );
129+ if (handler == null ) {
130+ logger .error ("Cannot find SwtVerifyHandler with alias name swt." );
131+ setExchangeStatus (exchange , HANDLER_NOT_FOUND , "com.networknt.openapi.SwtVerifyHandler@swt" );
132+ exchange .endExchange ();
133+ return ;
134+ } else {
135+ // get the jwkServiceIds list.
136+ if (handler .handleSwt (exchange , reqPath , pathPrefixAuth .getSwtServiceIds ())) {
137+ // verification is passed, go to the next handler in the chain.
138+ break ;
139+ } else {
140+ // verification is not passed and an error is returned. Don't call the next handler.
141+ return ;
142+ }
143+ }
144+ }
145+ } else if (pathPrefixAuth .isJwt ()) {
146+ // only jwt is enabled
104147 JwtVerifyHandler handler = (JwtVerifyHandler ) handlers .get (JWT );
105148 if (handler == null ) {
106149 logger .error ("Cannot find JwtVerifyHandler with alias name jwt." );
@@ -118,7 +161,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
118161 }
119162 }
120163 } else {
121- // this must be swt token
164+ // only swt is enabled
122165 SwtVerifyHandler handler = (SwtVerifyHandler ) handlers .get (SWT );
123166 if (handler == null ) {
124167 logger .error ("Cannot find SwtVerifyHandler with alias name swt." );
@@ -127,7 +170,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
127170 return ;
128171 } else {
129172 // get the jwkServiceIds list.
130- if (handler .handleSwt (exchange , reqPath , pathPrefixAuth .getJwkServiceIds ())) {
173+ if (handler .handleSwt (exchange , reqPath , pathPrefixAuth .getSwtServiceIds ())) {
131174 // verification is passed, go to the next handler in the chain.
132175 break ;
133176 } else {
@@ -184,6 +227,7 @@ public void handleRequest(HttpServerExchange exchange) throws Exception {
184227 Handler .next (exchange , next );
185228 }
186229
230+
187231 @ Override
188232 public HttpHandler getNext () {
189233 return next ;
0 commit comments