From 845156748c2e89581ba04053f38be19cd67c7ebe Mon Sep 17 00:00:00 2001 From: Ashera Silva Date: Sun, 24 Mar 2024 04:21:11 +0530 Subject: [PATCH] Change authentication type to api-key and add settings api changes --- .../wso2/carbon/apimgt/impl/APIConstants.java | 1 + .../carbon/apimgt/impl/utils/APIUtil.java | 22 +++++++++++++--- .../src/main/resources/devportal-api.yaml | 4 +++ .../rest/api/store/v1/dto/SettingsDTO.java | 25 +++++++++++++++++-- .../MarketplaceAssistantApiServiceImpl.java | 10 +------- .../v1/mappings/SettingsMappingUtil.java | 1 + .../src/main/resources/devportal-api.yaml | 4 +++ 7 files changed, 52 insertions(+), 15 deletions(-) diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java index 37a1b667b5b7..e0a581e2cf35 100755 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIConstants.java @@ -446,6 +446,7 @@ public final class APIConstants { public static final String DOT = "."; public static final String DEFAULT = "DEFAULT"; public static final String API_KEY_AUTH_TYPE = "API_KEY"; + public static final String API_KEY_AUTH = "API-KEY"; public static final String EXP = "exp"; public static final String JWT = "JWT"; public static final String JWT_DEFAULT_AUDIENCE = "http://org.wso2.apimgt/gateway"; diff --git a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java index 5bb0204ca529..5af3cdc95033 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java @@ -152,6 +152,7 @@ import org.wso2.carbon.apimgt.impl.IDPConfiguration; import org.wso2.carbon.apimgt.impl.PasswordResolverFactory; import org.wso2.carbon.apimgt.impl.RESTAPICacheConfiguration; +import org.wso2.carbon.apimgt.impl.ai.MarketplaceAssistantConfigurationDto; import org.wso2.carbon.apimgt.impl.caching.CacheProvider; import org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO; import org.wso2.carbon.apimgt.impl.dao.CorrelationConfigDAO; @@ -10386,6 +10387,18 @@ public static String getScopesAsString(Set scopes) { return scopesStringBuilder.toString().trim(); } + /** + * Check whether Marketplace Assistant is enabled + * + * @return returns true if Marketplace Assistant feature is enabled, false if disabled. + */ + public static boolean isMarketplaceAssistantEnabled() { + APIManagerConfiguration configuration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService() + .getAPIManagerConfiguration(); + MarketplaceAssistantConfigurationDto configDto = configuration.getMarketplaceAssistantConfigurationDto(); + return configDto.isEnabled(); + } + /** * Check whether API Chat feature is enabled * @@ -10423,7 +10436,7 @@ public static String invokeAIService(String endpointConfigName, String authToken String authToken = config.getFirstProperty(authTokenConfigName); try { HttpPost preparePost = new HttpPost(endpoint + resource); - preparePost.setHeader(HttpHeaders.AUTHORIZATION, APIConstants.AUTHORIZATION_BEARER + authToken); + preparePost.setHeader(APIConstants.API_KEY_AUTH, authToken); preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE); preparePost.setHeader("x-request-id", requestId); StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON); @@ -10462,11 +10475,12 @@ public static String invokeAIService(String endpointConfigName, String authToken * @return CloseableHttpResponse of the GET call * @throws APIManagementException */ - public static CloseableHttpResponse getMarketplaceChatApiCount(String endpoint, String resource) + public static CloseableHttpResponse getMarketplaceChatApiCount(String endpoint, String authToken, String resource) throws APIManagementException { try{ HttpGet apiCountGet = new HttpGet(endpoint + resource); + apiCountGet.setHeader(APIConstants.API_KEY_AUTH, authToken); URL url = new URL(endpoint); int port = url.getPort(); String protocol = url.getProtocol(); @@ -10484,7 +10498,7 @@ public static String MarketplaceAssistantPostService(String endpoint, String aut try { HttpPost preparePost = new HttpPost(endpoint + resource); - preparePost.setHeader(HttpHeaders.AUTHORIZATION, authToken); + preparePost.setHeader(APIConstants.API_KEY_AUTH, authToken); preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE); StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON); preparePost.setEntity(requestEntity); @@ -10520,7 +10534,7 @@ public static void DeleteApi(String endpoint, String authToken, resourceWithPathParam = resourceWithPathParam.replace("{uuid}", uuid); HttpDelete prepareDelete = new HttpDelete(resourceWithPathParam); - prepareDelete.setHeader(HttpHeaders.AUTHORIZATION, authToken); + prepareDelete.setHeader(APIConstants.API_KEY_AUTH, authToken); URL url = new URL(endpoint); int port = url.getPort(); diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/devportal-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/devportal-api.yaml index ce504bb89217..bcc658caffba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/devportal-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.common/src/main/resources/devportal-api.yaml @@ -5444,6 +5444,10 @@ components: type: boolean description: Checks if the auth token is provided for AI service usage. default: false + marketplaceAssistantEnabled: + type: boolean + description: Specifies whether Marketplace Assistant feature is enabled. + default: true ApplicationAttribute: title: Application attributes type: object diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/store/v1/dto/SettingsDTO.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/store/v1/dto/SettingsDTO.java index 342de83ed67a..2c96c6462d5e 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/store/v1/dto/SettingsDTO.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/gen/java/org/wso2/carbon/apimgt/rest/api/store/v1/dto/SettingsDTO.java @@ -41,6 +41,7 @@ public class SettingsDTO { private Integer passwordPolicyMaxLength = null; private Boolean apiChatEnabled = true; private Boolean aiAuthTokenProvided = false; + private Boolean marketplaceAssistantEnabled = true; /** **/ @@ -355,6 +356,24 @@ public void setAiAuthTokenProvided(Boolean aiAuthTokenProvided) { this.aiAuthTokenProvided = aiAuthTokenProvided; } + /** + * Specifies whether Marketplace Assistant feature is enabled. + **/ + public SettingsDTO marketplaceAssistantEnabled(Boolean marketplaceAssistantEnabled) { + this.marketplaceAssistantEnabled = marketplaceAssistantEnabled; + return this; + } + + + @ApiModelProperty(value = "Specifies whether Marketplace Assistant feature is enabled.") + @JsonProperty("marketplaceAssistantEnabled") + public Boolean isMarketplaceAssistantEnabled() { + return marketplaceAssistantEnabled; + } + public void setMarketplaceAssistantEnabled(Boolean marketplaceAssistantEnabled) { + this.marketplaceAssistantEnabled = marketplaceAssistantEnabled; + } + @Override public boolean equals(java.lang.Object o) { @@ -382,12 +401,13 @@ public boolean equals(java.lang.Object o) { Objects.equals(passwordPolicyMinLength, settings.passwordPolicyMinLength) && Objects.equals(passwordPolicyMaxLength, settings.passwordPolicyMaxLength) && Objects.equals(apiChatEnabled, settings.apiChatEnabled) && - Objects.equals(aiAuthTokenProvided, settings.aiAuthTokenProvided); + Objects.equals(aiAuthTokenProvided, settings.aiAuthTokenProvided) && + Objects.equals(marketplaceAssistantEnabled, settings.marketplaceAssistantEnabled); } @Override public int hashCode() { - return Objects.hash(grantTypes, scopes, applicationSharingEnabled, mapExistingAuthApps, apiGatewayEndpoint, monetizationEnabled, recommendationEnabled, isUnlimitedTierPaid, identityProvider, isAnonymousModeEnabled, isPasswordChangeEnabled, isJWTEnabledForLoginTokens, userStorePasswordPattern, passwordPolicyPattern, passwordPolicyMinLength, passwordPolicyMaxLength, apiChatEnabled, aiAuthTokenProvided); + return Objects.hash(grantTypes, scopes, applicationSharingEnabled, mapExistingAuthApps, apiGatewayEndpoint, monetizationEnabled, recommendationEnabled, isUnlimitedTierPaid, identityProvider, isAnonymousModeEnabled, isPasswordChangeEnabled, isJWTEnabledForLoginTokens, userStorePasswordPattern, passwordPolicyPattern, passwordPolicyMinLength, passwordPolicyMaxLength, apiChatEnabled, aiAuthTokenProvided, marketplaceAssistantEnabled); } @Override @@ -413,6 +433,7 @@ public String toString() { sb.append(" passwordPolicyMaxLength: ").append(toIndentedString(passwordPolicyMaxLength)).append("\n"); sb.append(" apiChatEnabled: ").append(toIndentedString(apiChatEnabled)).append("\n"); sb.append(" aiAuthTokenProvided: ").append(toIndentedString(aiAuthTokenProvided)).append("\n"); + sb.append(" marketplaceAssistantEnabled: ").append(toIndentedString(marketplaceAssistantEnabled)).append("\n"); sb.append("}"); return sb.toString(); } diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java index 5fbb2c74a3ca..e5e58acf60fe 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java @@ -33,29 +33,20 @@ import org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder; import org.wso2.carbon.apimgt.impl.utils.APIUtil; import org.wso2.carbon.apimgt.rest.api.store.v1.*; -import org.wso2.carbon.apimgt.rest.api.store.v1.dto.*; -import org.apache.cxf.jaxrs.ext.multipart.Attachment; import org.apache.cxf.jaxrs.ext.MessageContext; -import org.wso2.carbon.apimgt.rest.api.store.v1.dto.ErrorDTO; import org.wso2.carbon.apimgt.rest.api.store.v1.dto.MarketplaceAssistantApiCountResponseDTO; import org.wso2.carbon.apimgt.rest.api.store.v1.dto.MarketplaceAssistantRequestDTO; import org.wso2.carbon.apimgt.rest.api.store.v1.dto.MarketplaceAssistantResponseDTO; import org.wso2.carbon.apimgt.rest.api.util.utils.RestApiUtil; import java.io.IOException; -import java.util.List; - -import java.io.InputStream; import javax.ws.rs.core.Response; -import javax.ws.rs.core.SecurityContext; - public class MarketplaceAssistantApiServiceImpl implements MarketplaceAssistantApiService { - private static final Log log = LogFactory.getLog(MarketplaceAssistantApiServiceImpl.class); private static MarketplaceAssistantConfigurationDto marketplaceAssistantConfigurationDto; @@ -122,6 +113,7 @@ public Response getMarketplaceAssistantApiCount(MessageContext messageContext) t CloseableHttpResponse response = APIUtil. getMarketplaceChatApiCount(marketplaceAssistantConfigurationDto.getEndpoint(), + marketplaceAssistantConfigurationDto.getAccessToken(), marketplaceAssistantConfigurationDto.getApiCountResource()); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode == HttpStatus.SC_OK) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java index 6fcd6230f43b..781d0c1dea44 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java @@ -126,6 +126,7 @@ public SettingsDTO fromSettingstoDTO(Boolean isUserAvailable, Boolean moneatizat settingsDTO.setPasswordPolicyMinLength(passwordPolicyMinLength); settingsDTO.setPasswordPolicyMaxLength(passwordPolicyMaxLength); settingsDTO.setApiChatEnabled(APIUtil.isApiChatEnabled()); + settingsDTO.setMarketplaceAssistantEnabled(APIUtil.isMarketplaceAssistantEnabled()); settingsDTO.setAiAuthTokenProvided(APIUtil.isAuthTokenProvidedForAIFeatures()); if (isUserAvailable) { diff --git a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/resources/devportal-api.yaml b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/resources/devportal-api.yaml index ce504bb89217..bcc658caffba 100644 --- a/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/resources/devportal-api.yaml +++ b/components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/resources/devportal-api.yaml @@ -5444,6 +5444,10 @@ components: type: boolean description: Checks if the auth token is provided for AI service usage. default: false + marketplaceAssistantEnabled: + type: boolean + description: Specifies whether Marketplace Assistant feature is enabled. + default: true ApplicationAttribute: title: Application attributes type: object