Search in sources :

Example 61 with OAuthApplicationInfo

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

the class ApplicationRegistrationWSWorkflowExecutorTest method init.

@Before
public void init() throws Exception {
    ServiceReferenceHolder serviceReferenceHolder = TestUtils.getServiceReferenceHolder();
    ConfigurationContextService configurationContextService = Mockito.mock(ConfigurationContextService.class);
    configurationContext = Mockito.mock(ConfigurationContext.class);
    PowerMockito.when(serviceReferenceHolder.getContextService()).thenReturn(configurationContextService);
    PowerMockito.when(configurationContextService.getClientConfigContext()).thenReturn(configurationContext);
    serviceClient = Mockito.mock(ServiceClient.class);
    PowerMockito.whenNew(ServiceClient.class).withAnyArguments().thenReturn(serviceClient);
    applicationRegistrationWSWorkflowExecutor = new ApplicationRegistrationWSWorkflowExecutor();
    apiMgtDAO = TestUtils.getApiMgtDAO();
    application = new Application("test", new Subscriber("testUser"));
    application.setCallbackUrl(callBaclURL);
    application.setTier("Unlimited");
    PowerMockito.mockStatic(KeyManagerHolder.class);
    keyManager = Mockito.mock(KeyManager.class);
    KeyManagerConfiguration keyManagerConfiguration = new KeyManagerConfiguration();
    Mockito.when(keyManager.getKeyManagerConfiguration()).thenReturn(keyManagerConfiguration);
    PowerMockito.when(KeyManagerHolder.getKeyManagerInstance("carbon.super", "default")).thenReturn(keyManager);
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    PowerMockito.when(keyManager.createApplication((OAuthAppRequest) Mockito.anyObject())).thenReturn(oAuthApplicationInfo);
    OAuthAppRequest oAuthAppRequest = new OAuthAppRequest();
    oAuthAppRequest.setOAuthApplicationInfo(oAuthApplicationInfo);
    workflowDTO = new ApplicationRegistrationWorkflowDTO();
    workflowDTO.setWorkflowReference("1");
    workflowDTO.setApplication(application);
    workflowDTO.setCallbackUrl(callBaclURL);
    workflowDTO.setTenantDomain("carbon.super");
    workflowDTO.setUserName("testUser");
    workflowDTO.setExternalWorkflowReference("testUser");
    workflowDTO.setKeyType("PRODUCTION");
    workflowDTO.setAppInfoDTO(oAuthAppRequest);
    KeyManagerConfigurationDTO kmConfigDTO = new KeyManagerConfigurationDTO();
    kmConfigDTO.setOrganization("carbon.super");
    kmConfigDTO.setName("default");
    PowerMockito.when(apiMgtDAO.getKeyManagerConfigurationByUUID("default")).thenReturn(kmConfigDTO);
}
Also used : ServiceReferenceHolder(org.wso2.carbon.apimgt.impl.internal.ServiceReferenceHolder) ConfigurationContext(org.apache.axis2.context.ConfigurationContext) KeyManagerConfigurationDTO(org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO) ApplicationRegistrationWorkflowDTO(org.wso2.carbon.apimgt.impl.dto.ApplicationRegistrationWorkflowDTO) ConfigurationContextService(org.wso2.carbon.utils.ConfigurationContextService) Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) ServiceClient(org.apache.axis2.client.ServiceClient) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) Application(org.wso2.carbon.apimgt.api.model.Application) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager) KeyManagerConfiguration(org.wso2.carbon.apimgt.api.model.KeyManagerConfiguration) Before(org.junit.Before)

Example 62 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImplTest method testCreateApplicationWithException.

@Test(expected = APIManagementException.class)
public void testCreateApplicationWithException() throws APIManagementException {
    Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
    OAuthAppRequest oauthRequest = new OAuthAppRequest();
    OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
    oauthRequest.setOAuthApplicationInfo(oauthApplication);
    keyManager.createApplication(oauthRequest);
}
Also used : OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 63 with OAuthApplicationInfo

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

the class AMDefaultKeyManagerImplTest method testCreateApplication.

