Search in sources :

Example 1 with TokenManagementDAOImpl

use of org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2ServiceTest method testRevokeTokenByOAuthClientWithRefreshToken.

@Test(dataProvider = "RefreshTokenWithDifferentFlows")
public void testRevokeTokenByOAuthClientWithRefreshToken(String grantType, String tokenState) throws Exception {
    setUpRevokeToken();
    mockStatic(IdentityTenantUtil.class);
    when(IdentityTenantUtil.getTenantId(anyString())).thenReturn(-1234);
    RefreshTokenValidationDataDO refreshTokenValidationDataDO = new RefreshTokenValidationDataDO();
    refreshTokenValidationDataDO.setGrantType(GrantType.REFRESH_TOKEN.toString());
    refreshTokenValidationDataDO.setAccessToken("testAccessToken");
    refreshTokenValidationDataDO.setAuthorizedUser(authenticatedUser);
    refreshTokenValidationDataDO.setScope(new String[] { "test" });
    refreshTokenValidationDataDO.setRefreshTokenState(tokenState);
    refreshTokenValidationDataDO.setTokenBindingReference("dummyReference");
    OAuthTokenPersistenceFactory oAuthTokenPersistenceFactory = OAuthTokenPersistenceFactory.getInstance();
    TokenManagementDAOImpl mockTokenManagementDAOImpl = mock(TokenManagementDAOImpl.class);
    Whitebox.setInternalState(oAuthTokenPersistenceFactory, "managementDAO", mockTokenManagementDAOImpl);
    AccessTokenDAOImpl mockAccessTokenDAOImpl = mock(AccessTokenDAOImpl.class);
    Whitebox.setInternalState(oAuthTokenPersistenceFactory, "tokenDAO", mockAccessTokenDAOImpl);
    when(mockTokenManagementDAOImpl.validateRefreshToken(anyObject(), anyObject())).thenReturn(refreshTokenValidationDataDO);
    OAuthRevocationRequestDTO revokeRequestDTO = new OAuthRevocationRequestDTO();
    revokeRequestDTO.setConsumerKey("testConsumerKey");
    revokeRequestDTO.setToken("testToken");
    revokeRequestDTO.setTokenType(grantType);
    OAuthClientAuthnContext oAuthClientAuthnContext = new OAuthClientAuthnContext();
    oAuthClientAuthnContext.setAuthenticated(true);
    oAuthClientAuthnContext.setErrorCode("dummyErrorCode");
    revokeRequestDTO.setOauthClientAuthnContext(oAuthClientAuthnContext);
    assertFalse(oAuth2Service.revokeTokenByOAuthClient(revokeRequestDTO).isError());
}
Also used : OAuthRevocationRequestDTO(org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO) OAuthTokenPersistenceFactory(org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory) TokenManagementDAOImpl(org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl) RefreshTokenValidationDataDO(org.wso2.carbon.identity.oauth2.model.RefreshTokenValidationDataDO) AccessTokenDAOImpl(org.wso2.carbon.identity.oauth2.dao.AccessTokenDAOImpl) OAuthClientAuthnContext(org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 2 with TokenManagementDAOImpl

use of org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl 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)

Example 3 with TokenManagementDAOImpl

use of org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl in project identity-inbound-auth-oauth by wso2-extensions.

the class OAuth2ServiceTest method testRevokeTokenByOAuthClientWithAccessToken.

@Test
public void testRevokeTokenByOAuthClientWithAccessToken() throws Exception {
    setUpRevokeToken();
    AccessTokenDO accessTokenDO = getAccessToken();
    TokenBinding tokenBinding = new TokenBinding();
    tokenBinding.setBindingReference("dummyReference");
    accessTokenDO.setTokenBinding(tokenBinding);
    when(OAuth2Util.findAccessToken(anyString(), anyBoolean())).thenReturn(accessTokenDO);
    OAuthTokenPersistenceFactory oAuthTokenPersistenceFactory = OAuthTokenPersistenceFactory.getInstance();
    TokenManagementDAOImpl mockTokenManagementDAOImpl = mock(TokenManagementDAOImpl.class);
    Whitebox.setInternalState(oAuthTokenPersistenceFactory, "managementDAO", mockTokenManagementDAOImpl);
    AccessTokenDAO mockAccessTokenDAO = mock(AccessTokenDAO.class);
    Whitebox.setInternalState(oAuthTokenPersistenceFactory, "tokenDAO", mockAccessTokenDAO);
    when(mockAccessTokenDAO.getAccessToken(anyString(), anyBoolean())).thenReturn(accessTokenDO);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    when(OAuth2Util.getAppInformationByClientId(anyString())).thenReturn(oAuthAppDO);
    OAuthRevocationRequestDTO revokeRequestDTO = getOAuthRevocationRequestDTO();
    oAuth2Service.revokeTokenByOAuthClient(revokeRequestDTO);
    assertFalse(oAuth2Service.revokeTokenByOAuthClient(revokeRequestDTO).isError());
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) TokenBinding(org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding) OAuthRevocationRequestDTO(org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO) OAuthTokenPersistenceFactory(org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory) TokenManagementDAOImpl(org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) AccessTokenDAO(org.wso2.carbon.identity.oauth2.dao.AccessTokenDAO) 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)3 Test (org.testng.annotations.Test)3 OAuthTokenPersistenceFactory (org.wso2.carbon.identity.oauth2.dao.OAuthTokenPersistenceFactory)3 TokenManagementDAOImpl (org.wso2.carbon.identity.oauth2.dao.TokenManagementDAOImpl)3 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)3 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)2 AccessTokenDAO (org.wso2.carbon.identity.oauth2.dao.AccessTokenDAO)2 OAuthRevocationRequestDTO (org.wso2.carbon.identity.oauth2.dto.OAuthRevocationRequestDTO)2 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)2 HashSet (java.util.HashSet)1 Matchers.anyString (org.mockito.Matchers.anyString)1 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)1 ApplicationManagementService (org.wso2.carbon.identity.application.mgt.ApplicationManagementService)1 OAuthAppRevocationRequestDTO (org.wso2.carbon.identity.oauth.dto.OAuthAppRevocationRequestDTO)1 OAuthRevocationResponseDTO (org.wso2.carbon.identity.oauth.dto.OAuthRevocationResponseDTO)1 OAuthClientAuthnContext (org.wso2.carbon.identity.oauth2.bean.OAuthClientAuthnContext)1 AccessTokenDAOImpl (org.wso2.carbon.identity.oauth2.dao.AccessTokenDAOImpl)1 RefreshTokenValidationDataDO (org.wso2.carbon.identity.oauth2.model.RefreshTokenValidationDataDO)1 TokenBinding (org.wso2.carbon.identity.oauth2.token.bindings.TokenBinding)1