Search in sources :

Example 51 with OAuthApplicationInfo

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

the class AuthenticatorService method getAuthenticationConfigurations.

/**
 * This method returns the details of a DCR application.
 *
 * @param appName Name of the application to be created
 * @return oAuthData - A JsonObject with DCR application details, scopes, auth endpoint, and SSO is enabled or not
 * @throws APIManagementException When creating DCR application fails
 */
public JsonObject getAuthenticationConfigurations(String appName) throws APIManagementException {
    JsonObject oAuthData = new JsonObject();
    MultiEnvironmentOverview multiEnvironmentOverviewConfigs = apimConfigurationService.getEnvironmentConfigurations().getMultiEnvironmentOverview();
    boolean isMultiEnvironmentOverviewEnabled = multiEnvironmentOverviewConfigs.isEnabled();
    List<String> grantTypes = new ArrayList<>();
    grantTypes.add(KeyManagerConstants.PASSWORD_GRANT_TYPE);
    grantTypes.add(KeyManagerConstants.AUTHORIZATION_CODE_GRANT_TYPE);
    grantTypes.add(KeyManagerConstants.REFRESH_GRANT_TYPE);
    if (isMultiEnvironmentOverviewEnabled) {
        grantTypes.add(KeyManagerConstants.JWT_GRANT_TYPE);
    }
    APIMAppConfigurations appConfigs = apimAppConfigurationService.getApimAppConfigurations();
    String callBackURL = appConfigs.getApimBaseUrl() + AuthenticatorConstants.AUTHORIZATION_CODE_CALLBACK_URL + appName;
    // Get scopes of the application
    String scopes = getApplicationScopes(appName);
    log.debug("Set scopes for {} application using swagger definition.", appName);
    OAuthApplicationInfo oAuthApplicationInfo;
    try {
        oAuthApplicationInfo = createDCRApplication(appName, callBackURL, grantTypes);
        if (oAuthApplicationInfo != null) {
            log.debug("Created DCR Application successfully for {}.", appName);
            String oAuthApplicationClientId = oAuthApplicationInfo.getClientId();
            String oAuthApplicationCallBackURL = oAuthApplicationInfo.getCallBackURL();
            oAuthData.addProperty(KeyManagerConstants.OAUTH_CLIENT_ID, oAuthApplicationClientId);
            oAuthData.addProperty(KeyManagerConstants.OAUTH_CALLBACK_URIS, oAuthApplicationCallBackURL);
            oAuthData.addProperty(KeyManagerConstants.TOKEN_SCOPES, scopes);
            oAuthData.addProperty(KeyManagerConstants.AUTHORIZATION_ENDPOINT, appConfigs.getAuthorizationEndpoint());
            oAuthData.addProperty(AuthenticatorConstants.SSO_ENABLED, appConfigs.isSsoEnabled());
            oAuthData.addProperty(AuthenticatorConstants.MULTI_ENVIRONMENT_OVERVIEW_ENABLED, isMultiEnvironmentOverviewEnabled);
        } else {
            String errorMsg = "No information available in OAuth application.";
            log.error(errorMsg, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
        }
    } catch (APIManagementException e) {
        String errorMsg = "Error while creating the keys for OAuth application : " + appName;
        log.error(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
        throw new APIManagementException(errorMsg, e, ExceptionCodes.OAUTH2_APP_CREATION_FAILED);
    }
    return oAuthData;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) OAuthApplicationInfo(org.wso2.carbon.apimgt.core.models.OAuthApplicationInfo) ArrayList(java.util.ArrayList) APIMAppConfigurations(org.wso2.carbon.apimgt.rest.api.authenticator.configuration.models.APIMAppConfigurations) JsonObject(com.google.gson.JsonObject) MultiEnvironmentOverview(org.wso2.carbon.apimgt.core.configuration.models.MultiEnvironmentOverview)

Example 52 with OAuthApplicationInfo

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

the class AbstractKeyManager method buildAccessTokenRequestFromOAuthApp.

public AccessTokenRequest buildAccessTokenRequestFromOAuthApp(OAuthApplicationInfo oAuthApplication, AccessTokenRequest tokenRequest) throws APIManagementException {
    if (oAuthApplication == null) {
        return tokenRequest;
    }
    if (tokenRequest == null) {
        tokenRequest = new AccessTokenRequest();
    }
    if (oAuthApplication.getClientId() == null || oAuthApplication.getClientSecret() == null) {
        throw new APIManagementException("Consumer key or Consumer Secret missing.");
    }
    tokenRequest.setClientId(oAuthApplication.getClientId());
    tokenRequest.setClientSecret(oAuthApplication.getClientSecret());
    if (oAuthApplication.getParameter("tokenScope") != null) {
        String[] tokenScopes = (String[]) oAuthApplication.getParameter("tokenScope");
        tokenRequest.setScope(tokenScopes);
        oAuthApplication.addParameter("tokenScope", Arrays.toString(tokenScopes));
    }
    if (oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD) != null) {
        tokenRequest.setValidityPeriod(Long.parseLong((String) oAuthApplication.getParameter(ApplicationConstants.VALIDITY_PERIOD)));
    }
    return tokenRequest;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) AccessTokenRequest(org.wso2.carbon.apimgt.api.model.AccessTokenRequest)

Example 53 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo 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 54 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo 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 55 with OAuthApplicationInfo

use of org.wso2.carbon.apimgt.api.model.OAuthApplicationInfo 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

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