@Test
public void testCreateApplication() throws APIManagementException, KeyManagerClientException {
    PowerMockito.mockStatic(APIUtil.class);
    System.setProperty("carbon.home", "jhkjn");
    PowerMockito.mockStatic(PrivilegedCarbonContext.class);
    OAuthAppRequest oauthRequest = new OAuthAppRequest();
    OAuthApplicationInfo oauthApplication = new OAuthApplicationInfo();
    oauthApplication.setAppOwner(APP_OWNER);
    oauthApplication.setCallBackURL(StringUtils.join(REDIRECT_URIS, ","));
    oauthApplication.setClientName(APP_NAME);
    oauthApplication.addParameter(ApplicationConstants.OAUTH_CLIENT_USERNAME, APP_OWNER);
    oauthApplication.addParameter(ApplicationConstants.APP_KEY_TYPE, KEY_TYPE);
    oauthApplication.setJsonString(getJSONString());
    oauthRequest.setMappingId("123");
    oauthRequest.setOAuthApplicationInfo(oauthApplication);
    PowerMockito.when(APIUtil.isCrossTenantSubscriptionsEnabled()).thenReturn(false);
    PrivilegedCarbonContext privilegedCarbonContext = Mockito.mock(PrivilegedCarbonContext.class);
    ClientInfo response = new ClientInfo();
    response.setClientId(CLIENT_ID);
    response.setClientName(APP_UUID);
    response.setClientSecret(CLIENT_SECRET);
    response.setRedirectUris(Arrays.asList(REDIRECT_URIS));
    response.setGrantTypes(Arrays.asList(GRANT_TYPES));
    Mockito.when(dcrClient.createApplication(Mockito.any(ClientInfo.class))).thenReturn(response);
    PowerMockito.when(PrivilegedCarbonContext.getThreadLocalCarbonContext()).thenReturn(privilegedCarbonContext);
    Mockito.when(privilegedCarbonContext.getTenantDomain()).thenReturn(MultitenantConstants.SUPER_TENANT_DOMAIN_NAME);
    Mockito.when(APIUtil.getApplicationUUID(Mockito.anyString(), Mockito.anyString())).thenReturn(APP_UUID);
    OAuthApplicationInfo oauthApplicationResponse = keyManager.createApplication(oauthRequest);
    Assert.assertEquals(StringUtils.join(REDIRECT_URIS, ","), oauthApplicationResponse.getCallBackURL());
    Assert.assertEquals(APP_UUID, oauthApplicationResponse.getClientName());
}
Also used : OAuthAppRequest(org.wso2.carbon.apimgt.api.model.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) PrivilegedCarbonContext(org.wso2.carbon.context.PrivilegedCarbonContext) ClientInfo(org.wso2.carbon.apimgt.impl.kmclient.model.ClientInfo) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 64 with OAuthApplicationInfo

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

the class ApiMgtDAO method getClientOfApplication.

private Map<String, OAuthApplicationInfo> getClientOfApplication(String tenntDomain, int applicationID, String keyType) throws APIManagementException {
    String sqlQuery = SQLConstants.GET_CLIENT_OF_APPLICATION_SQL;
    Map<String, OAuthApplicationInfo> keyTypeWiseOAuthApps = new HashMap<>();
    Connection connection = null;
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        ps = connection.prepareStatement(sqlQuery);
        ps.setInt(1, applicationID);
        ps.setString(2, keyType);
        rs = ps.executeQuery();
        while (rs.next()) {
            String consumerKey = rs.getString("CONSUMER_KEY");
            String keyManagerName = rs.getString("KEY_MANAGER");
            if (consumerKey != null) {
                KeyManager keyManager = KeyManagerHolder.getKeyManagerInstance(tenntDomain, keyManagerName);
                if (keyManager != null) {
                    OAuthApplicationInfo oAuthApplication = keyManager.retrieveApplication(consumerKey);
                    keyTypeWiseOAuthApps.put(keyManagerName, oAuthApplication);
                }
            }
        }
    } catch (SQLException e) {
        handleException("Failed to get  client of application. SQL error", e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, connection, rs);
    }
    return keyTypeWiseOAuthApps;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) SQLException(java.sql.SQLException) OAuthApplicationInfo(org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) KeyManager(org.wso2.carbon.apimgt.api.model.KeyManager)

Example 65 with OAuthApplicationInfo

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

the class ApiMgtDAO method getApplicationById.

