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());
}
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());
}
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());
}
Aggregations