Search in sources :

Example 6 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)

Example 7 with OAuthAdminServiceImpl

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);
}
Also used : OAuthRevocationResponseDTO(org.wso2.carbon.identity.oauth.dto.OAuthRevocationResponseDTO) OAuthAppRevocationRequestDTO(org.wso2.carbon.identity.oauth.dto.OAuthAppRevocationRequestDTO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 8 with OAuthAdminServiceImpl

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);
}
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 9 with OAuthAdminServiceImpl

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);
    }
}
Also used : IdentityOAuthAdminException(org.wso2.carbon.identity.oauth.IdentityOAuthAdminException) User(org.wso2.carbon.identity.application.common.model.User) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) IdentityApplicationManagementException(org.wso2.carbon.identity.application.common.IdentityApplicationManagementException) OAuthAdminServiceImpl(org.wso2.carbon.identity.oauth.OAuthAdminServiceImpl) OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) InboundAuthenticationRequestConfig(org.wso2.carbon.identity.application.common.model.InboundAuthenticationRequestConfig) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)

Example 10 with OAuthAdminServiceImpl

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);
}
Also used : 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)

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