From b3bb43881d76a492a36ee3f5d00f922dd35ae794 Mon Sep 17 00:00:00 2001 From: Amanda Anganes Date: Tue, 6 Aug 2013 16:33:27 -0400 Subject: [PATCH] Moved getExpired to service layers --- .../repository/OAuth2TokenRepository.java | 7 ---- .../impl/JpaOAuth2TokenRepository.java | 34 ------------------- .../DefaultOAuth2ProviderTokenService.java | 28 +++++++++++++-- .../impl/JpaApprovedSiteRepository.java | 17 ---------- .../impl/DefaultApprovedSiteService.java | 16 ++++++++- 5 files changed, 41 insertions(+), 61 deletions(-) diff --git a/openid-connect-common/src/main/java/org/mitre/oauth2/repository/OAuth2TokenRepository.java b/openid-connect-common/src/main/java/org/mitre/oauth2/repository/OAuth2TokenRepository.java index b5a5a945d..96b402f1e 100644 --- a/openid-connect-common/src/main/java/org/mitre/oauth2/repository/OAuth2TokenRepository.java +++ b/openid-connect-common/src/main/java/org/mitre/oauth2/repository/OAuth2TokenRepository.java @@ -50,15 +50,8 @@ public interface OAuth2TokenRepository { public List getRefreshTokensForClient(ClientDetailsEntity client); - public List getExpiredAccessTokens(); - - public List getExpiredRefreshTokens(); - public OAuth2AccessTokenEntity getByAuthentication(OAuth2Authentication auth); - /** - * @return - */ public OAuth2AccessTokenEntity getAccessTokenForIdToken(OAuth2AccessTokenEntity idToken); public Set getAllAccessTokens(); diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaOAuth2TokenRepository.java b/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaOAuth2TokenRepository.java index 6714fccc5..eac31a8a5 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaOAuth2TokenRepository.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/repository/impl/JpaOAuth2TokenRepository.java @@ -33,8 +33,6 @@ import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; -import com.google.common.collect.Lists; - @Repository public class JpaOAuth2TokenRepository implements OAuth2TokenRepository { @@ -162,38 +160,6 @@ public class JpaOAuth2TokenRepository implements OAuth2TokenRepository { return refreshTokens; } - /* (non-Javadoc) - * @see org.mitre.oauth2.repository.OAuth2TokenRepository#getExpiredAccessTokens() - */ - @Override - public List getExpiredAccessTokens() { - TypedQuery queryA = manager.createNamedQuery("OAuth2AccessTokenEntity.getAll", OAuth2AccessTokenEntity.class); - List accessTokens = queryA.getResultList(); - List expired = Lists.newArrayList(); - for (OAuth2AccessTokenEntity a : accessTokens) { - if (a.isExpired()) { - expired.add(a); - } - } - return expired; - } - - /* (non-Javadoc) - * @see org.mitre.oauth2.repository.OAuth2TokenRepository#getExpiredRefreshTokens() - */ - @Override - public List getExpiredRefreshTokens() { - TypedQuery queryR = manager.createNamedQuery("OAuth2RefreshTokenEntity.getAll", OAuth2RefreshTokenEntity.class); - List refreshTokens = queryR.getResultList(); - List expired = Lists.newArrayList(); - for (OAuth2RefreshTokenEntity r : refreshTokens) { - if (r.isExpired()) { - expired.add(r); - } - } - return expired; - } - @Override public OAuth2AccessTokenEntity getByAuthentication(OAuth2Authentication auth) { TypedQuery queryA = manager.createNamedQuery("OAuth2AccessTokenEntity.getByAuthentication", OAuth2AccessTokenEntity.class); diff --git a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ProviderTokenService.java b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ProviderTokenService.java index cabed2b3e..ceaa15fba 100644 --- a/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ProviderTokenService.java +++ b/openid-connect-server/src/main/java/org/mitre/oauth2/service/impl/DefaultOAuth2ProviderTokenService.java @@ -19,6 +19,7 @@ */ package org.mitre.oauth2.service.impl; +import java.util.Collection; import java.util.Date; import java.util.HashSet; import java.util.List; @@ -49,6 +50,7 @@ import org.springframework.security.oauth2.provider.TokenRequest; import org.springframework.security.oauth2.provider.token.TokenEnhancer; import org.springframework.stereotype.Service; +import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.nimbusds.jwt.JWTClaimsSet; import com.nimbusds.jwt.PlainJWT; @@ -385,18 +387,40 @@ public class DefaultOAuth2ProviderTokenService implements OAuth2TokenEntityServi public void clearExpiredTokens() { logger.info("Cleaning out all expired tokens"); - List accessTokens = tokenRepository.getExpiredAccessTokens(); + List accessTokens = getExpiredAccessTokens(); logger.info("Found " + accessTokens.size() + " expired access tokens"); for (OAuth2AccessTokenEntity oAuth2AccessTokenEntity : accessTokens) { revokeAccessToken(oAuth2AccessTokenEntity); } - List refreshTokens = tokenRepository.getExpiredRefreshTokens(); + List refreshTokens = getExpiredRefreshTokens(); logger.info("Found " + refreshTokens.size() + " expired refresh tokens"); for (OAuth2RefreshTokenEntity oAuth2RefreshTokenEntity : refreshTokens) { revokeRefreshToken(oAuth2RefreshTokenEntity); } } + + private List getExpiredAccessTokens() { + Collection accessTokens = tokenRepository.getAllAccessTokens(); + List expired = Lists.newArrayList(); + for (OAuth2AccessTokenEntity a : accessTokens) { + if (a.isExpired()) { + expired.add(a); + } + } + return expired; + } + + private List getExpiredRefreshTokens() { + Collection refreshTokens = tokenRepository.getAllRefreshTokens(); + List expired = Lists.newArrayList(); + for (OAuth2RefreshTokenEntity r : refreshTokens) { + if (r.isExpired()) { + expired.add(r); + } + } + return expired; + } /* (non-Javadoc) * @see org.mitre.oauth2.service.OAuth2TokenEntityService#saveAccessToken(org.mitre.oauth2.model.OAuth2AccessTokenEntity) diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaApprovedSiteRepository.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaApprovedSiteRepository.java index 849d9a61c..c96f119d7 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaApprovedSiteRepository.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/repository/impl/JpaApprovedSiteRepository.java @@ -19,7 +19,6 @@ package org.mitre.openid.connect.repository.impl; import static org.mitre.util.jpa.JpaUtil.saveOrUpdate; import java.util.Collection; -import java.util.List; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; @@ -30,8 +29,6 @@ import org.mitre.openid.connect.repository.ApprovedSiteRepository; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; -import com.google.common.collect.Lists; - /** * JPA ApprovedSite repository implementation * @@ -104,18 +101,4 @@ public class JpaApprovedSiteRepository implements ApprovedSiteRepository { return query.getResultList(); } - - @Override - @Transactional - public Collection getExpired() { - TypedQuery query = manager.createNamedQuery("ApprovedSite.getAll", ApprovedSite.class); - List sites = query.getResultList(); - List expired = Lists.newArrayList(); - for (ApprovedSite a : sites) { - if (a.isExpired()) { - expired.add(a); - } - } - return expired; - } } diff --git a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultApprovedSiteService.java b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultApprovedSiteService.java index c8edf00b6..d86245ec3 100644 --- a/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultApprovedSiteService.java +++ b/openid-connect-server/src/main/java/org/mitre/openid/connect/service/impl/DefaultApprovedSiteService.java @@ -18,6 +18,7 @@ package org.mitre.openid.connect.service.impl; import java.util.Collection; import java.util.Date; +import java.util.List; import java.util.Set; import org.mitre.oauth2.model.OAuth2AccessTokenEntity; @@ -33,6 +34,8 @@ import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; +import com.google.common.collect.Lists; + /** * Implementation of the ApprovedSiteService * @@ -147,12 +150,23 @@ public class DefaultApprovedSiteService implements ApprovedSiteService { logger.info("Clearing expired approved sites"); - Collection expiredSites = approvedSiteRepository.getExpired(); + Collection expiredSites = getExpired(); if (expiredSites != null) { for (ApprovedSite expired : expiredSites) { approvedSiteRepository.remove(expired); } } } + + private Collection getExpired() { + Collection sites = approvedSiteRepository.getAll(); + List expired = Lists.newArrayList(); + for (ApprovedSite a : sites) { + if (a.isExpired()) { + expired.add(a); + } + } + return expired; + } }