cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject cxf git commit: Some clean up in OAuth2 JPA
Date Fri, 15 Apr 2016 10:10:41 GMT
Repository: cxf
Updated Branches:
  refs/heads/master d2661a973 -> 74b0254f4


Some clean up in OAuth2 JPA


Project: http://git-wip-us.apache.org/repos/asf/cxf/repo
Commit: http://git-wip-us.apache.org/repos/asf/cxf/commit/74b0254f
Tree: http://git-wip-us.apache.org/repos/asf/cxf/tree/74b0254f
Diff: http://git-wip-us.apache.org/repos/asf/cxf/diff/74b0254f

Branch: refs/heads/master
Commit: 74b0254f497dfb277645253ebb0390ac56d2653b
Parents: d2661a9
Author: Sergey Beryozkin <sberyozkin@gmail.com>
Authored: Fri Apr 15 11:10:27 2016 +0100
Committer: Sergey Beryozkin <sberyozkin@gmail.com>
Committed: Fri Apr 15 11:10:27 2016 +0100

----------------------------------------------------------------------
 .../oauth2/grants/code/JPACodeDataProvider.java | 16 ++----
 .../oauth2/provider/JPAOAuthDataProvider.java   | 51 +++++---------------
 rt/rs/security/sso/oidc/pom.xml                 |  2 +-
 3 files changed, 18 insertions(+), 51 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/74b0254f/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/code/JPACodeDataProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/code/JPACodeDataProvider.java
b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/code/JPACodeDataProvider.java
index d582fcf..c122f34 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/code/JPACodeDataProvider.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/code/JPACodeDataProvider.java
@@ -20,7 +20,6 @@ package org.apache.cxf.rs.security.oauth2.grants.code;
 
 import java.util.List;
 
-import javax.persistence.NoResultException;
 import javax.persistence.TypedQuery;
 
 import org.apache.cxf.rs.security.oauth2.common.Client;
@@ -50,13 +49,11 @@ public class JPACodeDataProvider extends JPAOAuthDataProvider implements
Authori
     
     @Override
     public ServerAuthorizationCodeGrant removeCodeGrant(String code) throws OAuthServiceException
{
-        try {
-            ServerAuthorizationCodeGrant grant = getCodeQuery(code).getSingleResult();
+        ServerAuthorizationCodeGrant grant = getEntityManager().find(ServerAuthorizationCodeGrant.class,
code);
+        if (grant != null) {
             removeEntity(grant);
-            return grant;
-        } catch (NoResultException ex) {
-            return null;
-        }
+        } 
+        return grant;
     }
 
     @Override
@@ -67,11 +64,6 @@ public class JPACodeDataProvider extends JPAOAuthDataProvider implements
Authori
     public void setCodeLifetime(long codeLifetime) {
         this.codeLifetime = codeLifetime;
     }
