Search in sources :

Example 26 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.api.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);
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ByteArrayInputStream(java.io.ByteArrayInputStream) SQLException(java.sql.SQLException) SQLIntegrityConstraintViolationException(java.sql.SQLIntegrityConstraintViolationException) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException)

Example 27 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.api.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);
        }
    }
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) APIManagerConfigurationService(org.wso2.carbon.apimgt.impl.APIManagerConfigurationService) TokenHandlingDto(org.wso2.carbon.apimgt.impl.dto.TokenHandlingDto) Gson(com.google.gson.Gson) ApiMgtDAO(org.wso2.carbon.apimgt.impl.dao.ApiMgtDAO)

Example 28 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.api.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");
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) Matchers.anyString(org.mockito.Matchers.anyString) Scope(org.wso2.carbon.apimgt.api.model.Scope) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) UserStoreException(org.wso2.carbon.user.api.UserStoreException) JSONObject(org.json.simple.JSONObject) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 29 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.api.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());
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 30 with KeyManagerConfigurationDTO

use of org.wso2.carbon.apimgt.api.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);
}
Also used : KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) Matchers.anyString(org.mockito.Matchers.anyString) Application(org.wso2.carbon.apimgt.api.model.Application) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)43 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)30 Gson (com.google.gson.Gson)16 ArrayList (java.util.ArrayList)13 HashMap (java.util.HashMap)12 OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)11 JSONObject (org.json.simple.JSONObject)10 JsonObject (com.google.gson.JsonObject)9 PreparedStatement (java.sql.PreparedStatement)9 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)9 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)9 Map (java.util.Map)8 APIAdmin (org.wso2.carbon.apimgt.api.APIAdmin)8 APIAdminImpl (org.wso2.carbon.apimgt.impl.APIAdminImpl)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 Connection (java.sql.Connection)7 SQLException (java.sql.SQLException)7 IdentityProvider (org.wso2.carbon.identity.application.common.model.IdentityProvider)7 LinkedHashMap (java.util.LinkedHashMap)6 Application (org.wso2.carbon.apimgt.api.model.Application)6