Search in sources :

Example 1 with AppInfoCache

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

the class OAuthApplicationMgtListener method removeEntriesFromCache.

private void removeEntriesFromCache(Set<String> consumerKeys) throws IdentityOAuth2Exception {
    if (isNotEmpty(consumerKeys)) {
        Set<AccessTokenDO> accessTokenDOSet = new HashSet<>();
        Set<AuthzCodeDO> authzCodeDOSet = new HashSet<>();
        AppInfoCache appInfoCache = AppInfoCache.getInstance();
        for (String oauthKey : consumerKeys) {
            accessTokenDOSet.addAll(OAuthTokenPersistenceFactory.getInstance().getAccessTokenDAO().getActiveTokenSetWithTokenIdByConsumerKeyForOpenidScope(oauthKey));
            authzCodeDOSet.addAll(OAuthTokenPersistenceFactory.getInstance().getAuthorizationCodeDAO().getAuthorizationCodeDOSetByConsumerKeyForOpenidScope(oauthKey));
            // Remove client credential from AppInfoCache
            appInfoCache.clearCacheEntry(oauthKey);
            OAuthCache.getInstance().clearCacheEntry(new OAuthCacheKey(oauthKey));
        }
        if (isNotEmpty(accessTokenDOSet)) {
            clearCacheEntriesAgainstToken(accessTokenDOSet);
        }
        if (isNotEmpty(authzCodeDOSet)) {
            clearCacheEntriesAgainstAuthzCode(authzCodeDOSet);
        }
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) HashSet(java.util.HashSet)

Example 2 with AppInfoCache

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

the class CodeResponseTypeHandlerTest method testIssue.

@Test(dataProvider = "IdpIDColumnAvailabilityDataProvider")
public void testIssue(boolean isIDPIdColumnEnabled) throws Exception {
    OAuth2ServiceComponentHolder.setIDPIdColumnEnabled(isIDPIdColumnEnabled);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    oAuthAppDO.setGrantTypes("implicit");
    oAuthAppDO.setOauthConsumerKey(TEST_CONSUMER_KEY);
    oAuthAppDO.setState("active");
    AuthenticatedUser user = new AuthenticatedUser();
    user.setUserStoreDomain("PRIMARY");
    user.setUserName("testUser");
    user.setFederatedIdPName(TestConstants.LOCAL_IDP);
    oAuthAppDO.setUser(user);
    oAuthAppDO.setApplicationName("testApp");
    AppInfoCache appInfoCache = AppInfoCache.getInstance();
    appInfoCache.addToCache(TEST_CONSUMER_KEY, oAuthAppDO);
    CodeResponseTypeHandler codeResponseTypeHandler = new CodeResponseTypeHandler();
    codeResponseTypeHandler.init();
    OAuth2AuthorizeRespDTO oAuth2AuthorizeRespDTO = codeResponseTypeHandler.issue(authAuthzReqMessageContext);
    Assert.assertNotNull(oAuth2AuthorizeRespDTO.getAuthorizationCode(), "Access token not Authorization code");
    Assert.assertEquals(oAuth2AuthorizeRespDTO.getCallbackURI(), TEST_CALLBACK_URL, "Callback url not set");
}
Also used : AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) OAuth2AuthorizeRespDTO(org.wso2.carbon.identity.oauth2.dto.OAuth2AuthorizeRespDTO) AuthenticatedUser(org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser) Test(org.testng.annotations.Test)

Example 3 with AppInfoCache

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

the class AuthorizationCodeGrantHandlerTest method testValidateGrantException.

@Test(dataProvider = "buildErrorTokenRequestMessageContext")
public void testValidateGrantException(Object tokenRequestMessageContext, Object authzCode, String clientId, boolean pkceValid, long timestamp, String expectedError) throws Exception {
    AuthzCodeDO authzCodeDO = (AuthzCodeDO) authzCode;
    WhiteboxImpl.setInternalState(authorizationCodeGrantHandler, "cacheEnabled", true);
    OAuthCache oAuthCache = mock(OAuthCache.class);
    when(OAuthCache.getInstance()).thenReturn(oAuthCache);
    WhiteboxImpl.setInternalState(authorizationCodeGrantHandler, "oauthCache", oAuthCache);
    OAuthTokenReqMessageContext tokReqMsgCtx = (OAuthTokenReqMessageContext) tokenRequestMessageContext;
    oAuthServerConfiguration = mock(OAuthServerConfiguration.class);
    TokenPersistenceProcessor tokenPersistenceProcessor = mock(TokenPersistenceProcessor.class);
    when(OAuthServerConfiguration.getInstance()).thenReturn(oAuthServerConfiguration);
    when(oAuthServerConfiguration.getPersistenceProcessor()).thenReturn(tokenPersistenceProcessor);
    OAuthAppDAO oAuthAppDAO = mock(OAuthAppDAO.class);
    OAuthAppDO oAuthAppDO = new OAuthAppDO();
    whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
    when(oAuthAppDAO.getAppInformation(CLIENT_ID_VALUE)).thenReturn(oAuthAppDO);
    when(oAuthAppDAO.getAppInformation(INVALID_CLIENT)).thenThrow(new InvalidOAuthClientException("Error"));
    AppInfoCache appInfoCache = mock(AppInfoCache.class);
    when(AppInfoCache.getInstance()).thenReturn(appInfoCache);
    doNothing().when(appInfoCache).addToCache(anyString(), any(OAuthAppDO.class));
    spy(OAuth2Util.class);
    doReturn(pkceValid).when(OAuth2Util.class, "validatePKCE", anyString(), anyString(), anyString(), any(OAuthAppDO.class));
    try {
        authorizationCodeGrantHandler.validateGrant(tokReqMsgCtx);
        fail("Expected exception not thrown");
    } catch (IdentityOAuth2Exception e) {
        assertTrue(e.getMessage().contains(expectedError), "Expected error message with '" + expectedError + "'");
    }
}
Also used : OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthCache(org.wso2.carbon.identity.oauth.cache.OAuthCache) OAuthTokenReqMessageContext(org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext) OAuthServerConfiguration(org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration) TokenPersistenceProcessor(org.wso2.carbon.identity.oauth.tokenprocessor.TokenPersistenceProcessor) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 4 with AppInfoCache

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

