Skip to content

Commit

Permalink
Change authentication type to api-key and add settings api changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ashera96 committed Mar 23, 2024
1 parent f9052ee commit 8451567
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -10386,6 +10387,18 @@ public static String getScopesAsString(Set<Scope> 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 warning on line 10399 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10396-L10399

Added lines #L10396 - L10399 were not covered by tests
}

/**
* Check whether API Chat feature is enabled
*
Expand Down Expand Up @@ -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);

Check warning on line 10439 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10439

Added line #L10439 was not covered by tests
preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
preparePost.setHeader("x-request-id", requestId);
StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON);
Expand Down Expand Up @@ -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);

Check warning on line 10483 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10483

Added line #L10483 was not covered by tests
URL url = new URL(endpoint);
int port = url.getPort();
String protocol = url.getProtocol();
Expand All @@ -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);

Check warning on line 10501 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10501

Added line #L10501 was not covered by tests
preparePost.setHeader(HttpHeaders.CONTENT_TYPE, APIConstants.APPLICATION_JSON_MEDIA_TYPE);
StringEntity requestEntity = new StringEntity(payload, ContentType.APPLICATION_JSON);
preparePost.setEntity(requestEntity);
Expand Down Expand Up @@ -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);

Check warning on line 10537 in components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/utils/APIUtil.java#L10537

Added line #L10537 was not covered by tests

URL url = new URL(endpoint);
int port = url.getPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class SettingsDTO {
private Integer passwordPolicyMaxLength = null;
private Boolean apiChatEnabled = true;
private Boolean aiAuthTokenProvided = false;
private Boolean marketplaceAssistantEnabled = true;

/**
**/
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -122,6 +113,7 @@ public Response getMarketplaceAssistantApiCount(MessageContext messageContext) t

CloseableHttpResponse response = APIUtil.
getMarketplaceChatApiCount(marketplaceAssistantConfigurationDto.getEndpoint(),
marketplaceAssistantConfigurationDto.getAccessToken(),

Check warning on line 116 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/impl/MarketplaceAssistantApiServiceImpl.java#L116

Added line #L116 was not covered by tests
marketplaceAssistantConfigurationDto.getApiCountResource());
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Check warning on line 129 in components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.rest.api.store.v1/src/main/java/org/wso2/carbon/apimgt/rest/api/store/v1/mappings/SettingsMappingUtil.java#L129

Added line #L129 was not covered by tests
settingsDTO.setAiAuthTokenProvided(APIUtil.isAuthTokenProvidedForAIFeatures());

if (isUserAvailable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8451567

Please sign in to comment.