Search in sources :

Example 1 with OAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.OAuth2RefreshToken in project pig by pig-mesh.

the class PigRedisTokenStore method storeAccessToken.

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    byte[] serializedAccessToken = serialize(token);
    byte[] serializedAuth = serialize(authentication);
    byte[] accessKey = serializeKey(ACCESS + token.getValue());
    byte[] authKey = serializeKey(AUTH + token.getValue());
    byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
    byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
    byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
    try (RedisConnection conn = getConnection()) {
        conn.openPipeline();
        if (springDataRedis_2_0) {
            try {
                this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);
                this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);
                this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        } else {
            conn.set(accessKey, serializedAccessToken);
            conn.set(authKey, serializedAuth);
            conn.set(authToAccessKey, serializedAccessToken);
        }
        if (token.getExpiration() != null) {
            int seconds = token.getExpiresIn();
            long expirationTime = token.getExpiration().getTime();
            if (!authentication.isClientOnly()) {
                conn.zAdd(approvalKey, expirationTime, serializedAccessToken);
            }
            conn.zAdd(clientId, expirationTime, serializedAccessToken);
            conn.expire(accessKey, seconds);
            conn.expire(authKey, seconds);
            conn.expire(authToAccessKey, seconds);
            conn.expire(clientId, seconds);
            conn.expire(approvalKey, seconds);
        } else {
            conn.zAdd(clientId, -1, serializedAccessToken);
            if (!authentication.isClientOnly()) {
                conn.zAdd(approvalKey, -1, serializedAccessToken);
            }
        }
        OAuth2RefreshToken refreshToken = token.getRefreshToken();
        if (refreshToken != null && refreshToken.getValue() != null) {
            byte[] auth = serialize(token.getValue());
            byte[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
            if (springDataRedis_2_0) {
                try {
                    this.redisConnectionSet_2_0.invoke(conn, refreshToAccessKey, auth);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                conn.set(refreshToAccessKey, auth);
            }
            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.closePipeline();
    }
}
Also used : ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) RedisConnection(org.springframework.data.redis.connection.RedisConnection) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)

Example 2 with OAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.OAuth2RefreshToken in project orcid-member-services by ORCID.

the class OAuth2CookieHelper method createCookies.

/**
 * Create cookies using the provided values.
 *
 * @param request
 *            the request we are handling.
 * @param accessToken
 *            the access token and enclosed refresh token for our cookies.
 * @param rememberMe
 *            whether the user had originally checked "remember me".
 * @param result
 *            will get the resulting cookies set.
 */
public void createCookies(HttpServletRequest request, OAuth2AccessToken accessToken, boolean rememberMe, OAuth2Cookies result) {
    String domain = getCookieDomain(request);
    log.debug("creating cookies for domain {}", domain);
    Cookie accessTokenCookie = new Cookie(ACCESS_TOKEN_COOKIE, accessToken.getValue());
    setCookieProperties(accessTokenCookie, request.isSecure(), domain);
    log.debug("created access token cookie '{}'", accessTokenCookie.getName());
    OAuth2RefreshToken refreshToken = accessToken.getRefreshToken();
    Cookie refreshTokenCookie = createRefreshTokenCookie(refreshToken, rememberMe);
    setCookieProperties(refreshTokenCookie, request.isSecure(), domain);
    log.debug("created refresh token cookie '{}', age: {}", refreshTokenCookie.getName(), refreshTokenCookie.getMaxAge());
    result.setCookies(accessTokenCookie, refreshTokenCookie);
}
Also used : Cookie(javax.servlet.http.Cookie) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken)

Example 3 with OAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.OAuth2RefreshToken in project jmix by jmix-framework.

the class TokenRevoker method revokeRefreshToken.

@Nullable
public String revokeRefreshToken(String tokenValue, Authentication clientAuth) {
    OAuth2RefreshToken refreshToken = tokenStore.readRefreshToken(tokenValue);
    if (refreshToken != null) {
        OAuth2Authentication authToRevoke = tokenStore.readAuthenticationForRefreshToken(refreshToken);
        checkIfTokenIsIssuedToClient(clientAuth, authToRevoke);
        tokenStore.removeAccessTokenUsingRefreshToken(refreshToken);
        tokenStore.removeRefreshToken(refreshToken);
        log.debug("Successfully removed refresh token {} (and any associated access token).", tokenMasker.maskToken(refreshToken.getValue()));
        return refreshToken.getValue();
    }
    log.debug("No refresh token {} found in the token store.", tokenMasker.maskToken(tokenValue));
    return null;
}
Also used : OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) OAuth2Authentication(org.springframework.security.oauth2.provider.OAuth2Authentication) Nullable(javax.annotation.Nullable)