the class OAuth2UtilTest method testAuthenticateClientWithHashPersistenceProcessor.

@Test(dataProvider = "AuthenticateClient")
public void testAuthenticateClientWithHashPersistenceProcessor(Object cacheResult, String clientSecretInDB, boolean expectedResult) throws Exception {
    OAuthAppDO appDO = new OAuthAppDO();
    appDO.setOauthConsumerKey(clientId);
    appDO.setOauthConsumerSecret(clientSecretInDB);
    // Mock the cache result
    AppInfoCache appInfoCache = mock(AppInfoCache.class);
    when(appInfoCache.getValueFromCache(clientId)).thenReturn((OAuthAppDO) cacheResult);
    mockStatic(AppInfoCache.class);
    when(AppInfoCache.getInstance()).thenReturn(appInfoCache);
    // Mock the DB result
    OAuthAppDAO oAuthAppDAO = mock(OAuthAppDAO.class);
    when(oAuthAppDAO.getAppInformation(clientId)).thenReturn(appDO);
    PowerMockito.whenNew(OAuthAppDAO.class).withNoArguments().thenReturn(oAuthAppDAO);
    TokenPersistenceProcessor hashingProcessor = mock(HashingPersistenceProcessor.class);
    when(hashingProcessor.getProcessedClientSecret(clientSecret)).thenReturn(clientSecret);
    when(oauthServerConfigurationMock.isClientSecretHashEnabled()).thenReturn(true);
    when(oauthServerConfigurationMock.getPersistenceProcessor()).thenReturn(hashingProcessor);
    assertEquals(OAuth2Util.authenticateClient(clientId, clientSecret), expectedResult);
}
Also used : AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDAO(org.wso2.carbon.identity.oauth.dao.OAuthAppDAO) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) TokenPersistenceProcessor(org.wso2.carbon.identity.oauth.tokenprocessor.TokenPersistenceProcessor) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Example 5 with AppInfoCache

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

the class OAuth2UtilTest method testGetClientSecret.

@Test
public void testGetClientSecret() throws Exception {
    OAuthAppDO appDO = new OAuthAppDO();
    appDO.setOauthConsumerKey(clientId);
    appDO.setOauthConsumerSecret(clientSecret);
    AppInfoCache appInfoCache = mock(AppInfoCache.class);
    when(appInfoCache.getValueFromCache(clientId)).thenReturn(appDO);
    mockStatic(AppInfoCache.class);
    when(AppInfoCache.getInstance()).thenReturn(appInfoCache);
    assertEquals(OAuth2Util.getClientSecret(clientId), appDO.getOauthConsumerSecret());
}
Also used : AppInfoCache(org.wso2.carbon.identity.oauth.cache.AppInfoCache) OAuthAppDO(org.wso2.carbon.identity.oauth.dao.OAuthAppDO) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) PowerMockIdentityBaseTest(org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)

Aggregations

AppInfoCache (org.wso2.carbon.identity.oauth.cache.AppInfoCache)15 Test (org.testng.annotations.Test)13 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)13 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)10 PowerMockIdentityBaseTest (org.wso2.carbon.identity.testutil.powermock.PowerMockIdentityBaseTest)9 OAuthAppDAO (org.wso2.carbon.identity.oauth.dao.OAuthAppDAO)7 PlainTextPersistenceProcessor (org.wso2.carbon.identity.oauth.tokenprocessor.PlainTextPersistenceProcessor)4 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)4 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)3 TokenPersistenceProcessor (org.wso2.carbon.identity.oauth.tokenprocessor.TokenPersistenceProcessor)3 AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)3 BeforeTest (org.testng.annotations.BeforeTest)2 AuthenticatedUser (org.wso2.carbon.identity.application.authentication.framework.model.AuthenticatedUser)2 OAuthCache (org.wso2.carbon.identity.oauth.cache.OAuthCache)2 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)2 OAuthServerConfiguration (org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration)2 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)2 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)2 HashSet (java.util.HashSet)1 Properties (java.util.Properties)1