Moved getExpired to service layers

pull/477/head
Amanda Anganes 2013-08-06 16:33:27 -04:00
parent eea37cf79c
commit b3bb43881d
5 changed files with 41 additions and 61 deletions

View File

@ -50,15 +50,8 @@ public interface OAuth2TokenRepository {
public List<OAuth2RefreshTokenEntity> getRefreshTokensForClient(ClientDetailsEntity client);
public List<OAuth2AccessTokenEntity> getExpiredAccessTokens();
public List<OAuth2RefreshTokenEntity> getExpiredRefreshTokens();
public OAuth2AccessTokenEntity getByAuthentication(OAuth2Authentication auth);
/**
* @return
*/
public OAuth2AccessTokenEntity getAccessTokenForIdToken(OAuth2AccessTokenEntity idToken);
public Set<OAuth2AccessTokenEntity> getAllAccessTokens();

View File

@ -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<OAuth2AccessTokenEntity> getExpiredAccessTokens() {
TypedQuery<OAuth2AccessTokenEntity> queryA = manager.createNamedQuery("OAuth2AccessTokenEntity.getAll", OAuth2AccessTokenEntity.class);
List<OAuth2AccessTokenEntity> accessTokens = queryA.getResultList();
List<OAuth2AccessTokenEntity> 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<OAuth2RefreshTokenEntity> getExpiredRefreshTokens() {
TypedQuery<OAuth2RefreshTokenEntity> queryR = manager.createNamedQuery("OAuth2RefreshTokenEntity.getAll", OAuth2RefreshTokenEntity.class);
List<OAuth2RefreshTokenEntity> refreshTokens = queryR.getResultList();
List<OAuth2RefreshTokenEntity> expired = Lists.newArrayList();
for (OAuth2RefreshTokenEntity r : refreshTokens) {
if (r.isExpired()) {
expired.add(r);
}
}
return expired;
}
@Override
public OAuth2AccessTokenEntity getByAuthentication(OAuth2Authentication auth) {
TypedQuery<OAuth2AccessTokenEntity> queryA = manager.createNamedQuery("OAuth2AccessTokenEntity.getByAuthentication", OAuth2AccessTokenEntity.class);

View File

@ -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<OAuth2AccessTokenEntity> accessTokens = tokenRepository.getExpiredAccessTokens();
List<OAuth2AccessTokenEntity> accessTokens = getExpiredAccessTokens();
logger.info("Found " + accessTokens.size() + " expired access tokens");
for (OAuth2AccessTokenEntity oAuth2AccessTokenEntity : accessTokens) {
revokeAccessToken(oAuth2AccessTokenEntity);
}
List<OAuth2RefreshTokenEntity> refreshTokens = tokenRepository.getExpiredRefreshTokens();
List<OAuth2RefreshTokenEntity> refreshTokens = getExpiredRefreshTokens();
logger.info("Found " + refreshTokens.size() + " expired refresh tokens");
for (OAuth2RefreshTokenEntity oAuth2RefreshTokenEntity : refreshTokens) {
revokeRefreshToken(oAuth2RefreshTokenEntity);
}
}
private List<OAuth2AccessTokenEntity> getExpiredAccessTokens() {
Collection<OAuth2AccessTokenEntity> accessTokens = tokenRepository.getAllAccessTokens();
List<OAuth2AccessTokenEntity> expired = Lists.newArrayList();
for (OAuth2AccessTokenEntity a : accessTokens) {
if (a.isExpired()) {
expired.add(a);
}
}
return expired;
}
private List<OAuth2RefreshTokenEntity> getExpiredRefreshTokens() {
Collection<OAuth2RefreshTokenEntity> refreshTokens = tokenRepository.getAllRefreshTokens();
List<OAuth2RefreshTokenEntity> 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)

View File

@ -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<ApprovedSite> getExpired() {
TypedQuery<ApprovedSite> query = manager.createNamedQuery("ApprovedSite.getAll", ApprovedSite.class);
List<ApprovedSite> sites = query.getResultList();
List<ApprovedSite> expired = Lists.newArrayList();
for (ApprovedSite a : sites) {
if (a.isExpired()) {
expired.add(a);
}
}
return expired;
}
}

View File

@ -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<ApprovedSite> expiredSites = approvedSiteRepository.getExpired();
Collection<ApprovedSite> expiredSites = getExpired();
if (expiredSites != null) {
for (ApprovedSite expired : expiredSites) {
approvedSiteRepository.remove(expired);
}
}
}
private Collection<ApprovedSite> getExpired() {
Collection<ApprovedSite> sites = approvedSiteRepository.getAll();
List<ApprovedSite> expired = Lists.newArrayList();
for (ApprovedSite a : sites) {
if (a.isExpired()) {
expired.add(a);
}
}
return expired;
}
}