Search in sources :

Example 1 with OAuthAdminServiceImpl

use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthServiceComponent method activate.

protected void activate(ComponentContext context) {
    try {
        // initialize the OAuth Server configuration
        OAuthServerConfiguration oauthServerConfig = OAuthServerConfiguration.getInstance();
        if (OAuthCache.getInstance().isEnabled()) {
            log.debug("OAuth Caching is enabled. Initializing the cache.");
        }
        IdentityOathEventListener listener = new IdentityOathEventListener();
        serviceRegistration = context.getBundleContext().registerService(UserOperationEventListener.class.getName(), listener, null);
        log.debug("Identity Oath Event Listener is enabled");
        context.getBundleContext().registerService(AbstractEventHandler.class.getName(), new IdentityOauthEventHandler(), null);
        if (log.isDebugEnabled()) {
            log.debug("Identity Oauth Event handler is enabled");
        }
        OAuth2Service oauth2Service = new OAuth2Service();
        context.getBundleContext().registerService(OAuth2Service.class.getName(), oauth2Service, null);
        OAuthComponentServiceHolder.getInstance().setOauth2Service(oauth2Service);
        // We need to explicitly populate the OAuthTokenIssuerMap since it's used for token validation.
        oauthServerConfig.populateOAuthTokenIssuerMap();
        OAuthAdminServiceImpl oauthAdminService = new OAuthAdminServiceImpl();
        OAuthComponentServiceHolder.getInstance().setOAuthAdminService(oauthAdminService);
        OAuth2ServiceComponentHolder.getInstance().setOAuthAdminService(oauthAdminService);
        context.getBundleContext().registerService(OAuthEventInterceptor.class, new OAuthTokenSessionMappingEventHandler(), null);
        if (log.isDebugEnabled()) {
            log.debug("OAuthTokenSessionMapping Event Handler is enabled");
        }
        context.getBundleContext().registerService(OAuthAdminServiceImpl.class.getName(), oauthAdminService, null);
        if (log.isDebugEnabled()) {
            log.debug("Identity OAuth bundle is activated");
        }
    } catch (Throwable e) {
        String errMsg = "Error occurred while activating OAuth Service Component";
        log.error(errMsg, e);
        throw new RuntimeException(errMsg, e);
    }
}
Also used : OAuth2Service(org.wso2.carbon.identity.oauth2.OAuth2Service) IdentityOauthEventHandler(org.wso2.carbon.identity.oauth.listener.IdentityOauthEventHandler) AbstractEventHandler(org.wso2.carbon.identity.event.handler.AbstractEventHandler) IdentityOathEventListener(org.wso2.carbon.identity.oauth.listener.IdentityOathEventListener) OAuthAdminServiceImpl(org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) OAuthTokenSessionMappingEventHandler(org.wso2.carbon.identity.oauth.listener.OAuthTokenSessionMappingEventHandler)

Example 2 with OAuthAdminServiceImpl

use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthAdminServiceImplTest method testValidateAudiencesWithValidAudiences.

@Test(description = "Test validating invalid audiences", dataProvider = "validAudienceDataProvider")
public void testValidateAudiencesWithValidAudiences(String[] validaAudience) throws Exception {
    OAuthConsumerAppDTO appDTO = new OAuthConsumerAppDTO();
    appDTO.setAudiences(validaAudience);
    OAuthAdminServiceImpl oAuthAdminService = new OAuthAdminServiceImpl();
    invokeMethod(oAuthAdminService, "validateAudiences", appDTO);
}
Also used : OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 3 with OAuthAdminServiceImpl

use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthAdminServiceImplTest method testGetAllOAuthApplicationData.

