Search in sources :

Example 26 with RedisConnection

use of org.maxkey.persistence.redis.RedisConnection in project MaxKey by dromara.

the class RedisTokenStore method removeRefreshToken.

public void removeRefreshToken(String tokenValue) {
    String refreshKey = (REFRESH + tokenValue);
    String refreshAuthKey = (REFRESH_AUTH + tokenValue);
    String refresh2AccessKey = (REFRESH_TO_ACCESS + tokenValue);
    String access2RefreshKey = (ACCESS_TO_REFRESH + tokenValue);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.delete(refreshKey);
        conn.delete(refreshAuthKey);
        conn.delete(refresh2AccessKey);
        conn.delete(access2RefreshKey);
        conn.closePipeline();
    } finally {
        conn.close();
    }
}
Also used : RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 27 with RedisConnection

use of org.maxkey.persistence.redis.RedisConnection in project MaxKey by dromara.

the class RedisTokenStore method removeAccessToken.

public void removeAccessToken(String tokenValue) {
    String accessKey = (ACCESS + tokenValue);
    String authKey = (AUTH + tokenValue);
    String accessToRefreshKey = (ACCESS_TO_REFRESH + tokenValue);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.getPipeline().get(accessKey);
        conn.getPipeline().get(authKey);
        conn.getPipeline().del(accessKey);
        conn.getPipeline().del(accessToRefreshKey);
        // Don't remove the refresh token - it's up to the caller to do that
        conn.getPipeline().del(authKey);
        List<Object> results = conn.closePipeline();
        String access = (String) results.get(0);
        String auth = (String) results.get(1);
        OAuth2Authentication authentication = ObjectTransformer.deserialize(auth);
        if (authentication != null) {
            String key = authenticationKeyGenerator.extractKey(authentication);
            String authToAccessKey = (AUTH_TO_ACCESS + key);
            String unameKey = (UNAME_TO_ACCESS + getApprovalKey(authentication));
            String clientId = (CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
            conn.openPipeline();
            conn.delete(authToAccessKey);
            conn.lRem(unameKey, 1, access);
            conn.lRem(clientId, 1, access);
            conn.delete(ACCESS + key);
            conn.closePipeline();
        }
    } finally {
        conn.close();
    }
}
Also used : OAuth2Authentication(org.maxkey.authz.oauth2.provider.OAuth2Authentication) RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 28 with RedisConnection

use of org.maxkey.persistence.redis.RedisConnection in project MaxKey by dromara.

the class RedisTokenStore method findTokensByClientIdAndUserName.

@Override
public Collection<OAuth2AccessToken> findTokensByClientIdAndUserName(String clientId, String userName) {
    String approvalKey = (UNAME_TO_ACCESS + getApprovalKey(clientId, userName));
    _logger.trace("approvalKey " + approvalKey);
    List<String> stringList = null;
    RedisConnection conn = getConnection();
    try {
        stringList = conn.lRange(approvalKey, 0, -1);
    } finally {
        conn.close();
    }
    if (stringList == null || stringList.size() == 0) {
        return Collections.<OAuth2AccessToken>emptySet();
    }
    List<OAuth2AccessToken> accessTokens = new ArrayList<OAuth2AccessToken>(stringList.size());
    for (String str : stringList) {
        // accessToken may expired
        OAuth2AccessToken accessToken = conn.getObject(str);
        accessTokens.add(accessToken);
    }
    return Collections.<OAuth2AccessToken>unmodifiableCollection(accessTokens);
}
Also used : OAuth2AccessToken(org.maxkey.authz.oauth2.common.OAuth2AccessToken) ArrayList(java.util.ArrayList) RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 29 with RedisConnection

use of org.maxkey.persistence.redis.RedisConnection in project MaxKey by dromara.

the class RedisTokenStore method readAccessToken.