-    protected TypedQuery<ServerAuthorizationCodeGrant> getCodeQuery(String code) {
-        return getEntityManager().createQuery(
-            "SELECT c FROM " + CODE_TABLE_NAME + " c WHERE c.code = '" + code + "'", 
-            ServerAuthorizationCodeGrant.class);
-    }
     protected TypedQuery<ServerAuthorizationCodeGrant> getCodesQuery(Client c, UserSubject
resourceOwnerSubject) {
         if (c == null && resourceOwnerSubject == null) {
             return getEntityManager().createQuery("SELECT c FROM " + CODE_TABLE_NAME + "
c", 

http://git-wip-us.apache.org/repos/asf/cxf/blob/74b0254f/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/provider/JPAOAuthDataProvider.java
----------------------------------------------------------------------
diff --git a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/provider/JPAOAuthDataProvider.java
b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/provider/JPAOAuthDataProvider.java
index e5cbdc6..c552592 100644
--- a/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/provider/JPAOAuthDataProvider.java
+++ b/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/provider/JPAOAuthDataProvider.java
@@ -22,7 +22,6 @@ import java.util.LinkedList;
 import java.util.List;
 
 import javax.persistence.EntityManager;
-import javax.persistence.NoResultException;
 import javax.persistence.TypedQuery;
 
 import org.apache.cxf.rs.security.oauth2.common.Client;
@@ -43,23 +42,21 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
     
     @Override
     public Client getClient(String clientId) throws OAuthServiceException {
-        try {
-            return getClientQuery(clientId).getSingleResult();
-        } catch (NoResultException ex) {
-            return null;
-        }
+        return getEntityManager().find(Client.class, clientId);
     }
     
     public void setClient(Client client) {
+        getEntityManager().getTransaction().begin();
         if (client.getResourceOwnerSubject() != null) {
             UserSubject sub = getEntityManager().find(UserSubject.class, client.getResourceOwnerSubject().getLogin());
             if (sub == null) {
-                persistEntity(client.getResourceOwnerSubject());
+                getEntityManager().persist(client.getResourceOwnerSubject());
             } else {
                 client.setResourceOwnerSubject(sub);
             }
         }
-        persistEntity(client);
+        getEntityManager().persist(client);
+        getEntityManager().getTransaction().commit();
     }
     
     @Override
@@ -84,11 +81,7 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
     
     @Override
     public ServerAccessToken getAccessToken(String accessToken) throws OAuthServiceException
{
-        try {
-            return getTokenQuery(accessToken).getSingleResult();
-        } catch (NoResultException ex) {
-            return null;
-        }
+        return getEntityManager().find(BearerAccessToken.class, accessToken);
     }
     @Override
     protected void doRevokeAccessToken(ServerAccessToken at) {
@@ -96,11 +89,7 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
     }
     @Override
     protected RefreshToken getRefreshToken(String refreshTokenKey) { 
-        try {
-            return getRefreshTokenQuery(refreshTokenKey).getSingleResult();
-        } catch (NoResultException ex) {
-            return null;
-        }
+        return getEntityManager().find(RefreshToken.class, refreshTokenKey);
     }
     @Override
     protected void doRevokeRefreshToken(RefreshToken rt) { 
@@ -108,13 +97,14 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
     }
     
     protected void saveAccessToken(ServerAccessToken serverToken) {
+        getEntityManager().getTransaction().begin();
         List<OAuthPermission> perms = new LinkedList<OAuthPermission>();
         for (OAuthPermission perm : serverToken.getScopes()) {
             OAuthPermission permSaved = getEntityManager().find(OAuthPermission.class, perm.getPermission());
             if (permSaved != null) {
                 perms.add(permSaved);
             } else {
-                persistEntity(perm);
+                getEntityManager().persist(perm);
                 perms.add(perm);
             }
         }
@@ -122,15 +112,14 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider
{
         
         UserSubject sub = getEntityManager().find(UserSubject.class, serverToken.getSubject().getLogin());
         if (sub == null) {
-            persistEntity(serverToken.getSubject());
+            getEntityManager().persist(serverToken.getSubject());
         } else {
-            entityManager.getTransaction().begin();
-            sub = entityManager.merge(serverToken.getSubject());
-            entityManager.getTransaction().commit();
+            sub = getEntityManager().merge(serverToken.getSubject());
             serverToken.setSubject(sub);
         }
         
-        persistEntity(serverToken);
+        getEntityManager().persist(serverToken);
+        getEntityManager().getTransaction().commit();
     }
     
     protected void saveRefreshToken(RefreshToken refreshToken) {
@@ -146,10 +135,6 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
         entityManager.remove(entity);
         entityManager.getTransaction().commit();
     }
-    protected TypedQuery<Client> getClientQuery(String clientId) {
-        return entityManager.createQuery(
-            "SELECT c FROM " + CLIENT_TABLE_NAME + " c WHERE c.clientId = '" + clientId +
"'", Client.class);
-    }
     protected TypedQuery<Client> getClientsQuery(UserSubject resourceOwnerSubject)
{
         if (resourceOwnerSubject == null) {
             return entityManager.createQuery("SELECT c FROM " + CLIENT_TABLE_NAME + " c",
Client.class);
@@ -159,11 +144,6 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
                 + resourceOwnerSubject.getLogin() + "'", Client.class);
         }
     }
-    protected TypedQuery<BearerAccessToken> getTokenQuery(String tokenKey) {
-        return entityManager.createQuery(
-            "SELECT t FROM " + BEARER_TOKEN_TABLE_NAME + " t WHERE t.tokenKey = '" + tokenKey
+ "'", 
-            BearerAccessToken.class);
-    }
     protected TypedQuery<ServerAccessToken> getTokensQuery(Client c, UserSubject resourceOwnerSubject)
{
         if (c == null && resourceOwnerSubject == null) {
             return entityManager.createQuery("SELECT t FROM " + BEARER_TOKEN_TABLE_NAME +
" t", 
@@ -183,11 +163,6 @@ public class JPAOAuthDataProvider extends AbstractOAuthDataProvider {
                 ServerAccessToken.class);
         }
     }
-    protected TypedQuery<RefreshToken> getRefreshTokenQuery(String tokenKey) {
-        return entityManager.createQuery(
-            "SELECT t FROM " + REFRESH_TOKEN_TABLE_NAME + " t WHERE t.tokenKey = '" + tokenKey
+ "'", 
-            RefreshToken.class);
-    }
     protected TypedQuery<RefreshToken> getRefreshTokensQuery(Client c, UserSubject
resourceOwnerSubject) {
         if (c == null && resourceOwnerSubject == null) {
             return entityManager.createQuery("SELECT t FROM " + REFRESH_TOKEN_TABLE_NAME
+ " t", 

http://git-wip-us.apache.org/repos/asf/cxf/blob/74b0254f/rt/rs/security/sso/oidc/pom.xml
----------------------------------------------------------------------
diff --git a/rt/rs/security/sso/oidc/pom.xml b/rt/rs/security/sso/oidc/pom.xml
index c271cd3..b5dcec2 100644
--- a/rt/rs/security/sso/oidc/pom.xml
+++ b/rt/rs/security/sso/oidc/pom.xml
@@ -75,7 +75,7 @@
              <groupId>org.apache.openjpa</groupId>
              <artifactId>openjpa</artifactId>
              <version>${openjpa.version}</version>
-             <scope>test</scope>
+             <scope>provided</scope>
         </dependency>
         -->
         <dependency>


Mime
View raw message