Search in sources :

Example 1 with AuthorizationGrantCache

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

the class OAuthApplicationMgtListener method clearCacheEntriesAgainstToken.

private void clearCacheEntriesAgainstToken(Set<AccessTokenDO> accessTokenDOSet) {
    for (AccessTokenDO accessTokenDo : accessTokenDOSet) {
        // Remove access token from AuthorizationGrantCache
        AuthorizationGrantCacheKey grantCacheKey = new AuthorizationGrantCacheKey(accessTokenDo.getAccessToken());
        AuthorizationGrantCache.getInstance().clearCacheEntryByTokenId(grantCacheKey, accessTokenDo.getTokenId());
        // Remove access token from OAuthCache
        OAuthCacheKey oauthCacheKey = new OAuthCacheKey(accessTokenDo.getAccessToken());
        CacheEntry oauthCacheEntry = OAuthCache.getInstance().getValueFromCache(oauthCacheKey);
        if (oauthCacheEntry != null) {
            OAuthCache.getInstance().clearCacheEntry(oauthCacheKey);
        }
    }
}
Also used : AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) AuthorizationGrantCacheKey(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey) CacheEntry(org.wso2.carbon.identity.oauth.cache.CacheEntry)

Example 2 with AuthorizationGrantCache

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

the class OAuthApplicationMgtListener method clearCacheEntriesAgainstAuthzCode.

private void clearCacheEntriesAgainstAuthzCode(Set<AuthzCodeDO> authzCodeDOSet) {
    for (AuthzCodeDO authzCodeDO : authzCodeDOSet) {
        // Remove authorization code from AuthorizationGrantCache
        AuthorizationGrantCacheKey grantCacheKey = new AuthorizationGrantCacheKey(authzCodeDO.getAuthorizationCode());
        AuthorizationGrantCache.getInstance().clearCacheEntryByCodeId(grantCacheKey, authzCodeDO.getAuthzCodeId());
        // Remove authorization code from OAuthCache
        OAuthCacheKey oauthCacheKey = new OAuthCacheKey(authzCodeDO.getAuthorizationCode());
        CacheEntry oauthCacheEntry = OAuthCache.getInstance().getValueFromCache(oauthCacheKey);
        if (oauthCacheEntry != null) {
            OAuthCache.getInstance().clearCacheEntry(oauthCacheKey);
        }
    }
}
Also used : OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) AuthorizationGrantCacheKey(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey) AuthzCodeDO(org.wso2.carbon.identity.oauth2.model.AuthzCodeDO) CacheEntry(org.wso2.carbon.identity.oauth.cache.CacheEntry)

Example 3 with AuthorizationGrantCache

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

the class AccessTokenIssuer method addUserAttributesAgainstAccessToken.

/**
 * Copies the cache entry against the authorization code and adds an entry against the access token. This is done to
 * reuse the calculated user claims for subsequent usages such as user info calls.
 *
 * @param tokenReqDTO
 * @param tokenRespDTO
 */
private void addUserAttributesAgainstAccessToken(OAuth2AccessTokenReqDTO tokenReqDTO, OAuth2AccessTokenRespDTO tokenRespDTO) {
    AuthorizationGrantCacheKey oldCacheKey = new AuthorizationGrantCacheKey(getAuthorizationCode(tokenReqDTO));
    // checking getUserAttributesId value of cacheKey before retrieve entry from cache as it causes to NPE
    if (oldCacheKey.getUserAttributesId() != null) {
        AuthorizationGrantCacheEntry authorizationGrantCacheEntry = AuthorizationGrantCache.getInstance().getValueFromCacheByCode(oldCacheKey);
        AuthorizationGrantCacheKey newCacheKey = new AuthorizationGrantCacheKey(tokenRespDTO.getAccessToken());
        if (authorizationGrantCacheEntry != null) {
            authorizationGrantCacheEntry.setTokenId(tokenRespDTO.getTokenId());
            if (log.isDebugEnabled()) {
                if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
                    log.debug("Adding AuthorizationGrantCache entry for the access token(hashed):" + DigestUtils.sha256Hex(newCacheKey.getUserAttributesId()));
                } else {
                    log.debug("Adding AuthorizationGrantCache entry for the access token");
                }
            }
            authorizationGrantCacheEntry.setValidityPeriod(TimeUnit.MILLISECONDS.toNanos(tokenRespDTO.getExpiresInMillis()));
            AuthorizationGrantCache.getInstance().addToCacheByToken(newCacheKey, authorizationGrantCacheEntry);
        }
    }
}
Also used : AuthorizationGrantCacheEntry(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry) AuthorizationGrantCacheKey(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey)

