use of org.wso2.carbon.apimgt.api.model.subscription.URLMapping in project carbon-apimgt by wso2.
the class DefaultKeyValidationHandler method validateScopes.
@Override
public boolean validateScopes(TokenValidationContext validationContext) throws APIKeyMgtException {
if (validationContext.isCacheHit()) {
return true;
}
APIKeyValidationInfoDTO apiKeyValidationInfoDTO = validationContext.getValidationInfoDTO();
if (apiKeyValidationInfoDTO == null) {
throw new APIKeyMgtException("Key Validation information not set");
}
String tenantDomain = PrivilegedCarbonContext.getThreadLocalCarbonContext().getTenantDomain();
String httpVerb = validationContext.getHttpVerb();
String[] scopes;
Set<String> scopesSet = apiKeyValidationInfoDTO.getScopes();
StringBuilder scopeList = new StringBuilder();
if (scopesSet != null && !scopesSet.isEmpty()) {
scopes = scopesSet.toArray(new String[scopesSet.size()]);
if (log.isDebugEnabled() && scopes != null) {
for (String scope : scopes) {
scopeList.append(scope);
scopeList.append(",");
}
scopeList.deleteCharAt(scopeList.length() - 1);
log.debug("Scopes allowed for token : " + validationContext.getAccessToken() + " : " + scopeList.toString());
}
}
String resourceList = validationContext.getMatchingResource();
List<String> resourceArray;
if ((APIConstants.GRAPHQL_QUERY.equalsIgnoreCase(validationContext.getHttpVerb())) || (APIConstants.GRAPHQL_MUTATION.equalsIgnoreCase(validationContext.getHttpVerb())) || (APIConstants.GRAPHQL_SUBSCRIPTION.equalsIgnoreCase(validationContext.getHttpVerb()))) {
resourceArray = new ArrayList<>(Arrays.asList(resourceList.split(",")));
} else {
resourceArray = new ArrayList<>(Arrays.asList(resourceList));
}
String actualVersion = validationContext.getVersion();
// Check if the api version has been prefixed with _default_
if (actualVersion != null && actualVersion.startsWith(APIConstants.DEFAULT_VERSION_PREFIX)) {
// Remove the prefix from the version.
actualVersion = actualVersion.split(APIConstants.DEFAULT_VERSION_PREFIX)[1];
}
SubscriptionDataStore tenantSubscriptionStore = SubscriptionDataHolder.getInstance().getTenantSubscriptionStore(tenantDomain);
API api = tenantSubscriptionStore.getApiByContextAndVersion(validationContext.getContext(), actualVersion);
boolean scopesValidated = false;
if (api != null) {
for (String resource : resourceArray) {
List<URLMapping> resources = api.getResources();
URLMapping urlMapping = null;
for (URLMapping mapping : resources) {
if (Objects.equals(mapping.getHttpMethod(), httpVerb) || "WS".equalsIgnoreCase(api.getApiType())) {
if (isResourcePathMatching(resource, mapping)) {
urlMapping = mapping;
break;
}
}
}
if (urlMapping != null) {
if (urlMapping.getScopes().size() == 0) {
scopesValidated = true;
continue;
}
List<String> mappingScopes = urlMapping.getScopes();
boolean validate = false;
for (String scope : mappingScopes) {
if (scopesSet.contains(scope)) {
scopesValidated = true;
validate = true;
break;
}
}
if (!validate && urlMapping.getScopes().size() > 0) {
scopesValidated = false;
break;
}
}
}
}
if (!scopesValidated) {
apiKeyValidationInfoDTO.setAuthorized(false);
apiKeyValidationInfoDTO.setValidationStatus(APIConstants.KeyValidationStatus.INVALID_SCOPE);
}
return scopesValidated;
}
use of org.wso2.carbon.apimgt.api.model.subscription.URLMapping in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method attachURlMappingDetailsOfApiProduct.
private void attachURlMappingDetailsOfApiProduct(Connection connection, API api) throws SQLException {
String sql = SubscriptionValidationSQLConstants.GET_ALL_API_PRODUCT_URI_TEMPLATES_SQL;
try (PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
preparedStatement.setInt(1, api.getApiId());
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String httpMethod = resultSet.getString("HTTP_METHOD");
String authScheme = resultSet.getString("AUTH_SCHEME");
String urlPattern = resultSet.getString("URL_PATTERN");
String throttlingTier = resultSet.getString("THROTTLING_TIER");
String scopeName = resultSet.getString("SCOPE_NAME");
URLMapping urlMapping = api.getResource(urlPattern, httpMethod);
if (urlMapping == null) {
urlMapping = new URLMapping();
urlMapping.setAuthScheme(authScheme);
urlMapping.setHttpMethod(httpMethod);
urlMapping.setThrottlingPolicy(throttlingTier);
urlMapping.setUrlPattern(urlPattern);
}
if (StringUtils.isNotEmpty(scopeName)) {
urlMapping.addScope(scopeName);
}
api.addResource(urlMapping);
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.URLMapping in project carbon-apimgt by wso2.
the class SubscriptionValidationDAO method attachURLMappingDetails.
private void attachURLMappingDetails(Connection connection, String revisionId, API api) throws SQLException {
try (PreparedStatement preparedStatement = connection.prepareStatement(SubscriptionValidationSQLConstants.GET_URI_TEMPLATES_BY_API_SQL)) {
preparedStatement.setInt(1, api.getApiId());
preparedStatement.setString(2, revisionId);
try (ResultSet resultSet = preparedStatement.executeQuery()) {
while (resultSet.next()) {
String httpMethod = resultSet.getString("HTTP_METHOD");
String authScheme = resultSet.getString("AUTH_SCHEME");
String urlPattern = resultSet.getString("URL_PATTERN");
String throttlingTier = resultSet.getString("THROTTLING_TIER");
String scopeName = resultSet.getString("SCOPE_NAME");
URLMapping urlMapping = api.getResource(urlPattern, httpMethod);
if (urlMapping == null) {
urlMapping = new URLMapping();
urlMapping.setAuthScheme(authScheme);
urlMapping.setHttpMethod(httpMethod);
urlMapping.setThrottlingPolicy(throttlingTier);
urlMapping.setUrlPattern(urlPattern);
}
if (StringUtils.isNotEmpty(scopeName)) {
urlMapping.addScope(scopeName);
}
api.addResource(urlMapping);
}
}
}
}
use of org.wso2.carbon.apimgt.api.model.subscription.URLMapping in project carbon-apimgt by wso2.
the class GatewayUtils method convertUriTemplate.
private static List<URLMappingDTO> convertUriTemplate(List<URLMapping> resources) {
List<URLMappingDTO> urlMappingDTOList = new ArrayList<>();
for (URLMapping resource : resources) {
URLMappingDTO urlMappingDTO = new URLMappingDTO().urlPattern(resource.getUrlPattern()).authScheme(resource.getAuthScheme()).httpMethod(resource.getHttpMethod()).throttlingPolicy(resource.getThrottlingPolicy()).scopes(resource.getScopes());
urlMappingDTOList.add(urlMappingDTO);
}
return urlMappingDTOList;
}
use of org.wso2.carbon.apimgt.api.model.subscription.URLMapping in project carbon-apimgt by wso2.
the class CacheInvalidationServiceImpl method invalidateResourceCache.
public void invalidateResourceCache(String context, String version, String organization, List<URLMapping> urlMappings) {
boolean isTenantFlowStarted = false;
try {
isTenantFlowStarted = startTenantFlow(organization);
Cache cache = CacheProvider.getResourceCache();
String apiCacheKey = APIUtil.getAPIInfoDTOCacheKey(context, version);
if (cache.containsKey(apiCacheKey)) {
cache.remove(apiCacheKey);
}
for (URLMapping uriTemplate : urlMappings) {
String resourceVerbCacheKey = APIUtil.getResourceInfoDTOCacheKey(context, version, uriTemplate.getUrlPattern(), uriTemplate.getHttpMethod());
if (cache.containsKey(resourceVerbCacheKey)) {
cache.remove(resourceVerbCacheKey);
}
}
} finally {
if (isTenantFlowStarted) {
endTenantFlow();
}
}
}
Aggregations