use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class ApiMgtDAO method addKeyManagerConfiguration.
public void addKeyManagerConfiguration(KeyManagerConfigurationDTO keyManagerConfigurationDTO) throws APIManagementException {
try (Connection conn = APIMgtDBUtil.getConnection()) {
conn.setAutoCommit(false);
try (PreparedStatement preparedStatement = conn.prepareStatement(SQLConstants.KeyManagerSqlConstants.ADD_KEY_MANAGER)) {
preparedStatement.setString(1, keyManagerConfigurationDTO.getUuid());
preparedStatement.setString(2, keyManagerConfigurationDTO.getName());
preparedStatement.setString(3, keyManagerConfigurationDTO.getDescription());
preparedStatement.setString(4, keyManagerConfigurationDTO.getType());
String configurationJson = new Gson().toJson(keyManagerConfigurationDTO.getAdditionalProperties());
preparedStatement.setBinaryStream(5, new ByteArrayInputStream(configurationJson.getBytes()));
preparedStatement.setString(6, keyManagerConfigurationDTO.getOrganization());
preparedStatement.setBoolean(7, keyManagerConfigurationDTO.isEnabled());
preparedStatement.setString(8, keyManagerConfigurationDTO.getDisplayName());
preparedStatement.setString(9, keyManagerConfigurationDTO.getTokenType());
preparedStatement.setString(10, keyManagerConfigurationDTO.getExternalReferenceId());
preparedStatement.executeUpdate();
conn.commit();
} catch (SQLException e) {
conn.rollback();
if (e instanceof SQLIntegrityConstraintViolationException) {
if (getKeyManagerConfigurationByName(conn, keyManagerConfigurationDTO.getOrganization(), keyManagerConfigurationDTO.getName()) != null) {
log.warn(keyManagerConfigurationDTO.getName() + " Key Manager Already Registered in tenant" + keyManagerConfigurationDTO.getOrganization());
} else {
throw new APIManagementException("Error while Storing key manager configuration with name " + keyManagerConfigurationDTO.getName() + " in tenant " + keyManagerConfigurationDTO.getOrganization(), e);
}
}
}
} catch (SQLException | IOException e) {
throw new APIManagementException("Error while Storing key manager configuration with name " + keyManagerConfigurationDTO.getName() + " in tenant " + keyManagerConfigurationDTO.getOrganization(), e);
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class KeyMgtRegistrationService method registerDefaultKeyManager.
public static void registerDefaultKeyManager(String organization) throws APIManagementException {
synchronized (KeyMgtRegistrationService.class.getName().concat(organization)) {
ApiMgtDAO instance = ApiMgtDAO.getInstance();
if (instance.getKeyManagerConfigurationByName(organization, APIConstants.KeyManager.DEFAULT_KEY_MANAGER) == null) {
APIManagerConfigurationService apiManagerConfigurationService = ServiceReferenceHolder.getInstance().getAPIManagerConfigurationService();
KeyManagerConfigurationDTO keyManagerConfigurationDTO = new KeyManagerConfigurationDTO();
keyManagerConfigurationDTO.setName(APIConstants.KeyManager.DEFAULT_KEY_MANAGER);
keyManagerConfigurationDTO.setEnabled(true);
keyManagerConfigurationDTO.setUuid(UUID.randomUUID().toString());
keyManagerConfigurationDTO.setOrganization(organization);
keyManagerConfigurationDTO.setDescription(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_DESCRIPTION);
keyManagerConfigurationDTO.setTokenType(KeyManagerConfiguration.TokenType.DIRECT.toString());
if (apiManagerConfigurationService != null && apiManagerConfigurationService.getAPIManagerConfiguration() != null) {
String defaultKeyManagerType = apiManagerConfigurationService.getAPIManagerConfiguration().getFirstProperty(APIConstants.DEFAULT_KEY_MANAGER_TYPE);
if (StringUtils.isNotEmpty(defaultKeyManagerType)) {
keyManagerConfigurationDTO.setType(defaultKeyManagerType);
} else {
keyManagerConfigurationDTO.setType(APIConstants.KeyManager.DEFAULT_KEY_MANAGER_TYPE);
}
}
TokenHandlingDto tokenHandlingDto = new TokenHandlingDto();
tokenHandlingDto.setEnable(true);
tokenHandlingDto.setType(TokenHandlingDto.TypeEnum.REFERENCE);
tokenHandlingDto.setValue(APIConstants.KeyManager.UUID_REGEX);
keyManagerConfigurationDTO.addProperty(APIConstants.KeyManager.TOKEN_FORMAT_STRING, new Gson().toJson(Arrays.asList(tokenHandlingDto)));
instance.addKeyManagerConfiguration(keyManagerConfigurationDTO);
}
}
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testRequestApprovalForApplicationRegistration.
@Test
public void testRequestApprovalForApplicationRegistration() throws APIManagementException, UserStoreException {
Scope scope1 = new Scope();
scope1.setName("api_view");
Scope scope2 = new Scope();
scope2.setName("api_create");
Set<Scope> scopes = new HashSet<Scope>();
scopes.add(scope1);
scopes.add(scope2);
PowerMockito.when(MultitenantUtils.getTenantDomain(Mockito.anyString())).thenReturn("abc.org");
KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
keyManagerConfigurationsDto.setEnabled(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("abc.org", "default")).thenReturn(keyManagerConfigurationsDto);
Mockito.when(tenantManager.getTenantId(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(-1234, 1);
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
Application app = new Application("app1", new Subscriber("1"));
app.setGroupId("2");
app.setUUID(UUID.randomUUID().toString());
Mockito.when(userStoreManager.getRoleListOfUser(Mockito.anyString())).thenThrow(UserStoreException.class).thenReturn(new String[] { "role1", "role2" });
Application application = Mockito.mock(Application.class);
Subscriber subscriber = Mockito.mock(Subscriber.class);
Mockito.when(subscriber.getName()).thenReturn("1");
Mockito.when(application.getSubscriber()).thenReturn(subscriber);
Mockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
try {
apiConsumer.requestApprovalForApplicationRegistration("1", app, "access", "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
Assert.fail("API management exception not thrown for invalid token type");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Invalid Token Type"));
}
scope1.setRoles("role1");
scope2.setRoles("role2");
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
application = new Application("app1", new Subscriber("1"));
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
BDDMockito.when(ApplicationUtils.retrieveApplication(Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(application);
Map<String, Object> result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_PRODUCTION, "identity.com/auth", null, "3600", "api_view", null, "default", null, false);
Assert.assertEquals(result.size(), 10);
Assert.assertEquals(result.get("keyState"), "APPROVED");
result = apiConsumer.requestApprovalForApplicationRegistration("1", app, APIConstants.API_KEY_TYPE_SANDBOX, "", null, "3600", "api_view", null, "default", null, false);
Assert.assertEquals(result.size(), 10);
Assert.assertEquals(result.get("keyState"), "APPROVED");
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testMapExistingOAuthClient.
@Test
public void testMapExistingOAuthClient() throws APIManagementException {
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
apiConsumer.tenantDomain = "carbon.super";
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
Mockito.when(apiMgtDAO.isKeyMappingExistsForConsumerKeyOrApplication(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(true, false);
Mockito.when(keyManager.mapOAuthApplication((OAuthAppRequest) Mockito.any())).thenReturn(oAuthApplicationInfo);
Mockito.doNothing().when(apiMgtDAO).createApplicationKeyTypeMappingForManualClients(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
KeyManagerConfigurationDTO keyManagerConfigurationsDto = new KeyManagerConfigurationDTO();
keyManagerConfigurationsDto.setUuid(UUID.randomUUID().toString());
keyManagerConfigurationsDto.setEnabled(true);
Mockito.when(apiMgtDAO.isKeyManagerConfigurationExistByName("default", "carbon.super")).thenReturn(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName("carbon.super", "default")).thenReturn(keyManagerConfigurationsDto);
AccessTokenRequest accessTokenRequest = new AccessTokenRequest();
AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
BDDMockito.when(ApplicationUtils.createAccessTokenRequest(keyManager, oAuthApplicationInfo, null)).thenReturn(accessTokenRequest);
Mockito.when(keyManager.getNewApplicationAccessToken(accessTokenRequest)).thenReturn(accessTokenInfo);
try {
apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "refresh", "DEFAULT", "Resident Key Manager", "carbon.super");
Assert.fail("Exception is not thrown when client id is already mapped to an application");
} catch (APIManagementException e) {
Assert.assertTrue(e.getMessage().contains("Key Mappings already exists for application"));
}
Assert.assertEquals(8, apiConsumer.mapExistingOAuthClient("", "admin", "1", "app1", "PRODUCTION", "DEFAULT", "Resident Key Manager", "carbon.super").size());
}
use of org.wso2.carbon.apimgt.rest.api.admin.v1.dto.KeyManagerConfigurationDTO in project carbon-apimgt by wso2.
the class APIConsumerImplTest method testUpdateAuthClient.
@Test
public void testUpdateAuthClient() throws APIManagementException {
String consumerKey = "aNTf-EFga";
OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
BDDMockito.when(ApplicationUtils.createOauthAppRequest(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())).thenReturn(oAuthAppRequest);
Mockito.when(apiMgtDAO.getConsumerKeyByApplicationIdKeyTypeKeyManager(Mockito.anyInt(), Mockito.anyString(), Mockito.anyString())).thenReturn(consumerKey);
OAuthApplicationInfo updatedAppInfo = new OAuthApplicationInfo();
String clientName = "sample client";
updatedAppInfo.setClientName(clientName);
Mockito.when(keyManager.updateApplication((OAuthAppRequest) Mockito.any())).thenReturn(updatedAppInfo);
KeyManagerConfigurationDTO keyManagerConfiguration = new KeyManagerConfigurationDTO();
keyManagerConfiguration.setEnabled(true);
Mockito.when(apiMgtDAO.getKeyManagerConfigurationByName(Mockito.anyString(), Mockito.anyString())).thenReturn(keyManagerConfiguration);
System.setProperty(CARBON_HOME, "");
APIManagerConfiguration apiManagerConfiguration = Mockito.mock(APIManagerConfiguration.class);
APIManagerConfigurationService apiManagerConfigurationService = Mockito.mock(APIManagerConfigurationService.class);
Mockito.when(serviceReferenceHolder.getAPIManagerConfigurationService()).thenReturn(apiManagerConfigurationService);
Mockito.when(apiManagerConfigurationService.getAPIManagerConfiguration()).thenReturn(apiManagerConfiguration);
Mockito.when(apiManagerConfiguration.getFirstProperty(APIConstants.KEYMANAGER_SERVERURL)).thenReturn("http://localhost");
Application application = Mockito.mock(Application.class);
application.setUUID(UUID.nameUUIDFromBytes("app1".getBytes()).toString());
Subscriber subscriber = Mockito.mock(Subscriber.class);
Mockito.when(ApplicationUtils.retrieveApplication("app1", "1", null)).thenReturn(application);
Mockito.when(application.getSubscriber()).thenReturn(subscriber);
Mockito.when(subscriber.getName()).thenReturn("1");
APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
apiConsumer.tenantDomain = SAMPLE_TENANT_DOMAIN_1;
Assert.assertEquals(apiConsumer.updateAuthClient("1", application, "access", "www.host.com", new String[0], null, null, null, null, "default").getClientName(), clientName);
}
Aggregations