Search in sources :

Example 1 with KeyManagerConnectorConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration in project carbon-apimgt by wso2.

the class AbstractKeyManager method validateOAuthAppCreationProperties.

protected void validateOAuthAppCreationProperties(OAuthApplicationInfo oAuthApplicationInfo) throws APIManagementException {
    String type = getType();
    List<String> missedRequiredValues = new ArrayList<>();
    KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = ServiceReferenceHolder.getInstance().getKeyManagerConnectorConfiguration(type);
    if (keyManagerConnectorConfiguration != null) {
        List<ConfigurationDto> applicationConfigurationDtoList = keyManagerConnectorConfiguration.getApplicationConfigurations();
        Object additionalProperties = oAuthApplicationInfo.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES);
        try {
            if (additionalProperties != null) {
                JSONObject additionalPropertiesJson;
                if (additionalProperties instanceof JSONObject) {
                    additionalPropertiesJson = (JSONObject) additionalProperties;
                } else {
                    additionalPropertiesJson = (JSONObject) new JSONParser().parse((String) additionalProperties);
                }
                for (ConfigurationDto configurationDto : applicationConfigurationDtoList) {
                    Object value = additionalPropertiesJson.get(configurationDto.getName());
                    if (value == null) {
                        if (configurationDto.isRequired()) {
                            missedRequiredValues.add(configurationDto.getName());
                        }
                    }
                }
                if (!missedRequiredValues.isEmpty()) {
                    throw new APIManagementException("Missing required properties to create/update oauth " + "application", ExceptionCodes.KEY_MANAGER_MISSING_REQUIRED_PROPERTIES_IN_APPLICATION);
                }
            }
        } catch (ParseException e) {
            throw new APIManagementException("Error while parsing the addition properties of OAuth " + "application");
        }
    } else {
        throw new APIManagementException("Invalid Key Manager Type " + type, ExceptionCodes.KEY_MANAGER_NOT_REGISTERED);
    }
}
Also used : KeyManagerConnectorConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration) ConfigurationDto(org.wso2.carbon.apimgt.api.model.ConfigurationDto) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ArrayList(java.util.ArrayList) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.simple.JSONObject) JSONParser(org.json.simple.parser.JSONParser) ParseException(org.json.simple.parser.ParseException)

Example 2 with KeyManagerConnectorConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration in project carbon-apimgt by wso2.

the class AbstractKeyManagerTestCase method buildFromJSONTest.

