Skip to content

Commit 120085d

Browse files
authored
Merge pull request #12742 from nimsara66/3486
Fix re-enabling OAuth2 security on APIs configured with only mutual SSL
2 parents 8f3935b + fd2dbb0 commit 120085d

File tree

1 file changed

+66
-3
lines changed
  • components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings

1 file changed

+66
-3
lines changed

components/apimgt/org.wso2.carbon.apimgt.rest.api.publisher.v1.common/src/main/java/org/wso2/carbon/apimgt/rest/api/publisher/v1/common/mappings/PublisherCommonUtils.java

+66-3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,9 @@
153153
import java.util.regex.Matcher;
154154
import java.util.regex.Pattern;
155155

156+
import static org.wso2.carbon.apimgt.api.model.policy.PolicyConstants.AI_API_QUOTA_TYPE;
157+
import static org.wso2.carbon.apimgt.api.model.policy.PolicyConstants.EVENT_COUNT_TYPE;
158+
156159
import static org.wso2.carbon.apimgt.impl.APIConstants.GOVERNANCE_COMPLIANCE_ERROR_MESSAGE;
157160
import static org.wso2.carbon.apimgt.impl.APIConstants.GOVERNANCE_COMPLIANCE_KEY;
158161
import static org.wso2.carbon.apimgt.impl.APIConstants.PUBLISH;
@@ -364,6 +367,7 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
364367
|| APIConstants.APITransportType.WEBSUB.toString().equals(originalAPI.getType())
365368
|| APIConstants.APITransportType.SSE.toString().equals(originalAPI.getType())
366369
|| APIConstants.APITransportType.ASYNC.toString().equals(originalAPI.getType()));
370+
boolean isAIAPI = APIConstants.API_SUBTYPE_AI_API.equals(originalAPI.getSubtype());
367371

368372
Scope[] apiDtoClassAnnotatedScopes = APIDTO.class.getAnnotationsByType(Scope.class);
369373
boolean hasClassLevelScope = checkClassScopeAnnotation(apiDtoClassAnnotatedScopes, tokenScopes);
@@ -525,9 +529,20 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
525529
if (!APIUtil.isSubscriptionValidationDisablingAllowed(tenantDomain)) {
526530
if (apiSecurity != null && (apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2) || apiSecurity
527531
.contains(APIConstants.API_SECURITY_API_KEY)) && condition) {
528-
throw new APIManagementException(
529-
"A tier should be defined if the API is not in CREATED or PROTOTYPED state",
530-
ExceptionCodes.TIER_CANNOT_BE_NULL);
532+
Set<Tier> availableThrottlingPolicyList = apiProvider.getTiers();
533+
tiersFromDTO = availableThrottlingPolicyList.stream()
534+
.filter(tier -> isApplicableTier(tier, isAsyncAPI, isAIAPI))
535+
.map(Tier::getName)
536+
.findFirst()
537+
.map(Collections::singletonList)
538+
.orElse(Collections.emptyList());
539+
apiDtoToUpdate.setPolicies(tiersFromDTO);
540+
541+
if (tiersFromDTO.isEmpty()) {
542+
throw new APIManagementException(
543+
"A tier should be defined if the API is not in CREATED or PROTOTYPED state",
544+
ExceptionCodes.TIER_CANNOT_BE_NULL);
545+
}
531546
}
532547
} else {
533548
if (apiSecurity != null) {
@@ -722,6 +737,54 @@ private static API prepareForUpdateApi(API originalAPI, APIDTO apiDtoToUpdate, A
722737
return apiToUpdate;
723738
}
724739

740+
private static boolean isApplicableTier(Tier tier, boolean isAsyncAPI, boolean isAIAPI) {
741+
if (isAsyncAPI) {
742+
return isAsyncAPITier(tier);
743+
}
744+
745+
if (isAIAPI) {
746+
return isAIAPITier(tier);
747+
}
748+
749+
return isRegularAPITier(tier);
750+
}
751+
752+
/**
753+
* Checks if the given tier is an Async API tier.
754+
*
755+
* @param tier The tier to evaluate.
756+
* @return {@code true} if the tier is of type EVENT_COUNT_TYPE, otherwise {@code false}.
757+
*/
758+
private static boolean isAsyncAPITier(Tier tier) {
759+
return EVENT_COUNT_TYPE.equals(tier.getQuotaPolicyType());
760+
}
761+
762+
/**
763+
* Checks if the given tier is an AI API tier.
764+
*
765+
* @param tier The tier to evaluate.
766+
* @return {@code true} if the tier is of type AI_API_QUOTA_TYPE,
767+
* contains the default subscription-less policy name,
768+
* or has a null quota policy type. Otherwise, returns {@code false}.
769+
*/
770+
private static boolean isAIAPITier(Tier tier) {
771+
return AI_API_QUOTA_TYPE.equals(tier.getQuotaPolicyType()) ||
772+
tier.getName().contains(APIConstants.DEFAULT_SUB_POLICY_SUBSCRIPTIONLESS) ||
773+
tier.getQuotaPolicyType() == null;
774+
}
775+
776+
/**
777+
* Checks if the given tier is a regular API tier.
778+
*
779+
* @param tier The tier to evaluate.
780+
* @return {@code true} if the tier is neither an AI API tier nor an Async API tier,
781+
* otherwise {@code false}.
782+
*/
783+
private static boolean isRegularAPITier(Tier tier) {
784+
return !AI_API_QUOTA_TYPE.equals(tier.getQuotaPolicyType()) &&
785+
!EVENT_COUNT_TYPE.equals(tier.getQuotaPolicyType());
786+
}
787+
725788
/**
726789
* This method will encrypt the Api Key
727790
*

0 commit comments

Comments
 (0)