Search in sources :

Example 26 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class ApplicationKeyMappingUtilTestCase method testFromApplicationKeysToDTO.

@Test
public void testFromApplicationKeysToDTO() {
    String keyType = "PRODUCTION";
    List<String> grantTypes = new ArrayList<>();
    grantTypes.add("password");
    grantTypes.add("jwt");
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    oAuthApplicationInfo.setKeyType(keyType);
    oAuthApplicationInfo.setClientId("clientID");
    oAuthApplicationInfo.setClientSecret("clientSecret");
    oAuthApplicationInfo.setGrantTypes(grantTypes);
    ApplicationKeysDTO applicationKeysDTO = ApplicationKeyMappingUtil.fromApplicationKeysToDTO(oAuthApplicationInfo);
    Assert.assertEquals(applicationKeysDTO.getKeyType().toString(), keyType);
}
Also used : ApplicationKeysDTO(org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 27 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class APIStoreImpl method getApplicationKeys.

@Override
public List<OAuthApplicationInfo> getApplicationKeys(String applicationId) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Getting keys of App: " + applicationId);
    }
    if (StringUtils.isEmpty(applicationId)) {
        String msg = "Input value is null or empty. Application Id: " + applicationId;
        log.error(msg);
        throw new APIManagementException(msg, ExceptionCodes.OAUTH2_APP_RETRIEVAL_FAILED);
    }
    try {
        List<OAuthApplicationInfo> allKeysFromDB = getApplicationDAO().getApplicationKeys(applicationId);
        for (OAuthApplicationInfo keys : allKeysFromDB) {
            OAuthApplicationInfo oAuthApp = getKeyManager().retrieveApplication(keys.getClientId());
            keys.setClientSecret(oAuthApp.getClientSecret());
            keys.setGrantTypes(oAuthApp.getGrantTypes());
            keys.setCallBackURL(oAuthApp.getCallBackURL());
        }
        if (log.isDebugEnabled()) {
            log.debug("Retrieved all keys of App: " + applicationId);
        }
        return allKeysFromDB;
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while getting keys of application: " + applicationId;
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)

Example 28 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class APIStoreImpl method generateApplicationKeys.