@Test
public void buildFromJSONTest() throws APIManagementException {
    AbstractKeyManager keyManager = new AMDefaultKeyManagerImpl();
    KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = Mockito.mock(DefaultKeyManagerConnectorConfiguration.class);
    ServiceReferenceHolder serviceReferenceHolder = PowerMockito.mock(ServiceReferenceHolder.class);
    PowerMockito.mockStatic(ServiceReferenceHolder.class);
    PowerMockito.when(ServiceReferenceHolder.getInstance()).thenReturn(serviceReferenceHolder);
    Mockito.when(serviceReferenceHolder.getKeyManagerConnectorConfiguration(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE)).thenReturn(keyManagerConnectorConfiguration);
    // test with empty json payload
    assertNotNull(keyManager.buildFromJSON(new OAuthApplicationInfo(), "{}"));
    // test with valid json
    String jsonPayload2 = "{ \"callbackUrl\": \"www.google.lk\", \"client_id\": \"XBPcXSfGK47WiEX7enchoP2Dcvga\"," + "\"client_secret\": \"4UD8VX8NaQMtrHCwqzI1tHJLPoca\", \"owner\": \"admin\", \"grantType\": \"password" + "  refresh_token\", " + "\"validityPeriod\": \"3600\" }";
    OAuthApplicationInfo oAuthApplicationInfo1 = keyManager.buildFromJSON(new OAuthApplicationInfo(), jsonPayload2);
    assertEquals("XBPcXSfGK47WiEX7enchoP2Dcvga", oAuthApplicationInfo1.getClientId());
    // test with invalid json
    try {
        keyManager.buildFromJSON(new OAuthApplicationInfo(), "{invalid}");
        assertTrue(false);
    } catch (APIManagementException e) {
        assertEquals("Error occurred while parsing JSON String", e.getMessage());
    }
    // test with invalid additionalProperties
    OAuthApplicationInfo applicationInfo = new OAuthApplicationInfo();
    applicationInfo.addParameter("additionalProperties", "{invalid}");
    try {
        keyManager.buildFromJSON(applicationInfo, "{}");
        fail();
    } catch (APIManagementException e) {
        assertEquals("Error while parsing the addition properties of OAuth application", e.getMessage());
    }
}
Also used : KeyManagerConnectorConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration) ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) Test(org.junit.Test) ModelKeyManagerForTest(org.wso2.carbon.apimgt.impl.factory.ModelKeyManagerForTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 3 with KeyManagerConnectorConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration in project carbon-apimgt by wso2.

the class AMDefaultKeyManagerImpl method validateOAuthAppCreationProperties.

@Override
protected void validateOAuthAppCreationProperties(OAuthApplicationInfo oAuthApplicationInfo) throws APIManagementException {
    super.validateOAuthAppCreationProperties(oAuthApplicationInfo);
    String type = getType();
    KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = ServiceReferenceHolder.getInstance().getKeyManagerConnectorConfiguration(type);
    if (keyManagerConnectorConfiguration != null) {
        Object additionalProperties = oAuthApplicationInfo.getParameter(APIConstants.JSON_ADDITIONAL_PROPERTIES);
        if (additionalProperties != null) {
            JsonObject additionalPropertiesJson = (JsonObject) new JsonParser().parse((String) additionalProperties);
            for (Map.Entry<String, JsonElement> entry : additionalPropertiesJson.entrySet()) {
                String additionalProperty = entry.getValue().getAsString();
                if (StringUtils.isNotBlank(additionalProperty) && !StringUtils.equals(additionalProperty, APIConstants.KeyManager.NOT_APPLICABLE_VALUE)) {
                    try {
                        if (APIConstants.KeyManager.PKCE_MANDATORY.equals(entry.getKey()) || APIConstants.KeyManager.PKCE_SUPPORT_PLAIN.equals(entry.getKey()) || APIConstants.KeyManager.BYPASS_CLIENT_CREDENTIALS.equals(entry.getKey())) {
                            if (!(additionalProperty.equalsIgnoreCase(Boolean.TRUE.toString()) || additionalProperty.equalsIgnoreCase(Boolean.FALSE.toString()))) {
                                String errMsg = "Application configuration values cannot have negative values.";
                                throw new APIManagementException(errMsg, ExceptionCodes.from(ExceptionCodes.INVALID_APPLICATION_ADDITIONAL_PROPERTIES, errMsg));
                            }
                        } else {
                            Long longValue = Long.parseLong(additionalProperty);
                            if (longValue < 0) {
                                String errMsg = "Application configuration values cannot have negative values.";
                                throw new APIManagementException(errMsg, ExceptionCodes.from(ExceptionCodes.INVALID_APPLICATION_ADDITIONAL_PROPERTIES, errMsg));
                            }
                        }
                    } catch (NumberFormatException e) {
                        String errMsg = "Application configuration values cannot have string values.";
                        throw new APIManagementException(errMsg, ExceptionCodes.from(ExceptionCodes.INVALID_APPLICATION_ADDITIONAL_PROPERTIES, errMsg));
                    }
                }
            }
        }
    }
}
Also used : KeyManagerConnectorConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) JsonElement(com.google.gson.JsonElement) JsonObject(com.google.gson.JsonObject) JsonObject(com.google.gson.JsonObject) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) JsonParser(com.google.gson.JsonParser)

Example 4 with KeyManagerConnectorConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration in project carbon-apimgt by wso2.

the class KeyManagerMappingUtil method fromKeyManagerConfigurationDto.

private static List<KeyManagerApplicationConfigurationDTO> fromKeyManagerConfigurationDto(String type) {
    List<KeyManagerApplicationConfigurationDTO> keyManagerApplicationConfigurationDTOS = new ArrayList<>();
    KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = APIUtil.getKeyManagerConnectorConfigurationsByConnectorType(type);
    if (keyManagerConnectorConfiguration != null && keyManagerConnectorConfiguration.getApplicationConfigurations() != null) {
        for (ConfigurationDto configurationDto : keyManagerConnectorConfiguration.getApplicationConfigurations()) {
            KeyManagerApplicationConfigurationDTO keyManagerApplicationConfigurationDTO = new KeyManagerApplicationConfigurationDTO();
            keyManagerApplicationConfigurationDTO.setName(configurationDto.getName());
            keyManagerApplicationConfigurationDTO.setLabel(configurationDto.getLabel());
            keyManagerApplicationConfigurationDTO.setType(configurationDto.getType());
            keyManagerApplicationConfigurationDTO.setRequired(configurationDto.isRequired());
            keyManagerApplicationConfigurationDTO.setMask(configurationDto.isMask());
            keyManagerApplicationConfigurationDTO.setMultiple(configurationDto.isMultiple());
            keyManagerApplicationConfigurationDTO.setTooltip(configurationDto.getTooltip());
            keyManagerApplicationConfigurationDTO.setDefault(configurationDto.getDefaultValue());
            keyManagerApplicationConfigurationDTO.setValues(configurationDto.getValues());
            keyManagerApplicationConfigurationDTOS.add(keyManagerApplicationConfigurationDTO);
        }
    }
    return keyManagerApplicationConfigurationDTOS;
}
Also used : KeyManagerConnectorConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration) KeyManagerApplicationConfigurationDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.KeyManagerApplicationConfigurationDTO) ConfigurationDto(org.wso2.carbon.apimgt.api.model.ConfigurationDto) ArrayList(java.util.ArrayList)

Example 5 with KeyManagerConnectorConfiguration

use of org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration in project carbon-apimgt by wso2.

