Search in sources :

Example 1 with ExpiringOAuth2RefreshToken

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

the class PigRedisTokenStore method storeRefreshToken.

@Override
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
    byte[] refreshKey = serializeKey(REFRESH + refreshToken.getValue());
    byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + refreshToken.getValue());
    byte[] serializedRefreshToken = serialize(refreshToken);
    try (RedisConnection conn = getConnection()) {
        conn.openPipeline();
        if (springDataRedis_2_0) {
            try {
                this.redisConnectionSet_2_0.invoke(conn, refreshKey, serializedRefreshToken);
                this.redisConnectionSet_2_0.invoke(conn, refreshAuthKey, serialize(authentication));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        } else {
            conn.set(refreshKey, serializedRefreshToken);
            conn.set(refreshAuthKey, serialize(authentication));
        }
        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(refreshKey, seconds);
                conn.expire(refreshAuthKey, seconds);
            }
        }
        conn.closePipeline();
    }
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)

Example 2 with ExpiringOAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken 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 3 with ExpiringOAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken 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)

Example 4 with ExpiringOAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken in project uaa by cloudfoundry.

the class RefreshTokenCreatorTest method refreshToken_ifIdTokenClaimsAreUnknown_omitsThem.

@Test
public void refreshToken_ifIdTokenClaimsAreUnknown_omitsThem() {
    // This is a backwards compatibility case when trying to construct a new refresh token from an old refresh
    // token issued before auth_time, amr, etc were included in the token claims. We can't show a value for the auth_time
    // because we don't know when the user authenticated.
    UaaUser user = new UaaUser(new UaaUserPrototype().withId("id").withEmail("spongebob@krustykrab.com").withUsername("spongebob").withOrigin("uaa"));
    Date authTime = null;
    HashSet<String> authenticationMethods = Sets.newHashSet();
    RefreshTokenRequestData refreshTokenRequestData = new RefreshTokenRequestData("refresh_token", Sets.newHashSet(), authenticationMethods, null, Sets.newHashSet(), "someclient", false, authTime, Sets.newHashSet(), Maps.newHashMap());
    ExpiringOAuth2RefreshToken refreshToken = refreshTokenCreator.createRefreshToken(user, refreshTokenRequestData, "abcdef");
    Map<String, Object> refreshClaims = UaaTokenUtils.getClaims(refreshToken.getValue());
    assertFalse(refreshClaims.containsKey(AUTH_TIME));
    assertFalse(refreshClaims.containsKey(AMR));
    assertFalse(refreshClaims.containsKey(ACR));
}
Also used : UaaUser(org.cloudfoundry.identity.uaa.user.UaaUser) UaaUserPrototype(org.cloudfoundry.identity.uaa.user.UaaUserPrototype) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken) Test(org.junit.Test)

Example 5 with ExpiringOAuth2RefreshToken

use of org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken in project XHuiCloud by sindaZeng.

the class XHuiRedisTokenStore method storeRefreshToken.

@Override
public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authentication authentication) {
    byte[] refreshKey = serializeKey(REFRESH + refreshToken.getValue());
    byte[] refreshAuthKey = serializeKey(REFRESH_AUTH + refreshToken.getValue());
    byte[] serializedRefreshToken = serialize(refreshToken);
    try (RedisConnection conn = getConnection()) {
        conn.openPipeline();
        if (springDataRedis_2_0) {
            try {
                this.redisConnectionSet_2_0.invoke(conn, refreshKey, serializedRefreshToken);
                this.redisConnectionSet_2_0.invoke(conn, refreshAuthKey, serialize(authentication));
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        } else {
            conn.set(refreshKey, serializedRefreshToken);
            conn.set(refreshAuthKey, serialize(authentication));
        }
        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(refreshKey, seconds);
                conn.expire(refreshAuthKey, seconds);
            }
        }
        conn.closePipeline();
    }
}
Also used : RedisConnection(org.springframework.data.redis.connection.RedisConnection) ExpiringOAuth2RefreshToken(org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)

Aggregations

ExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.ExpiringOAuth2RefreshToken)26 Test (org.junit.Test)13 RedisConnection (org.springframework.data.redis.connection.RedisConnection)10 OAuth2AccessToken (org.springframework.security.oauth2.common.OAuth2AccessToken)10 DefaultExpiringOAuth2RefreshToken (org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken)9 Date (java.util.Date)8 OAuth2RefreshToken (org.springframework.security.oauth2.common.OAuth2RefreshToken)8 OAuth2Authentication (org.springframework.security.oauth2.provider.OAuth2Authentication)6 TokenRequest (org.springframework.security.oauth2.provider.TokenRequest)6 ExpiringOAuth2RefreshToken (org.maxkey.authz.oauth2.common.ExpiringOAuth2RefreshToken)5 DefaultOAuth2AccessToken (org.springframework.security.oauth2.common.DefaultOAuth2AccessToken)4 UaaUser (org.cloudfoundry.identity.uaa.user.UaaUser)3 UaaUserPrototype (org.cloudfoundry.identity.uaa.user.UaaUserPrototype)3 DefaultExpiringOAuth2RefreshToken (org.maxkey.authz.oauth2.common.DefaultExpiringOAuth2RefreshToken)3 OAuth2RefreshToken (org.maxkey.authz.oauth2.common.OAuth2RefreshToken)3 Transactional (org.springframework.transaction.annotation.Transactional)3 DefaultOAuth2AccessToken (org.maxkey.authz.oauth2.common.DefaultOAuth2AccessToken)2 DefaultOAuth2RefreshToken (org.maxkey.authz.oauth2.common.DefaultOAuth2RefreshToken)2 OAuth2AccessToken (org.maxkey.authz.oauth2.common.OAuth2AccessToken)2 RedisConnection (org.maxkey.persistence.redis.RedisConnection)2