@Override
public OAuthApplicationInfo generateApplicationKeys(String applicationId, String keyType, String callbackUrl, List<String> grantTypes) throws APIManagementException {
    if (log.isDebugEnabled()) {
        log.debug("Generating application keys for application: " + applicationId);
    }
    Application application = getApplicationByUuid(applicationId);
    OAuthAppRequest oauthAppRequest = new OAuthAppRequest(application.getName(), callbackUrl, keyType, grantTypes);
    OAuthApplicationInfo oauthAppInfo = getKeyManager().createApplication(oauthAppRequest);
    if (log.isDebugEnabled()) {
        log.debug("Application key generation was successful for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
    }
    try {
        getApplicationDAO().addApplicationKeys(applicationId, keyType, oauthAppInfo.getClientId());
    } catch (APIMgtDAOException e) {
        String errorMsg = "Error occurred while saving key data for application: " + application.getName();
        log.error(errorMsg, e);
        throw new APIManagementException(errorMsg, e, e.getErrorHandler());
    }
    if (log.isDebugEnabled()) {
        log.debug("Application keys are successfully saved in the database for application: " + application.getName() + " Client Id: " + oauthAppInfo.getClientId());
    }
    List<SubscriptionValidationData> subscriptionValidationData = getApiSubscriptionDAO().getAPISubscriptionsOfAppForValidation(applicationId, keyType);
    if (subscriptionValidationData != null && !subscriptionValidationData.isEmpty()) {
        getApiGateway().addAPISubscription(subscriptionValidationData);
    }
    return oauthAppInfo;
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthAppRequest(org.wso2.carbon.apimgt.core.models.OAuthAppRequest) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) SubscriptionValidationData(org.wso2.carbon.apimgt.core.models.SubscriptionValidationData) Application(org.wso2.carbon.apimgt.core.models.Application)

Example 29 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class ApplicationDAOImplIT method testAddAndGetApplicationKeys.

@Test
public void testAddAndGetApplicationKeys() throws Exception {
    ApplicationDAO applicationDAO = DAOFactory.getApplicationDAO();
    // add test app
    Application app = TestUtil.addTestApplicationWithPermissions();
    String appId = app.getId();
    String prodConsumerKey = "prod-xxx";
    String sandConsumerKey = "sand-yyy";
    // add prod key
    applicationDAO.addApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_PRODUCTION, prodConsumerKey);
    // get by key type
    OAuthApplicationInfo keysFromDB = applicationDAO.getApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_PRODUCTION);
    Assert.assertEquals(keysFromDB.getClientId(), prodConsumerKey);
    // add sand key
    applicationDAO.addApplicationKeys(appId, KeyManagerConstants.OAUTH_CLIENT_SANDBOX, sandConsumerKey);
    // get all keys
    List<OAuthApplicationInfo> allKeysFromDB = applicationDAO.getApplicationKeys(appId);
    Assert.assertEquals(allKeysFromDB.size(), 2, "Wrong number of keys are returned.");
    // this should stay 0 at the end
    int i = 0;
    for (OAuthApplicationInfo oAuthApplicationInfo : allKeysFromDB) {
        switch(oAuthApplicationInfo.getKeyType()) {
            case KeyManagerConstants.OAUTH_CLIENT_PRODUCTION:
                Assert.assertEquals(oAuthApplicationInfo.getClientId(), prodConsumerKey);
                i++;
                break;
            case KeyManagerConstants.OAUTH_CLIENT_SANDBOX:
                Assert.assertEquals(oAuthApplicationInfo.getClientId(), sandConsumerKey);
                i--;
                break;
            default:
                Assert.fail("Invalid key type.");
                break;
        }
    }
    Assert.assertEquals(i, 0, "Received key counts of each type is not 1");
}
Also used : OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ApplicationDAO(org.wso2.carbon.apimgt.core.dao.ApplicationDAO) Application(org.wso2.carbon.apimgt.core.models.Application) Test(org.testng.annotations.Test)

Example 30 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo in project carbon-apimgt by wso2.

the class AuthenticatorServiceTestCase method testGetAuthenticationConfigurationsForPublisher.