@Test
public void testGetAllOAuthApplicationData() throws Exception {
    String username = "Moana";
    int tenantId = MultitenantConstants.SUPER_TENANT_ID;
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super");
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(tenantId);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(username);
    OAuthAppDO app = buildDummyOAuthAppDO(username);
    when(oAuthAppDAO.getOAuthConsumerAppsOfUser(username, tenantId)).thenReturn(new OAuthAppDO[] { app });
    whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
    OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
    OAuthConsumerAppDTO[] oAuthConsumerApps = oAuthAdminServiceImpl.getAllOAuthApplicationData();
    Assert.assertTrue((oAuthConsumerApps.length == 1), "OAuth consumer application count should be one.");
    assertAllAttributesOfConsumerAppDTO(oAuthConsumerApps[0], app);
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 4 with OAuthAdminServiceImpl

use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthAdminServiceImplTest method testGetAllOAuthApplicationData.

@Test(dataProvider = "getDataForAllOAuthApplicationData")
public void testGetAllOAuthApplicationData(String userName) throws Exception {
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantDomain("carbon.super");
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setTenantId(-1234);
    PrivilegedCarbonContext.getThreadLocalCarbonContext().setUsername(userName);
    whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    AuthenticatedUser authenticatedUser = new AuthenticatedUser();
    oAuthAppDO.setApplicationName("testapp1");
    oAuthAppDO.setUser(authenticatedUser);
    authenticatedUser.setUserName(userName);
    when(oAuthAppDAO.getOAuthConsumerAppsOfUser(userName, -1234)).thenReturn(new OAuthAppDO[] { oAuthAppDO });
    OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
    try {
        OAuthConsumerAppDTO[] allOAuthApplicationData = oAuthAdminServiceImpl.getAllOAuthApplicationData();
        Assert.assertNotNull(allOAuthApplicationData);
        Assert.assertEquals(allOAuthApplicationData.length, 1);
        Assert.assertEquals(allOAuthApplicationData[0].getApplicationName(), "testapp1");
    } catch (IdentityOAuthAdminException allOAuthApplicationData) {
        Assert.assertEquals(allOAuthApplicationData.getMessage(), "User not logged in to get all registered OAuth Applications.");
    }
}
Also used : OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 5 with OAuthAdminServiceImpl

use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuthAdminServiceImplTest method testRevokeIssuedTokensByApplication.

@Test
public void testRevokeIssuedTokensByApplication() throws Exception {
    String userId = UUID.randomUUID().toString();
    String consumerKey = UUID.randomUUID().toString();
    String accessToken = UUID.randomUUID().toString();
    String refreshToken = UUID.randomUUID().toString();
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    oAuthAppDO.setOauthConsumerKey(consumerKey);
    oAuthAppDO.setApplicationName("some-user-name");
    when(oAuthAppDAO.getAppInformation(consumerKey)).thenReturn(oAuthAppDO);
    PowerMockito.whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
    AuthenticatedUser user = buildUser("some-user-name");
    user.setUserId(userId);
    user.setFederatedIdPName(TestConstants.LOCAL_IDP);
    OAuthAppRevocationRequestDTO oAuthAppRevocationRequestDTO = new OAuthAppRevocationRequestDTO();
    oAuthAppRevocationRequestDTO.setConsumerKey(consumerKey);
    AccessTokenDO dummyToken = new AccessTokenDO();
    dummyToken.setAccessToken(accessToken);
    dummyToken.setRefreshToken(refreshToken);
    dummyToken.setAuthzUser(user);
    dummyToken.setScope(new String[] { "openid" });
    Set<AccessTokenDO> accessTokenDOSet = new HashSet<>();
    accessTokenDOSet.add(dummyToken);
    OAuthTokenPersistenceFactory tokenPersistenceFactory = OAuthTokenPersistenceFactory.getInstance();
    TokenManagementDAOImpl mockTokenManagementDAOImpl = mock(TokenManagementDAOImpl.class);
    Whitebox.setInternalState(tokenPersistenceFactory, "managementDAO", mockTokenManagementDAOImpl);
    AccessTokenDAO mockAccessTokenDAO = mock(AccessTokenDAO.class);
    Whitebox.setInternalState(tokenPersistenceFactory, "tokenDAO", mockAccessTokenDAO);
    when(mockAccessTokenDAO.getActiveAcessTokenDataByConsumerKey(anyString())).thenReturn(accessTokenDOSet);
    OAuthRevocationResponseDTO expectedOAuthRevocationResponseDTO = new OAuthRevocationResponseDTO();
    expectedOAuthRevocationResponseDTO.setError(false);
    ApplicationManagementService appMgtService = mock(ApplicationManagementService.class);
    when(appMgtService.getServiceProviderNameByClientId(consumerKey, INBOUND_AUTH2_TYPE, user.getTenantDomain())).thenReturn(oAuthAppDO.getApplicationName());
    OAuth2ServiceComponentHolder.setApplicationMgtService(appMgtService);
    OAuthAdminServiceImpl oAuthAdminServiceImpl = spy(new OAuthAdminServiceImpl());
    doNothing().when(oAuthAdminServiceImpl, "triggerPreApplicationTokenRevokeListeners", anyObject());
    doNothing().when(oAuthAdminServiceImpl, "triggerPostApplicationTokenRevokeListeners", anyObject(), anyObject(), anyObject());
    OAuthRevocationResponseDTO actualOAuthRevocationResponseDTO = oAuthAdminServiceImpl.revokeIssuedTokensByApplication(oAuthAppRevocationRequestDTO);
    Assert.assertEquals(actualOAuthRevocationResponseDTO.isError(), expectedOAuthRevocationResponseDTO.isError());
}
Also used : TokenManagementDAOImpl(org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl) OAuthRevocationResponseDTO(org.wso2.carbon.identity.oauth.dto.OAuthRevocationResponseDTO) AccessTokenDAO(org.wso2.carbon.identity.oauth2.dao.AccessTokenDAO) Matchers.anyString(org.mockito.Matchers.anyString) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OAuthTokenPersistenceFactory(org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuthAppRevocationRequestDTO(org.wso2.carbon.identity.oauth.dto.OAuthAppRevocationRequestDTO) ApplicationManagementService(org.wso2.carbon.identity.application.mgt.ApplicationManagementService) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)13 Test (org.testng.annotations.Test)13 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)13 OAuthConsumerAppDTO (org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO)10 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)9 Matchers.anyString (org.mockito.Matchers.anyString)7 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)5 OAuthAdminServiceImpl (org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl)3 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)2 OAuthAppDAO (org.wso2.carbon.identity.oauth.dao.OAuthAppDAO)2 OAuthAppRevocationRequestDTO (org.wso2.carbon.identity.oauth.dto.OAuthAppRevocationRequestDTO)2 OAuthRevocationResponseDTO (org.wso2.carbon.identity.oauth.dto.OAuthRevocationResponseDTO)2 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 Properties (java.util.Properties)1 GrantType (org.wso2.carbon.identity.api.server.application.management.v1.GrantType)1 GrantTypeMetaData (org.wso2.carbon.identity.api.server.application.management.v1.GrantTypeMetaData)1 MetadataProperty (org.wso2.carbon.identity.api.server.application.management.v1.MetadataProperty)1