use of org.wso2.carbon.apimgt.internal.service.dto.KeyManagerDTO in project carbon-apimgt by wso2.
the class KeyManagerMappingUtil method fromOpenIdConnectConfigurationToKeyManagerConfiguration.
public static KeyManagerWellKnownResponseDTO fromOpenIdConnectConfigurationToKeyManagerConfiguration(OpenIdConnectConfiguration openIdConnectConfiguration) {
KeyManagerWellKnownResponseDTO keyManagerWellKnownResponseDTO = new KeyManagerWellKnownResponseDTO();
if (openIdConnectConfiguration != null) {
keyManagerWellKnownResponseDTO.setValid(true);
KeyManagerDTO keyManagerDto = new KeyManagerDTO();
keyManagerDto.setIssuer(openIdConnectConfiguration.getIssuer());
keyManagerDto.setIntrospectionEndpoint(openIdConnectConfiguration.getIntrospectionEndpoint());
keyManagerDto.setClientRegistrationEndpoint(openIdConnectConfiguration.getRegistrationEndpoint());
keyManagerDto.setAuthorizeEndpoint(openIdConnectConfiguration.getAuthorizeEndpoint());
keyManagerDto.setTokenEndpoint(openIdConnectConfiguration.getTokenEndpoint());
keyManagerDto.setRevokeEndpoint(openIdConnectConfiguration.getRevokeEndpoint());
keyManagerDto.setEnabled(true);
keyManagerDto.setEnableTokenGeneration(true);
keyManagerDto.setEnableMapOAuthConsumerApps(true);
keyManagerDto.setEnableOAuthAppCreation(true);
keyManagerDto.setEnableSelfValidationJWT(true);
keyManagerDto.setAvailableGrantTypes(openIdConnectConfiguration.getGrantTypesSupported());
if (StringUtils.isNotEmpty(openIdConnectConfiguration.getJwksEndpoint())) {
KeyManagerCertificatesDTO keyManagerCertificatesDTO = new KeyManagerCertificatesDTO();
keyManagerCertificatesDTO.setType(KeyManagerCertificatesDTO.TypeEnum.JWKS);
keyManagerCertificatesDTO.setValue(openIdConnectConfiguration.getJwksEndpoint());
keyManagerDto.setCertificates(keyManagerCertificatesDTO);
}
keyManagerWellKnownResponseDTO.setValue(keyManagerDto);
}
return keyManagerWellKnownResponseDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.KeyManagerDTO in project carbon-apimgt by wso2.
the class KeyManagerMappingUtil method toKeyManagerDTO.
public static KeyManagerDTO toKeyManagerDTO(KeyManagerConfigurationDTO keyManagerConfigurationDTO) {
KeyManagerDTO keyManagerDTO = new KeyManagerDTO();
keyManagerDTO.setId(keyManagerConfigurationDTO.getUuid());
keyManagerDTO.setName(keyManagerConfigurationDTO.getName());
keyManagerDTO.setDisplayName(keyManagerConfigurationDTO.getDisplayName());
keyManagerDTO.setDescription(keyManagerConfigurationDTO.getDescription());
keyManagerDTO.setType(keyManagerConfigurationDTO.getType());
keyManagerDTO.setEnabled(keyManagerConfigurationDTO.isEnabled());
keyManagerDTO.setTokenType(KeyManagerDTO.TokenTypeEnum.valueOf(keyManagerConfigurationDTO.getTokenType()));
keyManagerDTO.setAlias(keyManagerConfigurationDTO.getAlias());
keyManagerDTO.setTokenType(KeyManagerDTO.TokenTypeEnum.fromValue(keyManagerConfigurationDTO.getTokenType()));
JsonObject jsonObject = fromConfigurationMapToJson(keyManagerConfigurationDTO.getAdditionalProperties());
JsonElement clientRegistrationElement = jsonObject.get(APIConstants.KeyManager.CLIENT_REGISTRATION_ENDPOINT);
if (clientRegistrationElement != null) {
keyManagerDTO.setClientRegistrationEndpoint(clientRegistrationElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.CLIENT_REGISTRATION_ENDPOINT);
}
JsonElement introspectionElement = jsonObject.get(APIConstants.KeyManager.INTROSPECTION_ENDPOINT);
if (introspectionElement != null) {
keyManagerDTO.setIntrospectionEndpoint(introspectionElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.INTROSPECTION_ENDPOINT);
}
JsonElement tokenEndpointElement = jsonObject.get(APIConstants.KeyManager.TOKEN_ENDPOINT);
if (tokenEndpointElement != null) {
keyManagerDTO.setTokenEndpoint(tokenEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.TOKEN_ENDPOINT);
}
JsonElement displayTokenEndpointElement = jsonObject.get(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT);
if (displayTokenEndpointElement != null && !displayTokenEndpointElement.getAsString().trim().isEmpty()) {
keyManagerDTO.setDisplayTokenEndpoint(displayTokenEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.DISPLAY_TOKEN_ENDPOINT);
}
JsonElement revokeEndpointElement = jsonObject.get(APIConstants.KeyManager.REVOKE_ENDPOINT);
if (revokeEndpointElement != null) {
keyManagerDTO.setRevokeEndpoint(revokeEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.REVOKE_ENDPOINT);
}
JsonElement displayRevokeEndpointElement = jsonObject.get(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT);
if (displayRevokeEndpointElement != null && !displayRevokeEndpointElement.getAsString().trim().isEmpty()) {
keyManagerDTO.setDisplayRevokeEndpoint(displayRevokeEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.DISPLAY_REVOKE_ENDPOINT);
}
JsonElement scopeEndpointElement = jsonObject.get(APIConstants.KeyManager.SCOPE_MANAGEMENT_ENDPOINT);
if (scopeEndpointElement != null) {
keyManagerDTO.setScopeManagementEndpoint(scopeEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.SCOPE_MANAGEMENT_ENDPOINT);
}
JsonElement grantTypesElement = jsonObject.get(APIConstants.KeyManager.AVAILABLE_GRANT_TYPE);
if (grantTypesElement instanceof JsonArray) {
keyManagerDTO.setAvailableGrantTypes(new Gson().fromJson(grantTypesElement, List.class));
jsonObject.remove(APIConstants.KeyManager.AVAILABLE_GRANT_TYPE);
}
JsonElement issuerElement = jsonObject.get(APIConstants.KeyManager.ISSUER);
if (issuerElement != null) {
keyManagerDTO.setIssuer(issuerElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.ISSUER);
}
JsonElement wellKnownEndpointElement = jsonObject.get(APIConstants.KeyManager.WELL_KNOWN_ENDPOINT);
if (wellKnownEndpointElement != null) {
keyManagerDTO.setWellKnownEndpoint(wellKnownEndpointElement.getAsString());
jsonObject.remove(APIConstants.KeyManager.WELL_KNOWN_ENDPOINT);
}
JsonElement certificateValueElement = jsonObject.get(APIConstants.KeyManager.CERTIFICATE_VALUE);
JsonElement certificateTypeElement = jsonObject.get(APIConstants.KeyManager.CERTIFICATE_TYPE);
if (certificateTypeElement != null && certificateValueElement != null) {
KeyManagerCertificatesDTO keyManagerCertificatesDTO = new KeyManagerCertificatesDTO();
keyManagerCertificatesDTO.setValue(certificateValueElement.getAsString());
if (APIConstants.KeyManager.CERTIFICATE_TYPE_JWKS_ENDPOINT.equals(certificateTypeElement.getAsString())) {
keyManagerCertificatesDTO.setType(KeyManagerCertificatesDTO.TypeEnum.JWKS);
} else if (APIConstants.KeyManager.CERTIFICATE_TYPE_PEM_FILE.equals(certificateTypeElement.getAsString())) {
keyManagerCertificatesDTO.setType(KeyManagerCertificatesDTO.TypeEnum.PEM);
}
keyManagerDTO.setCertificates(keyManagerCertificatesDTO);
jsonObject.remove(APIConstants.KeyManager.CERTIFICATE_VALUE);
jsonObject.remove(APIConstants.KeyManager.CERTIFICATE_TYPE);
}
JsonElement userInfoEndpoint = jsonObject.get(APIConstants.KeyManager.USERINFO_ENDPOINT);
if (userInfoEndpoint != null) {
keyManagerDTO.setUserInfoEndpoint(userInfoEndpoint.getAsString());
jsonObject.remove(APIConstants.KeyManager.USERINFO_ENDPOINT);
}
JsonElement authorizeEndpoint = jsonObject.get(APIConstants.KeyManager.AUTHORIZE_ENDPOINT);
if (authorizeEndpoint != null) {
keyManagerDTO.setAuthorizeEndpoint(authorizeEndpoint.getAsString());
jsonObject.remove(APIConstants.KeyManager.AUTHORIZE_ENDPOINT);
}
JsonElement enableOauthAppCreation = jsonObject.get(APIConstants.KeyManager.ENABLE_OAUTH_APP_CREATION);
if (enableOauthAppCreation != null) {
keyManagerDTO.setEnableOAuthAppCreation(enableOauthAppCreation.getAsBoolean());
jsonObject.remove(APIConstants.KeyManager.ENABLE_OAUTH_APP_CREATION);
}
JsonElement enableMapOauthConsumerApps = jsonObject.get(APIConstants.KeyManager.ENABLE_MAP_OAUTH_CONSUMER_APPS);
if (enableMapOauthConsumerApps != null) {
keyManagerDTO.setEnableMapOAuthConsumerApps(enableMapOauthConsumerApps.getAsBoolean());
jsonObject.remove(APIConstants.KeyManager.ENABLE_MAP_OAUTH_CONSUMER_APPS);
}
JsonElement enableTokenEncryption = jsonObject.get(APIConstants.KeyManager.ENABLE_TOKEN_ENCRYPTION);
if (enableTokenEncryption != null) {
keyManagerDTO.setEnableTokenEncryption(enableTokenEncryption.getAsBoolean());
jsonObject.remove(APIConstants.KeyManager.ENABLE_TOKEN_ENCRYPTION);
}
JsonElement enableTokenHHashing = jsonObject.get(APIConstants.KeyManager.ENABLE_TOKEN_HASH);
if (enableTokenEncryption != null) {
keyManagerDTO.setEnableTokenHashing(enableTokenHHashing.getAsBoolean());
jsonObject.remove(APIConstants.KeyManager.ENABLE_TOKEN_HASH);
}
JsonElement enableTokenGeneration = jsonObject.get(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION);
if (enableTokenGeneration != null) {
keyManagerDTO.setEnableTokenGeneration(enableTokenGeneration.getAsBoolean());
jsonObject.remove(APIConstants.KeyManager.ENABLE_TOKEN_GENERATION);
}
JsonElement selfValidateJWTElement = jsonObject.get(APIConstants.KeyManager.SELF_VALIDATE_JWT);
JsonElement validationValueElement = jsonObject.get(APIConstants.KeyManager.TOKEN_FORMAT_STRING);
if (validationValueElement instanceof JsonPrimitive) {
keyManagerDTO.setTokenValidation(Arrays.asList(new Gson().fromJson(validationValueElement.getAsString(), TokenValidationDTO[].class)));
jsonObject.remove(APIConstants.KeyManager.TOKEN_FORMAT_STRING);
}
if (selfValidateJWTElement != null) {
keyManagerDTO.setEnableSelfValidationJWT(selfValidateJWTElement.getAsBoolean());
}
JsonElement claimMappingElement = jsonObject.get(APIConstants.KeyManager.CLAIM_MAPPING);
if (claimMappingElement != null) {
keyManagerDTO.setClaimMapping(Arrays.asList(new Gson().fromJson(claimMappingElement, ClaimMappingEntryDTO[].class)));
jsonObject.remove(APIConstants.KeyManager.CLAIM_MAPPING);
}
JsonElement scopeClaimKey = jsonObject.get(APIConstants.KeyManager.SCOPES_CLAIM);
if (scopeClaimKey != null) {
keyManagerDTO.setScopesClaim(scopeClaimKey.getAsString());
jsonObject.remove(APIConstants.KeyManager.SCOPES_CLAIM);
}
JsonElement consumerKeyClaim = jsonObject.get(APIConstants.KeyManager.CONSUMER_KEY_CLAIM);
if (scopeClaimKey != null) {
keyManagerDTO.setConsumerKeyClaim(consumerKeyClaim.getAsString());
jsonObject.remove(APIConstants.KeyManager.CONSUMER_KEY_CLAIM);
}
keyManagerDTO.setAdditionalProperties(new Gson().fromJson(jsonObject, Map.class));
return keyManagerDTO;
}
use of org.wso2.carbon.apimgt.internal.service.dto.KeyManagerDTO in project carbon-apimgt by wso2.
the class KeyManagersApiServiceImpl method keyManagersKeyManagerIdPut.
public Response keyManagersKeyManagerIdPut(String keyManagerId, KeyManagerDTO body, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getOrganization(messageContext);
APIAdmin apiAdmin = new APIAdminImpl();
try {
KeyManagerConfigurationDTO keyManagerConfigurationDTO = KeyManagerMappingUtil.toKeyManagerConfigurationDTO(organization, body);
keyManagerConfigurationDTO.setUuid(keyManagerId);
KeyManagerConfigurationDTO oldKeyManagerConfigurationDTO = apiAdmin.getKeyManagerConfigurationById(organization, keyManagerId);
if (oldKeyManagerConfigurationDTO == null) {
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
} else {
if (!oldKeyManagerConfigurationDTO.getName().equals(keyManagerConfigurationDTO.getName())) {
RestApiUtil.handleBadRequest("Key Manager name couldn't able to change", log);
}
KeyManagerConfigurationDTO retrievedKeyManagerConfigurationDTO = apiAdmin.updateKeyManagerConfiguration(keyManagerConfigurationDTO);
APIUtil.logAuditMessage(APIConstants.AuditLogConstants.KEY_MANAGER, new Gson().toJson(keyManagerConfigurationDTO), APIConstants.AuditLogConstants.UPDATED, RestApiCommonUtil.getLoggedInUsername());
return Response.ok(KeyManagerMappingUtil.toKeyManagerDTO(retrievedKeyManagerConfigurationDTO)).build();
}
} catch (APIManagementException e) {
String error = "Error while Retrieving Key Manager configuration for " + keyManagerId + " in organization " + organization;
throw new APIManagementException(error, e, ExceptionCodes.INTERNAL_ERROR);
}
}
use of org.wso2.carbon.apimgt.internal.service.dto.KeyManagerDTO in project carbon-apimgt by wso2.
the class KeyManagersApiServiceImpl method keyManagersKeyManagerIdGet.
public Response keyManagersKeyManagerIdGet(String keyManagerId, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getOrganization(messageContext);
APIAdmin apiAdmin = new APIAdminImpl();
KeyManagerConfigurationDTO keyManagerConfigurationDTO = apiAdmin.getKeyManagerConfigurationById(organization, keyManagerId);
if (keyManagerConfigurationDTO != null) {
KeyManagerDTO keyManagerDTO = KeyManagerMappingUtil.toKeyManagerDTO(keyManagerConfigurationDTO);
return Response.ok(keyManagerDTO).build();
}
throw new APIManagementException("Requested KeyManager not found", ExceptionCodes.KEY_MANAGER_NOT_FOUND);
}
use of org.wso2.carbon.apimgt.internal.service.dto.KeyManagerDTO in project carbon-apimgt by wso2.
the class APIProviderImplTest method init.
@Before
public void init() throws Exception {
System.setProperty("carbon.home", APIProviderImplTest.class.getResource("/").getFile());
PowerMockito.mockStatic(ApiMgtDAO.class);
PowerMockito.mockStatic(GatewayArtifactsMgtDAO.class);
PowerMockito.mockStatic(ScopesDAO.class);
PowerMockito.mockStatic(PrivilegedCarbonContext.class);
PowerMockito.mockStatic(RegistryUtils.class);
PowerMockito.mockStatic(GovernanceUtils.class);
PowerMockito.mockStatic(WorkflowExecutorFactory.class);
PowerMockito.mockStatic(LifecycleBeanPopulator.class);
PowerMockito.mockStatic(KeyManagerHolder.class);
PowerMockito.mockStatic(Caching.class);
PowerMockito.mockStatic(PaginationContext.class);
PowerMockito.mockStatic(APIUtil.class);
PowerMockito.mockStatic(APIGatewayManager.class);
PowerMockito.mockStatic(CertificateManagerImpl.class);
PowerMockito.mockStatic(RegistryPersistenceUtil.class);
apimgtDAO = Mockito.mock(ApiMgtDAO.class);
gatewayArtifactsMgtDAO = Mockito.mock(GatewayArtifactsMgtDAO.class);
scopesDAO = Mockito.mock(ScopesDAO.class);
keyManager = Mockito.mock(KeyManager.class);
apiPersistenceInstance = Mockito.mock(APIPersistence.class);
certificateManager = Mockito.mock(CertificateManagerImpl.class);
Mockito.when(keyManager.getResourceByApiId(Mockito.anyString())).thenReturn(null);
Mockito.when(keyManager.registerNewResource(Mockito.any(API.class), Mockito.any(Map.class))).thenReturn(true);
KeyManagerDto keyManagerDto = new KeyManagerDto();
keyManagerDto.setName("default");
keyManagerDto.setKeyManager(keyManager);
keyManagerDto.setIssuer("https://localhost");
Map<String, KeyManagerDto> tenantKeyManagerDtoMap = new HashMap<>();
tenantKeyManagerDtoMap.put("default", keyManagerDto);
PowerMockito.when(KeyManagerHolder.getTenantKeyManagers("carbon.super")).thenReturn(tenantKeyManagerDtoMap);
PowerMockito.when(CertificateManagerImpl.getInstance()).thenReturn(certificateManager);
PowerMockito.when(APIUtil.isAPIManagementEnabled()).thenReturn(false);
PowerMockito.when(APIUtil.replaceEmailDomainBack(Mockito.anyString())).thenReturn("admin");
Mockito.when(APIUtil.replaceEmailDomain(Mockito.anyString())).thenReturn("admin");
PrivilegedCarbonContext prcontext = Mockito.mock(PrivilegedCarbonContext.class);
PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(prcontext);
PowerMockito.doNothing().when(prcontext).setUsername(Mockito.anyString());
PowerMockito.doNothing().when(prcontext).setTenantDomain(Mockito.anyString(), Mockito.anyBoolean());
artifactManager = Mockito.mock(GenericArtifactManager.class);
registry = Mockito.mock(Registry.class);
PowerMockito.when(APIUtil.getArtifactManager(any(Registry.class), Mockito.anyString())).thenReturn(artifactManager);
artifact = Mockito.mock(GenericArtifact.class);
gatewayManager = Mockito.mock(APIGatewayManager.class);
Mockito.when(APIGatewayManager.getInstance()).thenReturn(gatewayManager);
TestUtils.mockRegistryAndUserRealm(-1234);
TestUtils.mockAPICacheClearence();
TestUtils.mockAPIMConfiguration();
mockDocumentationCreation();
config = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = new APIManagerConfigurationServiceImpl(config);
ServiceReferenceHolder.getInstance().setAPIManagerConfigurationService(apiManagerConfigurationService);
APIManagerConfiguration config = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService().getAPIManagerConfiguration();
GatewayArtifactSynchronizerProperties synchronizerProperties = new GatewayArtifactSynchronizerProperties();
Mockito.when(config.getGatewayArtifactSynchronizerProperties()).thenReturn(synchronizerProperties);
Mockito.when(config.getApiRecommendationEnvironment()).thenReturn(null);
PowerMockito.when(APIUtil.replaceSystemProperty(Mockito.anyString())).thenAnswer((Answer<String>) invocation -> {
Object[] args = invocation.getArguments();
return (String) args[0];
});
TestUtils.initConfigurationContextService(true);
superTenantDomain = "carbon.super";
}
Aggregations