@Override
public OAuth2AccessToken readAccessToken(String tokenValue) {
    RedisConnection conn = getConnection();
    try {
        String key = (ACCESS + tokenValue);
        OAuth2AccessToken accessToken = conn.getObject(key);
        return accessToken;
    } finally {
        conn.close();
    }
}
Also used : OAuth2AccessToken(org.maxkey.authz.oauth2.common.OAuth2AccessToken) RedisConnection(org.maxkey.persistence.redis.RedisConnection)

Example 30 with RedisConnection

use of org.maxkey.persistence.redis.RedisConnection in project MaxKey by dromara.

the class RedisTokenStore method storeAccessToken.

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    String accessKey = (ACCESS + token.getValue());
    String authKey = (AUTH + token.getValue());
    String authToAccessKey = (AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
    String approvalKey = (UNAME_TO_ACCESS + getApprovalKey(authentication));
    String clientId = (CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
    _logger.trace("accessKey " + accessKey);
    _logger.trace("authKey " + authKey);
    _logger.trace("authToAccessKey " + authToAccessKey);
    _logger.trace("approvalKey " + approvalKey);
    _logger.trace("clientId " + clientId);
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        conn.setObject(accessKey, token);
        conn.setObject(authKey, authentication);
        conn.setObject(authToAccessKey, token);
        if (!authentication.isClientOnly()) {
            conn.rPush(approvalKey, token);
        }
        conn.rPush(clientId, token);
        if (token.getExpiration() != null) {
            int seconds = token.getExpiresIn();
            conn.expire(accessKey, seconds);
            conn.expire(authKey, seconds);
            conn.expire(authToAccessKey, seconds);
            conn.expire(clientId, seconds);
            conn.expire(approvalKey, seconds);
        }
        OAuth2RefreshToken refreshToken = token.getRefreshToken();
        if (refreshToken != null && refreshToken.getValue() != null) {
            String refresh = (token.getRefreshToken().getValue());
            String auth = (token.getValue());
            String refreshToAccessKey = (REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
            _logger.trace("refreshToAccessKey " + refreshToAccessKey);
            conn.set(refreshToAccessKey, auth);
            String accessToRefreshKey = (ACCESS_TO_REFRESH + token.getValue());
            _logger.trace("accessToRefreshKey " + accessToRefreshKey);
            conn.set(accessToRefreshKey, refresh);
            if (refreshToken instanceof ExpiringOAuth2RefreshToken) {
                ExpiringOAuth2RefreshToken expiringRefreshToken = (ExpiringOAuth2RefreshToken) refreshToken;
                Date expiration = expiringRefreshToken.getExpiration();
                if (expiration != null) {
                    int seconds = Long.valueOf((expiration.getTime() - System.currentTimeMillis()) / 1000L).intValue();
                    conn.expire(refreshToAccessKey, seconds);
                    conn.expire(accessToRefreshKey, seconds);
                }
            }
        }
        conn.closePipeline();
    } finally {
        conn.close();
    }
}
Also used : ExpiringOAuth2RefreshToken(org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.maxkey.authz.oauth2.common.OAuth2RefreshToken) Date(java.util.Date) RedisConnection(org.maxkey.persistence.redis.RedisConnection) ExpiringOAuth2RefreshToken(org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken)

Aggregations

RedisConnection (org.maxkey.persistence.redis.RedisConnection)31 Ticket (org.maxkey.authz.cas.endpoint.ticket.Ticket)6 OAuth2AccessToken (org.maxkey.authz.oauth2.common.OAuth2AccessToken)4 ExpiringOAuth2RefreshToken (org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken)3 OAuth2Authentication (org.maxkey.authz.oauth2.provider.OAuth2Authentication)3 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 OAuth2RefreshToken (org.maxkey.authz.oauth2.common.OAuth2RefreshToken)2 OneTimePassword (org.maxkey.password.onetimepwd.OneTimePassword)2 DateTime (org.joda.time.DateTime)1