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