@Test
public void testGetAuthenticationConfigurationsForPublisher() throws Exception {
    // Happy Path - 200
    // // Mocked response object from DCR api
    SystemApplicationDao systemApplicationDao = Mockito.mock(SystemApplicationDao.class);
    Mockito.when(systemApplicationDao.isConsumerKeyExistForApplication("store")).thenReturn(false);
    APIMConfigurationService apimConfigurationService = Mockito.mock(APIMConfigurationService.class);
    EnvironmentConfigurations environmentConfigurations = new EnvironmentConfigurations();
    Mockito.when(apimConfigurationService.getEnvironmentConfigurations()).thenReturn(environmentConfigurations);
    APIMAppConfigurationService apimAppConfigurationService = Mockito.mock(APIMAppConfigurationService.class);
    APIMAppConfigurations apimAppConfigurations = new APIMAppConfigurations();
    Mockito.when(apimAppConfigurationService.getApimAppConfigurations()).thenReturn(apimAppConfigurations);
    OAuthApplicationInfo oAuthApplicationInfo = new OAuthApplicationInfo();
    oAuthApplicationInfo.setClientId("xxx-client-id-xxx");
    oAuthApplicationInfo.setCallBackURL("https://localhost:9292/login/callback/publisher");
    // // Expected data object to be passed to the front-end
    JsonObject oAuthData = new JsonObject();
    String scopes = "apim:api_view apim:api_create apim:api_update apim:api_delete apim:apidef_update " + "apim:api_publish apim:subscription_view apim:subscription_block openid " + "apim:external_services_discover apim:dedicated_gateway";
    oAuthData.addProperty(KeyManagerConstants.OAUTH_CLIENT_ID, oAuthApplicationInfo.getClientId());
    oAuthData.addProperty(KeyManagerConstants.OAUTH_CALLBACK_URIS, oAuthApplicationInfo.getCallBackURL());
    oAuthData.addProperty(KeyManagerConstants.TOKEN_SCOPES, scopes);
    oAuthData.addProperty(KeyManagerConstants.AUTHORIZATION_ENDPOINT, "https://localhost:9443/oauth2/authorize");
    oAuthData.addProperty(AuthenticatorConstants.SSO_ENABLED, ServiceReferenceHolder.getInstance().getAPIMAppConfiguration().isSsoEnabled());
    KeyManager keyManager = Mockito.mock(KeyManager.class);
    MultiEnvironmentOverview multiEnvironmentOverview = new MultiEnvironmentOverview();
    environmentConfigurations.setMultiEnvironmentOverview(multiEnvironmentOverview);
    multiEnvironmentOverview.setEnabled(true);
    AuthenticatorService authenticatorService = new AuthenticatorService(keyManager, systemApplicationDao, apimConfigurationService, apimAppConfigurationService);
    // // Get data object to be passed to the front-end
    Mockito.when(keyManager.createApplication(Mockito.any())).thenReturn(oAuthApplicationInfo);
    JsonObject responseOAuthDataObj = authenticatorService.getAuthenticationConfigurations("publisher");
    String[] scopesActual = responseOAuthDataObj.get(KeyManagerConstants.TOKEN_SCOPES).toString().split(" ");
    String[] scopesExpected = oAuthData.get(KeyManagerConstants.TOKEN_SCOPES).toString().split(" ");
    Assert.assertEquals(scopesActual.length, scopesExpected.length);
    // Error Path - 500 - When OAuthApplicationInfo is null
    JsonObject emptyOAuthDataObj = new JsonObject();
    Mockito.when(keyManager.createApplication(Mockito.any())).thenReturn(null);
    JsonObject responseEmptyOAuthDataObj = authenticatorService.getAuthenticationConfigurations("publisher");
    Assert.assertEquals(responseEmptyOAuthDataObj, emptyOAuthDataObj);
    // Error Path - When DCR application creation fails and throws an APIManagementException
    Mockito.when(keyManager.createApplication(Mockito.any())).thenThrow(KeyManagementException.class);
    try {
        authenticatorService.getAuthenticationConfigurations("publisher");
    } catch (APIManagementException e) {
        Assert.assertEquals(e.getMessage(), "Error while creating the keys for OAuth application : publisher");
    }
}
Also used : JsonObject(com.google.gson.JsonObject) APIMAppConfigurationService(org.wso2.carbon.apimgt.rest.api.authenticator.configuration.APIMAppConfigurationService) EnvironmentConfigurations(org.wso2.carbon.apimgt.core.configuration.models.EnvironmentConfigurations) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) APIMAppConfigurations(org.wso2.carbon.apimgt.rest.api.authenticator.configuration.models.APIMAppConfigurations) SystemApplicationDao(org.wso2.carbon.apimgt.core.dao.SystemApplicationDao) MultiEnvironmentOverview(org.wso2.carbon.apimgt.core.configuration.models.MultiEnvironmentOverview) KeyManager(org.wso2.carbon.apimgt.core.api.KeyManager) APIMConfigurationService(org.wso2.carbon.apimgt.core.configuration.APIMConfigurationService) Test(org.junit.Test)

Aggregations

OAuthApplicationInfo (org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo)30 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)15 ArrayList (java.util.ArrayList)12 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)10 Test (org.junit.Test)9 ApplicationKeysDTO (org.wso2.carbon.apimgt.rest.api.store.dto.ApplicationKeysDTO)9 KeyManagementException (org.wso2.carbon.apimgt.core.exception.KeyManagementException)8 HashMap (java.util.HashMap)7 DCRClientInfo (org.wso2.carbon.apimgt.core.auth.dto.DCRClientInfo)6 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)6 Response (feign.Response)5 Map (java.util.Map)5 Response (javax.ws.rs.core.Response)5 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)5 OAuth2IntrospectionResponse (org.wso2.carbon.apimgt.core.auth.dto.OAuth2IntrospectionResponse)5 ApplicationCreationResponse (org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse)5 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)5 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)5 Request (org.wso2.msf4j.Request)5