Example 4 with AuthorizationGrantCache

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

the class IdentityOathEventListenerTest method testDoPreSetUserClaimValueWithAuthorizationCode.

@Test
public void testDoPreSetUserClaimValueWithAuthorizationCode() throws Exception {
    Set<String> accessToken = new HashSet<>();
    accessToken.add("kljdslfjljdsfjldsflkdsjkfjdsjlkj");
    Set<String> authorizationCodes = new HashSet<String>();
    authorizationCodes.add("AUTHORIZATION_CODE");
    AuthorizationGrantCache authorizationGrantCache = mock(AuthorizationGrantCache.class);
    when(UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration())).thenReturn("DOMAIN_NAME");
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn("TENANT_DOMAIN_NAME");
    whenNew(AuthenticatedUser.class).withNoArguments().thenReturn(authenticatedUser);
    when(AuthorizationGrantCache.getInstance()).thenReturn(authorizationGrantCache);
    IdentityOathEventListener identityOathEventListener = new IdentityOathEventListener();
    assertTrue(identityOathEventListener.doPreSetUserClaimValue(username, claimUri, claimValue, profileName, userStoreManager));
}
Also used : AuthorizationGrantCache(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCache) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Example 5 with AuthorizationGrantCache

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

the class IdentityOathEventListenerTest method testDoPreSetUserClaimValue.

@Test
public void testDoPreSetUserClaimValue() throws Exception {
    Set<String> accessToken = new HashSet<>();
    accessToken.add("kljdslfjljdsfjldsflkdsjkfjdsjlkj");
    AuthorizationGrantCache authorizationGrantCache = mock(AuthorizationGrantCache.class);
    when(UserCoreUtil.getDomainName(userStoreManager.getRealmConfiguration())).thenReturn("DOMAIN_NAME");
    when(IdentityTenantUtil.getTenantDomain(anyInt())).thenReturn("TENANT_DOMAIN_NAME");
    whenNew(AuthenticatedUser.class).withNoArguments().thenReturn(authenticatedUser);
    when(AuthorizationGrantCache.getInstance()).thenReturn(authorizationGrantCache);
    IdentityOathEventListener identityOathEventListener = new IdentityOathEventListener();
    assertTrue(identityOathEventListener.doPreSetUserClaimValue(username, claimUri, claimValue, profileName, userStoreManager));
}
Also used : AuthorizationGrantCache(org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCache) Matchers.anyString(org.mockito.Matchers.anyString) HashSet(java.util.HashSet) Test(org.testng.annotations.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) IdentityBaseTest(org.wso2.carbon.identity.testutil.IdentityBaseTest)

Aggregations

AuthorizationGrantCacheKey (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey)4 HashSet (java.util.HashSet)3 Matchers.anyString (org.mockito.Matchers.anyString)3 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)3 Test (org.testng.annotations.Test)3 AuthorizationGrantCache (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCache)3 IdentityBaseTest (org.wso2.carbon.identity.testutil.IdentityBaseTest)3 CacheEntry (org.wso2.carbon.identity.oauth.cache.CacheEntry)2 OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Matchers.anyMap (org.mockito.Matchers.anyMap)1 AuthorizationGrantCacheEntry (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheEntry)1 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)1 AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)1