Skip to content

Commit 61569ac

Browse files
committed
Fix scope validation issue when multiple security schemes are configured
1 parent cdb10a3 commit 61569ac

File tree

3 files changed

+17
-15
lines changed

3 files changed

+17
-15
lines changed

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/KeyValidator.java

+6-15
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,13 @@ public static boolean validateScopes(TokenValidationContext validationContext) t
8686

8787
ResourceConfig matchedResource = validationContext.getMatchingResourceConfig();
8888
boolean scopesValidated = false;
89-
if (matchedResource.getSecuritySchemas().entrySet().size() > 0) {
90-
for (Map.Entry<String, List<String>> pair : matchedResource.getSecuritySchemas().entrySet()) {
91-
boolean validate = false;
92-
if (pair.getValue() != null && pair.getValue().size() > 0) {
93-
scopesValidated = false;
94-
for (String scope : pair.getValue()) {
95-
if (scopesSet.contains(scope)) {
96-
scopesValidated = true;
97-
validate = true;
98-
break;
99-
}
100-
}
101-
} else {
89+
90+
List<String> requiredScopes = matchedResource.getSecuritySchemas()
91+
.get(validationContext.getSecurityScheme());
92+
if (requiredScopes != null && !requiredScopes.isEmpty()) {
93+
for (String scope : requiredScopes) {
94+
if (scopesSet.contains(scope)) {
10295
scopesValidated = true;
103-
}
104-
if (validate) {
10596
break;
10697
}
10798
}

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/TokenValidationContext.java

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ public class TokenValidationContext {
4646
private AccessTokenInfo tokenInfo;
4747
private String authorizationCode;
4848
private String tenantDomain;
49+
private String securityScheme;
4950
private List<String> keyManagers = new ArrayList<>();
5051

5152
public AccessTokenInfo getTokenInfo() {
@@ -179,5 +180,13 @@ public void setKeyManagers(List<String> keyManagers) {
179180

180181
this.keyManagers = keyManagers;
181182
}
183+
184+
public String getSecurityScheme() {
185+
return securityScheme;
186+
}
187+
188+
public void setSecurityScheme(String securityScheme) {
189+
this.securityScheme = securityScheme;
190+
}
182191
}
183192

enforcer-parent/enforcer/src/main/java/org/wso2/choreo/connect/enforcer/security/jwt/JWTAuthenticator.java

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@
8383
public class JWTAuthenticator implements Authenticator {
8484

8585
private static final Logger log = LogManager.getLogger(JWTAuthenticator.class);
86+
private static final String SWAGGER_OAUTH2_SECURITY_SCHEME_NAME = "default";
8687
private final JWTValidator jwtValidator = new JWTValidator();
8788
private final boolean isGatewayTokenCacheEnabled;
8889
private AbstractAPIMgtGatewayJWTGenerator jwtGenerator;
@@ -573,6 +574,7 @@ private void validateScopes(String apiContext, String apiVersion, ResourceConfig
573574
tokenValidationContext.setMatchingResourceConfig(matchingResource);
574575
tokenValidationContext.setContext(apiContext);
575576
tokenValidationContext.setVersion(apiVersion);
577+
tokenValidationContext.setSecurityScheme(SWAGGER_OAUTH2_SECURITY_SCHEME_NAME);
576578

577579
boolean valid = KeyValidator.validateScopes(tokenValidationContext);
578580
if (valid) {

0 commit comments

Comments
 (0)