Search in sources :

Example 1 with OAuthCache

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

the class ResponseTypeHandlerUtil method addTokenToCache.

private static void addTokenToCache(OAuthCacheKey cacheKey, AccessTokenDO tokenBean) {
    OAuthCache.getInstance().addToCache(cacheKey, tokenBean);
    // Adding AccessTokenDO to improve validation performance
    OAuthCacheKey accessTokenCacheKey = new OAuthCacheKey(tokenBean.getAccessToken());
    OAuthCache.getInstance().addToCache(accessTokenCacheKey, tokenBean);
    if (log.isDebugEnabled()) {
        log.debug("Access token info was added to the cache for cache key : " + cacheKey.getCacheKeyString());
        if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
            log.debug("Access token was added to OAuthCache for cache key : " + accessTokenCacheKey.getCacheKeyString());
        }
    }
}
Also used : OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)

Example 2 with OAuthCache

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

the class OAuthAdminServiceImpl method updateAndRetrieveOauthSecretKey.

/**
 * Regenerate consumer secret for the application and retrieve application details.
 *
 * @param consumerKey Consumer key for the application.
 * @return OAuthConsumerAppDTO OAuth application details.
 * @throws IdentityOAuthAdminException Error while regenerating the consumer secret.
 */
public OAuthConsumerAppDTO updateAndRetrieveOauthSecretKey(String consumerKey) throws IdentityOAuthAdminException {
    Properties properties = new Properties();
    String newSecret = OAuthUtil.getRandomNumber();
    properties.setProperty(OAuthConstants.OAUTH_APP_NEW_SECRET_KEY, newSecret);
    properties.setProperty(OAuthConstants.ACTION_PROPERTY_KEY, OAuthConstants.ACTION_REGENERATE);
    properties.setProperty(OAuthConstants.OAUTH_APP_NEW_STATE, APP_STATE_ACTIVE);
    AppInfoCache.getInstance().clearCacheEntry(consumerKey);
    updateAppAndRevokeTokensAndAuthzCodes(consumerKey, properties);
    if (LOG.isDebugEnabled()) {
        LOG.debug("Client Secret for OAuth app with consumerKey: " + consumerKey + " updated in OAuthCache.");
    }
    OAuthConsumerAppDTO updatedApplication = getOAuthApplicationData(consumerKey);
    updatedApplication.setOauthConsumerSecret(newSecret);
    return updatedApplication;
}
Also used : OAuthConsumerAppDTO(org.wso2.carbon.identity.oauth.dto.OAuthConsumerAppDTO) OAuth2Util.buildScopeString(org.wso2.carbon.identity.oauth2.util.OAuth2Util.buildScopeString) Properties(java.util.Properties)

Example 3 with OAuthCache

use of org.wso2.carbon.identity.oauth.cache.OAuthCache 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 4 with OAuthCache

use of org.wso2.carbon.identity.oauth.cache.OAuthCache 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 5 with OAuthCache

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

the class OAuth2Util method addTokenDOtoCache.

/**
 * There are cases where we store an 'alias' of the token returned to the client as the token inside IS.
 * For example, in the case of JWT access tokens we store the 'jti' claim in the database instead of the
 * actual JWT. Therefore we need to cache an AccessTokenDO with the stored token identifier.
 *
 * @param newTokenBean token DO to be added to the cache.
 */
public static void addTokenDOtoCache(AccessTokenDO newTokenBean) throws IdentityOAuth2Exception {
    OauthTokenIssuer tokenIssuer = null;
    try {
        tokenIssuer = OAuth2Util.getOAuthTokenIssuerForOAuthApp(newTokenBean.getConsumerKey());
        String tokenAlias = tokenIssuer.getAccessTokenHash(newTokenBean.getAccessToken());
        OAuthCacheKey accessTokenCacheKey = new OAuthCacheKey(tokenAlias);
        AccessTokenDO tokenDO = AccessTokenDO.clone(newTokenBean);
        tokenDO.setAccessToken(tokenAlias);
        OAuthCache.getInstance().addToCache(accessTokenCacheKey, tokenDO);
        if (log.isDebugEnabled()) {
            if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
                log.debug("Access token DO was added to OAuthCache with cache key: " + accessTokenCacheKey.getCacheKeyString());
            } else {
                log.debug("Access token DO was added to OAuthCache");
            }
        }
    } catch (OAuthSystemException e) {
        if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
            throw new IdentityOAuth2Exception("Error while getting the token alias from token issuer: " + tokenIssuer.toString() + " for the token: " + newTokenBean.getAccessToken(), e);
        } else {
            throw new IdentityOAuth2Exception("Error while getting the token alias from token issuer: " + tokenIssuer.toString(), e);
        }
    } catch (InvalidOAuthClientException e) {
        if (IdentityUtil.isTokenLoggable(IdentityConstants.IdentityTokens.ACCESS_TOKEN)) {
            throw new IdentityOAuth2Exception("Error while getting the token issuer for the token: " + newTokenBean.getAccessToken(), e);
        } else {
            throw new IdentityOAuth2Exception("Error while getting the token issuer", e);
        }
    }
}
Also used : OauthTokenIssuer(org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer) AccessTokenDO(org.wso2.carbon.identity.oauth2.model.AccessTokenDO) OAuthCacheKey(org.wso2.carbon.identity.oauth.cache.OAuthCacheKey) IdentityOAuth2Exception(org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception) OAuthSystemException(org.apache.oltu.oauth2.common.exception.OAuthSystemException) InvalidOAuthClientException(org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)

Aggregations

OAuthCacheKey (org.wso2.carbon.identity.oauth.cache.OAuthCacheKey)18 OAuthCache (org.wso2.carbon.identity.oauth.cache.OAuthCache)11 AccessTokenDO (org.wso2.carbon.identity.oauth2.model.AccessTokenDO)11 Test (org.testng.annotations.Test)10 IdentityOAuth2Exception (org.wso2.carbon.identity.oauth2.IdentityOAuth2Exception)6 UserIdNotFoundException (org.wso2.carbon.identity.application.authentication.framework.exception.UserIdNotFoundException)5 CacheEntry (org.wso2.carbon.identity.oauth.cache.CacheEntry)5 InvalidOAuthClientException (org.wso2.carbon.identity.oauth.common.exception.InvalidOAuthClientException)4 OAuthAppDO (org.wso2.carbon.identity.oauth.dao.OAuthAppDO)4 OauthTokenIssuer (org.wso2.carbon.identity.oauth2.token.OauthTokenIssuer)4 OAuthSystemException (org.apache.oltu.oauth2.common.exception.OAuthSystemException)3 BeforeTest (org.testng.annotations.BeforeTest)3 OAuthServerConfiguration (org.wso2.carbon.identity.oauth.config.OAuthServerConfiguration)3 AuthzCodeDO (org.wso2.carbon.identity.oauth2.model.AuthzCodeDO)3 OAuthTokenReqMessageContext (org.wso2.carbon.identity.oauth2.token.OAuthTokenReqMessageContext)3 User (org.wso2.carbon.identity.application.common.model.User)2 AppInfoCache (org.wso2.carbon.identity.oauth.cache.AppInfoCache)2 AuthorizationGrantCacheKey (org.wso2.carbon.identity.oauth.cache.AuthorizationGrantCacheKey)2 OAuthCallbackManager (org.wso2.carbon.identity.oauth.callback.OAuthCallbackManager)2 OAuthAppDAO (org.wso2.carbon.identity.oauth.dao.OAuthAppDAO)2