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());
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testRevokeIssuedTokensByApplicationWithEmptyConsumerKey.
@Test
public void testRevokeIssuedTokensByApplicationWithEmptyConsumerKey() throws Exception {
OAuthAppRevocationRequestDTO oAuthAppRevocationRequestDTO = new OAuthAppRevocationRequestDTO();
oAuthAppRevocationRequestDTO.setConsumerKey("");
OAuthAdminServiceImpl oAuthAdminServiceImpl = spy(new OAuthAdminServiceImpl());
doNothing().when(oAuthAdminServiceImpl, "triggerPreApplicationTokenRevokeListeners", anyObject());
OAuthRevocationResponseDTO actualOAuthRevocationResponseDTO = oAuthAdminServiceImpl.revokeIssuedTokensByApplication(oAuthAppRevocationRequestDTO);
Assert.assertEquals(actualOAuthRevocationResponseDTO.getErrorCode(), OAuth2ErrorCodes.INVALID_REQUEST);
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testGetOAuthApplicationData.
@Test
public void testGetOAuthApplicationData() throws Exception {
String consumerKey = "some-consumer-key";
OAuthAppDO app = buildDummyOAuthAppDO("some-user-name");
when(oAuthAppDAO.getAppInformation(consumerKey)).thenReturn(app);
whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
OAuthConsumerAppDTO oAuthConsumerApp = oAuthAdminServiceImpl.getOAuthApplicationData(consumerKey);
assertAllAttributesOfConsumerAppDTO(oAuthConsumerApp, app);
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthApplicationMgtListener method doImportServiceProvider.
@Override
public void doImportServiceProvider(ServiceProvider serviceProvider) throws IdentityApplicationManagementException {
try {
if (serviceProvider.getInboundAuthenticationConfig() != null && serviceProvider.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs() != null) {
for (InboundAuthenticationRequestConfig authConfig : serviceProvider.getInboundAuthenticationConfig().getInboundAuthenticationRequestConfigs()) {
if (OAUTH.equals(authConfig.getInboundAuthType()) || OAUTH2.equals(authConfig.getInboundAuthType())) {
String inboundConfiguration = authConfig.getInboundConfiguration();
if (inboundConfiguration == null || "".equals(inboundConfiguration)) {
String errorMSg = String.format("No inbound configurations found for oauth in the " + "imported %s", serviceProvider.getApplicationName());
throw new IdentityApplicationManagementException(errorMSg);
}
User owner = serviceProvider.getOwner();
OAuthAppDO oAuthAppDO = marshelOAuthDO(authConfig.getInboundConfiguration(), serviceProvider.getApplicationName(), owner.getTenantDomain());
oAuthAppDO.setAppOwner(new AuthenticatedUser(owner));
OAuthConsumerAppDTO oAuthConsumerAppDTO = OAuthUtil.buildConsumerAppDTO(oAuthAppDO);
OAuthAppDAO dao = new OAuthAppDAO();
String oauthConsumerKey = oAuthConsumerAppDTO.getOauthConsumerKey();
boolean isExistingClient = dao.isDuplicateConsumer(oauthConsumerKey);
// Set the client secret before doing registering/updating the oauth app.
if (oAuthConsumerAppDTO.getOauthConsumerSecret() == null) {
if (isExistingClient) {
// For existing client, we fetch the existing client secret and set.
OAuthAppDO app = OAuth2Util.getAppInformationByClientId(oauthConsumerKey);
oAuthConsumerAppDTO.setOauthConsumerSecret(app.getOauthConsumerSecret());
} else {
oAuthConsumerAppDTO.setOauthConsumerSecret(OAuthUtil.getRandomNumber());
}
}
OAuthAdminServiceImpl oAuthAdminService = OAuthComponentServiceHolder.getInstance().getoAuthAdminService();
if (isExistingClient) {
oAuthAdminService.updateConsumerApplication(oAuthConsumerAppDTO);
} else {
oAuthAdminService.registerOAuthApplicationData(oAuthConsumerAppDTO);
}
return;
}
}
}
} catch (IdentityOAuthAdminException | InvalidOAuthClientException | IdentityOAuth2Exception e) {
String message = "Error occurred when importing OAuth inbound.";
throw handleException(message, e);
}
}
use of org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl in project identity-inbound-auth-oauth by wso2-extensions.
the class OAuthAdminServiceImplTest method testGetOAuthApplicationDataException.
@Test(dataProvider = "getAppInformationExceptions", expectedExceptions = IdentityOAuthAdminException.class)
public void testGetOAuthApplicationDataException(String exception) throws Exception {
String consumerKey = "invalid_consumer_key";
whenNew(OAuthAppDAO.class).withAnyArguments().thenReturn(oAuthAppDAO);
switch(exception) {
case "InvalidOAuthClientException":
when(oAuthAppDAO.getAppInformation(consumerKey)).thenThrow(InvalidOAuthClientException.class);
break;
case "IdentityOAuth2Exception":
when(oAuthAppDAO.getAppInformation(consumerKey)).thenThrow(IdentityOAuth2Exception.class);
}
OAuthAdminServiceImpl oAuthAdminServiceImpl = new OAuthAdminServiceImpl();
oAuthAdminServiceImpl.getOAuthApplicationData(consumerKey);
}
Aggregations