public Application getApplicationById(int applicationId, String userId, String groupId) throws APIManagementException {
    Connection connection = null;
    PreparedStatement prepStmt = null;
    ResultSet rs = null;
    Application application = null;
    try {
        connection = APIMgtDBUtil.getConnection();
        String query = SQLConstants.GET_APPLICATION_BY_ID_SQL;
        String whereClause = "  AND SUB.USER_ID =?";
        String whereClauseCaseInSensitive = "  AND LOWER(SUB.USER_ID) =LOWER(?)";
        String whereClauseWithGroupId = "  AND  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP.GROUP_ID IS NULL)" + " AND SUB.USER_ID = ?))";
        String whereClauseWithGroupIdCaseInSensitive = "  AND  (APP.GROUP_ID = ? OR ((APP.GROUP_ID='' OR APP" + ".GROUP_ID IS NULL)" + " AND LOWER(SUB.USER_ID) = LOWER(?)))";
        String whereClauseWithMultiGroupId = "  AND  ((APP.APPLICATION_ID IN (SELECT APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  OR   SUB.USER_ID = ? )";
        String whereClauseWithMultiGroupIdCaseInSensitive = "  AND  ((APP.APPLICATION_ID IN (SELECT " + "APPLICATION_ID  FROM " + "AM_APPLICATION_GROUP_MAPPING WHERE GROUP_ID IN ($params) AND TENANT = ?))  OR   LOWER(SUB" + ".USER_ID) = LOWER(?) )";
        if (groupId != null && !"null".equals(groupId) && !groupId.isEmpty()) {
            if (multiGroupAppSharingEnabled) {
                Subscriber subscriber = getSubscriber(userId);
                String tenantDomain = MultitenantUtils.getTenantDomain(subscriber.getName());
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithMultiGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithMultiGroupId;
                }
                String[] groupIds = groupId.split(",");
                // since index 1 is applicationId
                int parameterIndex = groupIds.length + 1;
                // query params will fil from 2
                prepStmt = fillQueryParams(connection, query, groupIds, 2);
                prepStmt.setString(++parameterIndex, tenantDomain);
                prepStmt.setInt(1, applicationId);
                prepStmt.setString(++parameterIndex, userId);
            } else {
                if (forceCaseInsensitiveComparisons) {
                    query = query + whereClauseWithGroupIdCaseInSensitive;
                } else {
                    query = query + whereClauseWithGroupId;
                }
                prepStmt = connection.prepareStatement(query);
                prepStmt.setInt(1, applicationId);
                prepStmt.setString(2, groupId);
                prepStmt.setString(3, userId);
            }
        } else {
            if (forceCaseInsensitiveComparisons) {
                query = query + whereClauseCaseInSensitive;
            } else {
                query = query + whereClause;
            }
            prepStmt = connection.prepareStatement(query);
            prepStmt.setInt(1, applicationId);
            prepStmt.setString(2, userId);
        }
        rs = prepStmt.executeQuery();
        if (rs.next()) {
            String applicationName = rs.getString("NAME");
            String subscriberId = rs.getString("SUBSCRIBER_ID");
            String subscriberName = rs.getString("USER_ID");
            Subscriber subscriber = new Subscriber(subscriberName);
            subscriber.setId(Integer.parseInt(subscriberId));
            application = new Application(applicationName, subscriber);
            application.setOwner(rs.getString("CREATED_BY"));
            application.setDescription(rs.getString("DESCRIPTION"));
            application.setStatus(rs.getString("APPLICATION_STATUS"));
            application.setCallbackUrl(rs.getString("CALLBACK_URL"));
            application.setId(rs.getInt("APPLICATION_ID"));
            application.setGroupId(rs.getString("GROUP_ID"));
            application.setUUID(rs.getString("UUID"));
            application.setTier(rs.getString("APPLICATION_TIER"));
            subscriber.setId(rs.getInt("SUBSCRIBER_ID"));
            String tenantDomain = MultitenantUtils.getTenantDomain(subscriberName);
            Map<String, Map<String, OAuthApplicationInfo>> keyMap = getOAuthApplications(tenantDomain, application.getId());
            application.getKeyManagerWiseOAuthApp().putAll(keyMap);
            if (multiGroupAppSharingEnabled) {
                if (application.getGroupId() == null || application.getGroupId().isEmpty()) {
                    application.setGroupId(getGroupId(connection, applicationId));
                }
            }
        }
        if (application != null) {
            Map<String, String> applicationAttributes = getApplicationAttributes(connection, applicationId);
            application.setApplicationAttributes(applicationAttributes);
        }
    } catch (SQLException e) {
        handleException("Error while obtaining details of the Application : " + applicationId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(prepStmt, connection, rs);
    }
    return application;
}
Also used : Subscriber(org.wso2.carbon.apimgt.api.model.Subscriber) SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Application(org.wso2.carbon.apimgt.api.model.Application) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap)

Aggregations

OAuthApplicationInfo (org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo)37 OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)30 Test (org.junit.Test)22 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)21 HashMap (java.util.HashMap)19 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 OAuthAppRequest (org.wso2.carbon.apimgt.api.model.OAuthAppRequest)15 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 ArrayList (java.util.ArrayList)13 Map (java.util.Map)13 KeyManagerConfigurationDTO (org.wso2.carbon.apimgt.api.dto.KeyManagerConfigurationDTO)11 Application (org.wso2.carbon.apimgt.api.model.Application)11 KeyManager (org.wso2.carbon.apimgt.api.model.KeyManager)10 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)10 JsonObject (com.google.gson.JsonObject)9 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)9 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)9 JSONObject (org.json.simple.JSONObject)8 AccessTokenRequest (org.wso2.carbon.apimgt.api.model.AccessTokenRequest)8 Gson (com.google.gson.Gson)7