Search in sources :

Example 16 with APIKey

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

the class ApiMgtDAO method getKeyMappingFromApplicationIdAndKeyMappingId.

public APIKey getKeyMappingFromApplicationIdAndKeyMappingId(int applicationId, String keyMappingId) throws APIManagementException {
    final String query = "SELECT UUID,CONSUMER_KEY,KEY_MANAGER,KEY_TYPE,STATE,CREATE_MODE FROM " + "AM_APPLICATION_KEY_MAPPING WHERE APPLICATION_ID=? AND UUID = ?";
    Set<APIKey> apiKeyList = new HashSet<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(query)) {
        preparedStatement.setInt(1, applicationId);
        preparedStatement.setString(2, keyMappingId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            if (resultSet.next()) {
                APIKey apiKey = new APIKey();
                apiKey.setMappingId(resultSet.getString("UUID"));
                apiKey.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
                apiKey.setKeyManager(resultSet.getString("KEY_MANAGER"));
                apiKey.setType(resultSet.getString("KEY_TYPE"));
                apiKey.setState(resultSet.getString("STATE"));
                String createMode = resultSet.getString("CREATE_MODE");
                if (StringUtils.isEmpty(createMode)) {
                    createMode = APIConstants.OAuthAppMode.CREATED.name();
                }
                apiKey.setCreateMode(createMode);
                return apiKey;
            }
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while Retrieving Key Mapping ", e);
    }
    return null;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 17 with APIKey

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

the class ApiMgtDAO method getKeyMappingsFromApplicationId.

public Set<APIKey> getKeyMappingsFromApplicationId(int applicationId) throws APIManagementException {
    Set<APIKey> apiKeyList = new HashSet<>();
    try (Connection connection = APIMgtDBUtil.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(SQLConstants.GET_KEY_MAPPING_INFO_FROM_APP_ID)) {
        preparedStatement.setInt(1, applicationId);
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                APIKey apiKey = new APIKey();
                apiKey.setMappingId(resultSet.getString("UUID"));
                apiKey.setConsumerKey(resultSet.getString("CONSUMER_KEY"));
                apiKey.setKeyManager(resultSet.getString("KEY_MANAGER"));
                apiKey.setType(resultSet.getString("KEY_TYPE"));
                apiKey.setState(resultSet.getString("STATE"));
                String createMode = resultSet.getString("CREATE_MODE");
                if (StringUtils.isEmpty(createMode)) {
                    createMode = APIConstants.OAuthAppMode.CREATED.name();
                }
                apiKey.setCreateMode(createMode);
                try (InputStream appInfo = resultSet.getBinaryStream("APP_INFO")) {
                    if (appInfo != null) {
                        apiKey.setAppMetaData(IOUtils.toString(appInfo));
                    }
                } catch (IOException e) {
                    log.error("Error while retrieving metadata", e);
                }
                apiKeyList.add(apiKey);
            }
        }
    } catch (SQLException e) {
        throw new APIManagementException("Error while Retrieving Key Mappings ", e);
    }
    return apiKeyList;
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) SQLException(java.sql.SQLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 18 with APIKey

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

the class APIConsumerImplTest method testGetSubscribedIdentifiers.

@Test
public void testGetSubscribedIdentifiers() throws APIManagementException {
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Set<SubscribedAPI> originalSubscribedAPIs = new HashSet<>();
    SubscribedAPI subscribedAPI = Mockito.mock(SubscribedAPI.class);
    originalSubscribedAPIs.add(subscribedAPI);
    Subscriber subscriber = new Subscriber("Subscriber");
    APIIdentifier apiId1 = new APIIdentifier(API_PROVIDER, SAMPLE_API_NAME, SAMPLE_API_VERSION);
    Tier tier = Mockito.mock(Tier.class);
    when(apiMgtDAO.getSubscribedAPIs("testorg", subscriber, "testID")).thenReturn(originalSubscribedAPIs);
    when(subscribedAPI.getTier()).thenReturn(tier);
    when(tier.getName()).thenReturn("tier");
    when(subscribedAPI.getApiId()).thenReturn(apiId1);
    Application app = Mockito.mock(Application.class);
    when(app.getId()).thenReturn(1);
    when(subscribedAPI.getApplication()).thenReturn(app);
    Set<APIKey> apiKeys = new HashSet<>();
    APIKey apiKey = new APIKey();
    apiKey.setType("Production");
    apiKeys.add(apiKey);
    Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
    Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
    assertNotNull(apiConsumer.getSubscribedIdentifiers(subscriber, apiId1, "testID", "testorg"));
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) Tier(org.wso2.carbon.apimgt.api.model.Tier) SubscribedAPI(org.wso2.carbon.apimgt.api.model.SubscribedAPI) APIIdentifier(org.wso2.carbon.apimgt.api.model.APIIdentifier) Application(org.wso2.carbon.apimgt.api.model.Application) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 19 with APIKey

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

the class APIConsumerImplTest method testGetApplicationKeys.

@Test
public void testGetApplicationKeys() throws APIManagementException {
    APIKey apiKey1 = new APIKey();
    apiKey1.setConsumerKey(UUID.randomUUID().toString());
    apiKey1.setType(APIConstants.API_KEY_TYPE_PRODUCTION);
    apiKey1.setState(UUID.randomUUID().toString());
    APIKey apiKey2 = new APIKey();
    apiKey2.setConsumerKey(UUID.randomUUID().toString());
    apiKey2.setType(APIConstants.API_KEY_TYPE_SANDBOX);
    apiKey2.setState(UUID.randomUUID().toString());
    APIConsumerImpl apiConsumer = new APIConsumerImplWrapper(apiMgtDAO);
    Map<String, String> consumerKeyMap = new HashMap<>();
    consumerKeyMap.put("default", apiKey1.getConsumerKey());
    Set<APIKey> apiKeys = new HashSet<>();
    apiKeys.add(apiKey1);
    apiKeys.add(apiKey2);
    Mockito.when(apiMgtDAO.getKeyMappingsFromApplicationId(Mockito.anyInt())).thenReturn(apiKeys);
    Mockito.when(apiMgtDAO.getConsumerkeyByApplicationIdAndKeyType(Mockito.anyInt(), Mockito.anyString())).thenReturn(consumerKeyMap, consumerKeyMap);
    AccessTokenInfo accessTokenInfo = new AccessTokenInfo();
    accessTokenInfo.setAccessToken(UUID.randomUUID().toString());
    Mockito.when(keyManager.getAccessTokenByConsumerKey(Mockito.anyString())).thenReturn(accessTokenInfo);
    Mockito.when(keyManagerConfigurationDTO.isEnabled()).thenReturn(true);
    assertNotNull(apiConsumer.getApplicationKeys(1));
    assertEquals(apiConsumer.getApplicationKeys(1).size(), 2);
    assertNotNull(apiConsumer.getApplicationKeys(1).iterator().next().getAccessToken());
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey) AccessTokenInfo(org.wso2.carbon.apimgt.api.model.AccessTokenInfo) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 20 with APIKey

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

the class APIConsumerImpl method cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId.

@Override
public void cleanUpApplicationRegistrationByApplicationIdAndKeyMappingId(int applicationId, String keyMappingId) throws APIManagementException {
    APIKey apiKey = apiMgtDAO.getKeyMappingFromApplicationIdAndKeyMappingId(applicationId, keyMappingId);
    if (apiKey != null) {
        apiMgtDAO.deleteApplicationRegistration(applicationId, apiKey.getType(), apiKey.getKeyManager());
        apiMgtDAO.deleteApplicationKeyMappingByMappingId(keyMappingId);
    }
}
Also used : APIKey(org.wso2.carbon.apimgt.api.model.APIKey)

Aggregations

APIKey (org.wso2.carbon.apimgt.api.model.APIKey)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)14 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)14 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)14 ArrayList (java.util.ArrayList)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 Test (org.testng.annotations.Test)11 Application (org.wso2.carbon.apimgt.api.model.Application)10 Cache (javax.cache.Cache)9 AxisConfiguration (org.apache.axis2.engine.AxisConfiguration)9 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)9 HttpResponse (org.wso2.micro.gateway.tests.util.HttpResponse)9 HashSet (java.util.HashSet)7 APIKeyDataStore (org.wso2.carbon.apimgt.gateway.handlers.security.keys.APIKeyDataStore)7 WSAPIKeyDataStore (org.wso2.carbon.apimgt.gateway.handlers.security.keys.WSAPIKeyDataStore)7 ApplicationKeyDTO (org.wso2.carbon.apimgt.rest.api.store.v1.dto.ApplicationKeyDTO)6 IOException (java.io.IOException)5 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)5 LinkedHashSet (java.util.LinkedHashSet)4