@@ -171,30 +171,8 @@ public AuthenticationContext authenticate(RequestContext requestContext) throws
171
171
Utils .setTag (jwtAuthenticatorInfoSpan , APIConstants .LOG_TRACE_ID ,
172
172
ThreadContext .get (APIConstants .LOG_TRACE_ID ));
173
173
}
174
- String authHeaderVal = retrieveAuthHeaderValue (requestContext );
175
174
176
- if (authHeaderVal == null
177
- && requestContext .getMatchedAPI ().getApiType ().equalsIgnoreCase (APIConstants .ApiType .WEB_SOCKET )) {
178
- String tokenValue = extractJWTInWSProtocolHeader (requestContext );
179
- if (StringUtils .isNotEmpty (tokenValue )) {
180
- authHeaderVal = JWTConstants .BEARER + " " + tokenValue ;
181
- }
182
- }
183
-
184
- if (authHeaderVal == null || !authHeaderVal .toLowerCase ().contains (JWTConstants .BEARER )) {
185
- throw new APISecurityException (APIConstants .StatusCodes .UNAUTHENTICATED .getCode (),
186
- APISecurityConstants .API_AUTH_MISSING_CREDENTIALS , "Missing Credentials" );
187
- }
188
- String [] splitToken = authHeaderVal .split ("\\ s" );
189
- String token = authHeaderVal ;
190
- // Extract the token when it is sent as bearer token. i.e Authorization: Bearer <token>
191
- if (splitToken .length > 1 ) {
192
- token = splitToken [1 ];
193
- }
194
- // Handle PAT logic
195
- if (isPATEnabled && token .startsWith (APIKeyConstants .PAT_PREFIX )) {
196
- token = exchangeJWTForPAT (requestContext , token );
197
- }
175
+ String token = retrieveTokenFromRequestCtx (requestContext );
198
176
String context = requestContext .getMatchedAPI ().getBasePath ();
199
177
String name = requestContext .getMatchedAPI ().getName ();
200
178
String version = requestContext .getMatchedAPI ().getVersion ();
@@ -266,7 +244,7 @@ public AuthenticationContext authenticate(RequestContext requestContext) throws
266
244
ThreadContext .get (APIConstants .LOG_TRACE_ID ));
267
245
}
268
246
// if the token is self contained, validation subscription from `subscribedApis` claim
269
- JSONObject api = validateSubscriptionFromClaim (name , version , claims , splitToken ,
247
+ JSONObject api = validateSubscriptionFromClaim (name , version , claims , token ,
270
248
apiKeyValidationInfoDTO , true );
271
249
if (api == null ) {
272
250
if (log .isDebugEnabled ()) {
@@ -527,6 +505,40 @@ private String retrieveAuthHeaderValue(RequestContext requestContext) {
527
505
return headers .get (FilterUtils .getAuthHeaderName (requestContext ));
528
506
}
529
507
508
+ /**
509
+ * Extract the JWT token from the request context.
510
+ *
511
+ * @param requestContext Request context
512
+ * @return JWT token
513
+ * @throws APISecurityException If an error occurs while extracting the JWT token
514
+ */
515
+ protected String retrieveTokenFromRequestCtx (RequestContext requestContext ) throws APISecurityException {
516
+
517
+ String authHeaderVal = retrieveAuthHeaderValue (requestContext );
518
+ if (authHeaderVal == null
519
+ && requestContext .getMatchedAPI ().getApiType ().equalsIgnoreCase (APIConstants .ApiType .WEB_SOCKET )) {
520
+ String tokenValue = extractJWTInWSProtocolHeader (requestContext );
521
+ if (StringUtils .isNotEmpty (tokenValue )) {
522
+ authHeaderVal = JWTConstants .BEARER + " " + tokenValue ;
523
+ }
524
+ }
525
+ if (authHeaderVal == null || !authHeaderVal .toLowerCase ().contains (JWTConstants .BEARER )) {
526
+ throw new APISecurityException (APIConstants .StatusCodes .UNAUTHENTICATED .getCode (),
527
+ APISecurityConstants .API_AUTH_MISSING_CREDENTIALS , "Missing Credentials" );
528
+ }
529
+ String [] splitToken = authHeaderVal .split ("\\ s" );
530
+ String token = authHeaderVal ;
531
+ // Extract the token when it is sent as bearer token. i.e Authorization: Bearer <token>
532
+ if (splitToken .length > 1 ) {
533
+ token = splitToken [1 ];
534
+ }
535
+ // Handle PAT logic
536
+ if (isPATEnabled && token .startsWith (APIKeyConstants .PAT_PREFIX )) {
537
+ token = exchangeJWTForPAT (requestContext , token );
538
+ }
539
+ return token ;
540
+ }
541
+
530
542
@ Override
531
543
public int getPriority () {
532
544
return 10 ;
@@ -612,9 +624,9 @@ private APIKeyValidationInfoDTO validateSubscriptionUsingKeyManager(RequestConte
612
624
* If the subscription information is not found, return a null object.
613
625
* @throws APISecurityException if the user is not subscribed to the API
614
626
*/
615
- private JSONObject validateSubscriptionFromClaim (String name , String version , JWTClaimsSet payload ,
616
- String [] splitToken , APIKeyValidationInfoDTO validationInfo ,
617
- boolean isOauth ) throws APISecurityException {
627
+ private JSONObject validateSubscriptionFromClaim (String name , String version , JWTClaimsSet payload , String token ,
628
+ APIKeyValidationInfoDTO validationInfo , boolean isOauth )
629
+ throws APISecurityException {
618
630
JSONObject api = null ;
619
631
try {
620
632
validationInfo .setEndUserName (payload .getSubject ());
@@ -678,15 +690,15 @@ private JSONObject validateSubscriptionFromClaim(String name, String version, JW
678
690
}
679
691
if (log .isDebugEnabled ()) {
680
692
log .debug ("User is subscribed to the API: " + name + ", " +
681
- "version: " + version + ". Token: " + FilterUtils .getMaskedToken (splitToken [ 0 ] ));
693
+ "version: " + version + ". Token: " + FilterUtils .getMaskedToken (token ));
682
694
}
683
695
break ;
684
696
}
685
697
}
686
698
if (api == null ) {
687
699
if (log .isDebugEnabled ()) {
688
700
log .debug ("User is not subscribed to access the API: " + name +
689
- ", version: " + version + ". Token: " + FilterUtils .getMaskedToken (splitToken [ 0 ] ));
701
+ ", version: " + version + ". Token: " + FilterUtils .getMaskedToken (token ));
690
702
}
691
703
log .error ("User is not subscribed to access the API." );
692
704
throw new APISecurityException (APIConstants .StatusCodes .UNAUTHORIZED .getCode (),
0 commit comments