the class KeyManagerHolder method addKeyManagerConfiguration.

public static void addKeyManagerConfiguration(String organization, String name, String type, KeyManagerConfiguration keyManagerConfiguration) throws APIManagementException {
    String issuer = (String) keyManagerConfiguration.getParameter(APIConstants.KeyManager.ISSUER);
    OrganizationKeyManagerDto organizationKeyManagerDto = organizationWiseMap.get(organization);
    if (organizationKeyManagerDto == null) {
        organizationKeyManagerDto = new OrganizationKeyManagerDto();
    }
    if (organizationKeyManagerDto.getKeyManagerByName(name) != null) {
        log.warn("Key Manager " + name + " already initialized in tenant " + organization);
    }
    if (keyManagerConfiguration.isEnabled() && !KeyManagerConfiguration.TokenType.EXCHANGED.equals(keyManagerConfiguration.getTokenType())) {
        KeyManager keyManager = null;
        JWTValidator jwtValidator = null;
        APIManagerConfiguration apiManagerConfiguration = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
        String defaultKeyManagerType = apiManagerConfiguration.getFirstProperty(APIConstants.DEFAULT_KEY_MANAGER_TYPE);
        KeyManagerConnectorConfiguration keyManagerConnectorConfiguration = ServiceReferenceHolder.getInstance().getKeyManagerConnectorConfiguration(type);
        if (keyManagerConnectorConfiguration != null) {
            if (StringUtils.isNotEmpty(keyManagerConnectorConfiguration.getImplementation())) {
                try {
                    keyManager = (KeyManager) Class.forName(keyManagerConnectorConfiguration.getImplementation()).newInstance();
                    keyManager.setTenantDomain(organization);
                    if (StringUtils.isNotEmpty(defaultKeyManagerType) && defaultKeyManagerType.equals(type)) {
                        keyManagerConfiguration.addParameter(APIConstants.KEY_MANAGER_USERNAME, apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_VALIDATOR_USERNAME));
                        keyManagerConfiguration.addParameter(APIConstants.KEY_MANAGER_PASSWORD, apiManagerConfiguration.getFirstProperty(APIConstants.API_KEY_VALIDATOR_PASSWORD));
                    }
                    keyManager.loadConfiguration(keyManagerConfiguration);
                } catch (ClassNotFoundException | IllegalAccessException | InstantiationException e) {
                    throw new APIManagementException("Error while loading keyManager configuration", e);
                }
            }
            jwtValidator = getJWTValidator(keyManagerConfiguration, keyManagerConnectorConfiguration.getJWTValidator());
        } else {
            if (APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE.equals(type)) {
                keyManager = new AMDefaultKeyManagerImpl();
                keyManager.setTenantDomain(organization);
                keyManager.loadConfiguration(keyManagerConfiguration);
                jwtValidator = getJWTValidator(keyManagerConfiguration, null);
            }
        }
        KeyManagerDto keyManagerDto = new KeyManagerDto();
        keyManagerDto.setName(name);
        keyManagerDto.setIssuer(issuer);
        keyManagerDto.setJwtValidator(jwtValidator);
        keyManagerDto.setKeyManager(keyManager);
        organizationKeyManagerDto.putKeyManagerDto(keyManagerDto);
        organizationWiseMap.put(organization, organizationKeyManagerDto);
    }
}
Also used : APIManagerConfiguration(org.wso2.carbon.apimgt.impl.APIManagerConfiguration) OrganizationKeyManagerDto(org.wso2.carbon.apimgt.impl.dto.OrganizationKeyManagerDto) KeyManagerDto(org.wso2.carbon.apimgt.impl.dto.KeyManagerDto) AMDefaultKeyManagerImpl(org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl) KeyManagerConnectorConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OrganizationKeyManagerDto(org.wso2.carbon.apimgt.impl.dto.OrganizationKeyManagerDto) JWTValidator(org.wso2.carbon.apimgt.impl.jwt.JWTValidator) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Aggregations

KeyManagerConnectorConfiguration (org.wso2.carbon.apimgt.api.model.KeyManagerConnectorConfiguration)8 JsonObject (com.google.gson.JsonObject)5 ConfigurationDto (org.wso2.carbon.apimgt.api.model.ConfigurationDto)5 JSONObject (org.json.simple.JSONObject)4 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)4 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 JsonElement (com.google.gson.JsonElement)1 JsonParser (com.google.gson.JsonParser)1 Map (java.util.Map)1 JSONObject (org.json.JSONObject)1 JSONParser (org.json.simple.parser.JSONParser)1 ParseException (org.json.simple.parser.ParseException)1 Test (org.junit.Test)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)1 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)1 AMDefaultKeyManagerImpl (org.wso2.carbon.apimgt.impl.AMDefaultKeyManagerImpl)1 APIManagerConfiguration (org.wso2.carbon.apimgt.impl.APIManagerConfiguration)1 KeyManagerDto (org.wso2.carbon.apimgt.impl.dto.KeyManagerDto)1