Example 4 with OAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.OAuth2RefreshToken in project ballcat by ballcat-projects.

the class CustomRedisTokenStore method readRefreshToken.

@Override
public OAuth2RefreshToken readRefreshToken(String tokenValue) {
    byte[] key = serializeKey(REFRESH + tokenValue);
    byte[] bytes = null;
    RedisConnection conn = getConnection();
    try {
        bytes = conn.get(key);
    } finally {
        conn.close();
    }
    OAuth2RefreshToken refreshToken = deserializeRefreshToken(bytes);
    return refreshToken;
}
Also used : ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) RedisConnection(org.springframework.data.redis.connection.RedisConnection)

Example 5 with OAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.OAuth2RefreshToken in project ballcat by ballcat-projects.

the class CustomRedisTokenStore method storeAccessToken.

@Override
public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
    byte[] serializedAccessToken = serialize(token);
    byte[] serializedAuth = serialize(authentication);
    byte[] accessKey = serializeKey(ACCESS + token.getValue());
    byte[] authKey = serializeKey(AUTH + token.getValue());
    byte[] authToAccessKey = serializeKey(AUTH_TO_ACCESS + authenticationKeyGenerator.extractKey(authentication));
    byte[] approvalKey = serializeKey(UNAME_TO_ACCESS + getApprovalKey(authentication));
    byte[] clientId = serializeKey(CLIENT_ID_TO_ACCESS + authentication.getOAuth2Request().getClientId());
    RedisConnection conn = getConnection();
    try {
        conn.openPipeline();
        if (springDataRedis_2_0) {
            try {
                this.redisConnectionSet_2_0.invoke(conn, accessKey, serializedAccessToken);
                this.redisConnectionSet_2_0.invoke(conn, authKey, serializedAuth);
                this.redisConnectionSet_2_0.invoke(conn, authToAccessKey, serializedAccessToken);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        } else {
            conn.set(accessKey, serializedAccessToken);
            conn.set(authKey, serializedAuth);
            conn.set(authToAccessKey, serializedAccessToken);
        }
        if (!authentication.isClientOnly()) {
            conn.sAdd(approvalKey, serializedAccessToken);
        }
        conn.sAdd(clientId, serializedAccessToken);
        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) {
            byte[] refresh = serialize(token.getRefreshToken().getValue());
            byte[] auth = serialize(token.getValue());
            byte[] refreshToAccessKey = serializeKey(REFRESH_TO_ACCESS + token.getRefreshToken().getValue());
            byte[] accessToRefreshKey = serializeKey(ACCESS_TO_REFRESH + token.getValue());
            if (springDataRedis_2_0) {
                try {
                    this.redisConnectionSet_2_0.invoke(conn, refreshToAccessKey, auth);
                    this.redisConnectionSet_2_0.invoke(conn, accessToRefreshKey, refresh);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
            } else {
                conn.set(refreshToAccessKey, auth);
                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.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) OAuth2RefreshToken(org.springframework.security.oauth2.common.OAuth2RefreshToken) SerializationException(org.springframework.data.redis.serializer.SerializationException) RedisConnection(org.springframework.data.redis.connection.RedisConnection) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)

Aggregations

OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)74 DefaultOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultOAuth2RefreshToken)34 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)27 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)24 Test (org.junit.Test)23 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)22 ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)19 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)15 Date (java.util.Date)14 Test (org.junit.jupiter.api.Test)12 CompositeExpiringOAuth2RefreshToken (org.cloudfoundry.identity.uaa.oauth.refresh.CompositeExpiringOAuth2RefreshToken)11 Authentication (org.springframework.security.core.Authentication)11 IsEmptyString.isEmptyString (org.hamcrest.text.IsEmptyString.isEmptyString)10 RedisConnection (org.springframework.data.redis.connection.RedisConnection)8 AuthorizationRequest (org.springframework.security.oauth2.provider.AuthorizationRequest)8 ExpiringOAuth2RefreshToken (org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken)7 BaseClientDetails (org.springframework.security.oauth2.provider.client.BaseClientDetails)7 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)6 DefaultExpiringOAuth2RefreshToken (org.maxkey.authz.oauth2.common.DefaultExpiringOAuth2RefreshToken)5 OAuth2RefreshToken (org.maxkey.authz.oauth2.common.OAuth2RefreshToken)5