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);
}
}
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());
}
}
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));
}
}
}
}
}
}
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;
}
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);
}
}
Aggregations