Skip to content

Commit

Permalink
refactor-2 subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
tharindu1st committed Dec 12, 2023
1 parent 09d025d commit f3aeb0b
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public class APIKeyValidationInfoDTO implements Serializable {
private String apiContext;
private String applicationUUID;
private Map<String, String> appAttributes;
private String envType;

public String getOrganization() {

Expand Down Expand Up @@ -431,5 +432,15 @@ public void setApiUUID(String apiUUID) {

this.apiUUID = apiUUID;
}

public void setEnvType(String envType) {

this.envType = envType;
}

public String getEnvType() {

return envType;
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.wso2.apk.enforcer.models;

import org.wso2.apk.enforcer.common.CacheableEntity;
import org.wso2.apk.enforcer.subscription.SubscriptionDataStoreUtil;

/**
* Entity for keeping mapping between Application and Consumer key.
Expand All @@ -32,52 +33,65 @@ public class ApplicationKeyMapping implements CacheableEntity<String> {
private String envId;

public String getApplicationUUID() {

return applicationUUID;
}

public void setApplicationUUID(String applicationUUID) {

this.applicationUUID = applicationUUID;
}

public String getSecurityScheme() {

return securityScheme;
}

public void setSecurityScheme(String securityScheme) {

this.securityScheme = securityScheme;
}

public String getApplicationIdentifier() {

return applicationIdentifier;
}

public void setApplicationIdentifier(String applicationIdentifier) {

this.applicationIdentifier = applicationIdentifier;
}

public String getKeyType() {

return keyType;
}

public void setKeyType(String keyType) {

this.keyType = keyType;
}

public String getEnvId() {

return envId;
}

public void setEnvId(String envId) {

this.envId = envId;
}

@Override
public String getCacheKey() {
return securityScheme + CacheableEntity.DELEM_PERIOD + applicationIdentifier;

return SubscriptionDataStoreUtil.getApplicationKeyMappingCacheKey(applicationIdentifier, keyType,
securityScheme, envId);
}

@Override
public String toString() {

return "ApplicationKeyMapping{" +
"applicationUUID='" + applicationUUID + '\'' +
", securityScheme='" + securityScheme + '\'' +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ public static void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO
throws APISecurityException {

Application app;
Subscription sub;
Subscription sub = null;
ApplicationKeyMapping keyMapping;
ApplicationMapping appMapping;
Set<ApplicationMapping> appMappings;
String apiName = validationInfo.getApiName();
String apiContext = validationInfo.getApiContext();
String apiVersion = validationInfo.getApiVersion();
Expand All @@ -138,19 +138,33 @@ public static void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO

if (datastore != null) {
// Get application key mapping using the consumer key, key type and security scheme
keyMapping = datastore.getMatchingApplicationKeyMapping(consumerKey, keyType, securityScheme);
keyMapping = datastore.getMatchingApplicationKeyMapping(consumerKey, keyType, securityScheme,
validationInfo.getEnvType());

if (keyMapping != null) {
// Get application and application mapping using application UUID
String applicationUUID = keyMapping.getApplicationUUID();
app = datastore.getMatchingApplication(applicationUUID);
appMapping = datastore.getMatchingApplicationMapping(applicationUUID);
appMappings = datastore.getMatchingApplicationMappings(applicationUUID);

if (appMapping != null && app != null) {
if (appMappings != null && app != null) {
// Get subscription using the subscription UUID
String subscriptionUUID = appMapping.getSubscriptionUUID();
sub = datastore.getMatchingSubscription(subscriptionUUID);
for (ApplicationMapping appMapping : appMappings) {
String subscriptionUUID = appMapping.getSubscriptionUUID();
Subscription subscription = datastore.getMatchingSubscription(subscriptionUUID);

if (validationInfo.getApiName().equals(subscription.getSubscribedApi().getName())) {
// Validate API version
String versionRegex = subscription.getSubscribedApi().getVersion();
String versionToMatch = validationInfo.getApiVersion();
Pattern pattern = Pattern.compile(versionRegex);
Matcher matcher = pattern.matcher(versionToMatch);
if (!matcher.matches()) {
sub = subscription;
break;
}
}
}
// Validate subscription
if (sub != null) {
validate(validationInfo, app, sub);
Expand Down Expand Up @@ -258,23 +272,6 @@ private static void validate(APIKeyValidationInfoDTO infoDTO, Application app, S
infoDTO.setAuthorized(false);
return;
}

// Validate API details embedded within the subscription
// Validate API name
if (!infoDTO.getApiName().equals(sub.getSubscribedApi().getName())) {
infoDTO.setAuthorized(false);
return;
}
// Validate API version
String versionRegex = sub.getSubscribedApi().getVersion();
String versionToMatch = infoDTO.getApiVersion();
Pattern pattern = Pattern.compile(versionRegex);
Matcher matcher = pattern.matcher(versionToMatch);
if (!matcher.matches()) {
infoDTO.setAuthorized(false);
return;
}

infoDTO.setApplicationUUID(app.getUUID());
infoDTO.setSubscriber(app.getOwner());
infoDTO.setApplicationName(app.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,8 @@ private void validateSubscriptionUsingConsumerKey(APIKeyValidationInfoDTO valida
validationInfo.setApiVersion(version);
validationInfo.setApiContext(context);
validationInfo.setConsumerKey(consumerKey);
validationInfo.setType(envType);
validationInfo.setType(matchedAPI.getApiType());
validationInfo.setEnvType(envType);
validationInfo.setSecurityScheme(APIConstants.API_SECURITY_OAUTH2);
validationInfo.setSubscriberOrganization(organization);
validationInfo.setApiContext(matchedAPI.getBasePath());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.wso2.apk.enforcer.security.jwt.validator.JWTValidator;

import java.util.List;
import java.util.Set;

/**
* A Facade for obtaining Subscription related Data.
Expand Down Expand Up @@ -60,18 +61,19 @@ void addApplicationKeyMappings(
* @param uuid Application UUID
* @return ApplicationMapping which match the given UUID
*/
ApplicationMapping getMatchingApplicationMapping(String uuid);
Set<ApplicationMapping> getMatchingApplicationMappings(String uuid);

/**
* Filter the application key mapping map based on provided parameters
*
* @param applicationIdentifier Application identifier
* @param keyType Key type, i.e. PRODUCTION or SANDBOX
* @param securityScheme Security scheme
* @param envType
* @return ApplicationKeyMapping which match the given parameters
*/
ApplicationKeyMapping getMatchingApplicationKeyMapping(String applicationIdentifier, String keyType,
String securityScheme);
String securityScheme, String envType);

/**
* Filter the applications map based on the provided parameters.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
Expand Down Expand Up @@ -78,8 +80,6 @@ public Application getApplicationById(String appUUID) {
return applicationMap.get(appUUID);
}



@Override
public Subscription getSubscriptionById(String appId, String apiId) {

Expand Down Expand Up @@ -128,7 +128,6 @@ public void addApplications(List<ApplicationDto> applicationList) {
this.applicationMap = newApplicationMap;
}


public void addApplicationKeyMappings(List<ApplicationKeyMappingDTO> applicationKeyMappingList) {

Map<String, ApplicationKeyMapping> newApplicationKeyMappingMap = new ConcurrentHashMap<>();
Expand Down Expand Up @@ -167,47 +166,25 @@ public void addApplicationMappings(List<ApplicationMappingDto> applicationMappin

@Override
public ApplicationKeyMapping getMatchingApplicationKeyMapping(String applicationIdentifier, String keyType,
String securityScheme) {

for (ApplicationKeyMapping applicationKeyMapping : applicationKeyMappingMap.values()) {
boolean isApplicationIdentifierMatching = false;
boolean isSecuritySchemeMatching = false;
boolean isKeyTypeMatching = false;

if (StringUtils.isNotEmpty(applicationIdentifier)) {
if (applicationKeyMapping.getApplicationIdentifier().equals(applicationIdentifier)) {
isApplicationIdentifierMatching = true;
}
}
if (StringUtils.isNotEmpty(securityScheme)) {
if (applicationKeyMapping.getSecurityScheme().equals(securityScheme)) {
isSecuritySchemeMatching = true;
}
}
if (StringUtils.isNotEmpty(keyType)) {
if (applicationKeyMapping.getKeyType().equals(keyType)) {
isKeyTypeMatching = true;
}
}
String securityScheme, String envType) {

if (isApplicationIdentifierMatching && isSecuritySchemeMatching && isKeyTypeMatching) {
return applicationKeyMapping;
}
}
return null;
String cacheKey = SubscriptionDataStoreUtil.getApplicationKeyMappingCacheKey(applicationIdentifier, keyType,
securityScheme, envType);
return applicationKeyMappingMap.get(cacheKey);
}

@Override
public ApplicationMapping getMatchingApplicationMapping(String uuid) {
public Set<ApplicationMapping> getMatchingApplicationMappings(String uuid) {

for (ApplicationMapping applicationMapping : applicationMappingMap.values()) {
if (StringUtils.isNotEmpty(uuid)) {
Set<ApplicationMapping> applicationMappings = new HashSet<>();
if (StringUtils.isNotEmpty(uuid)) {
for (ApplicationMapping applicationMapping : applicationMappingMap.values()) {
if (applicationMapping.getApplicationUUID().equals(uuid)) {
return applicationMapping;
applicationMappings.add(applicationMapping);
}
}
}
return null;
return applicationMappings;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import feign.gson.GsonDecoder;
import feign.gson.GsonEncoder;
import feign.slf4j.Slf4jLogger;
import org.wso2.apk.enforcer.common.CacheableEntity;
import org.wso2.apk.enforcer.config.ConfigHolder;
import org.wso2.apk.enforcer.discovery.JWTIssuerDiscoveryClient;
import org.wso2.apk.enforcer.util.ApacheFeignHttpClient;
Expand Down Expand Up @@ -193,6 +194,11 @@ private static void loadSubscriptions() {
}).start();
}

public static String getApplicationKeyMappingCacheKey(String applicationIdentifier, String keyType, String securityScheme, String envType) {

return securityScheme + CacheableEntity.DELEM_PERIOD + envType +CacheableEntity.DELEM_PERIOD + keyType+ CacheableEntity.DELEM_PERIOD + applicationIdentifier;
}

public void loadStartupArtifacts(){
loadApplications();
loadSubscriptions();
Expand Down

0 comments on commit f3aeb0b

Please sign in to comment.