]> git.aero2k.de Git - tmp/jakarta-migration.git/commitdiff
DRYD-736: Added post-auth code to update lastlogin timestamp to users table.
authorRichard Millet <remillet@gmail.com>
Thu, 29 Aug 2019 23:53:10 +0000 (16:53 -0700)
committerRichard Millet <remillet@gmail.com>
Thu, 19 Sep 2019 18:19:23 +0000 (11:19 -0700)
services/authentication/service/src/main/java/org/collectionspace/authentication/CSpaceAuthenticationSuccessEvent.java
services/authentication/service/src/main/java/org/collectionspace/authentication/realm/db/CSpaceDbRealm.java

index a325cd05351ec6746a27b95a578544f69703daef..f83cfdb311ad760fac4f629423e0d757abc67445 100644 (file)
@@ -1,14 +1,97 @@
 package org.collectionspace.authentication;
 
+import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+
+import javax.security.auth.login.AccountException;
+import javax.security.auth.login.AccountNotFoundException;
+
+import org.collectionspace.authentication.realm.db.CSpaceDbRealm;
+import org.postgresql.util.PSQLState;
 import org.springframework.context.ApplicationListener;
 import org.springframework.security.authentication.event.AuthenticationSuccessEvent;
+import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 
 public class CSpaceAuthenticationSuccessEvent implements ApplicationListener<AuthenticationSuccessEvent> {
+       
+       final private static String UPDATE_USER_SQL =
+                       "UPDATE users SET lastlogin = now() WHERE username = ?";
 
        @Override
        public void onApplicationEvent(AuthenticationSuccessEvent event) {
                // TODO Auto-generated method stub
                System.out.println(); //org.springframework.security.authentication.UsernamePasswordAuthenticationToken@8a633e91: Principal: org.collectionspace.authentication.CSpaceUser@b122ec20: Username: admin@core.collectionspace.org; Password: [PROTECTED]; Enabled: true; AccountNonExpired: true; credentialsNonExpired: true; AccountNonLocked: true; Granted Authorities: ROLE_1_TENANT_ADMINISTRATOR,ROLE_SPRING_ADMIN; Credentials: [PROTECTED]; Authenticated: true; Details: {grant_type=password, username=admin@core.collectionspace.org}; Granted Authorities: ROLE_1_TENANT_ADMINISTRATOR, ROLE_SPRING_ADMIN
+               String userName = null;
+               CSpaceDbRealm cspaceDbRealm = new CSpaceDbRealm();
+               
+               if (event.getSource() instanceof UsernamePasswordAuthenticationToken) {
+                       UsernamePasswordAuthenticationToken eventSource = (UsernamePasswordAuthenticationToken)event.getSource();
+                       if (eventSource.getPrincipal() instanceof CSpaceUser) {
+                               CSpaceUser cspaceUser = (CSpaceUser) eventSource.getPrincipal();
+                               userName = cspaceUser.getUsername();
+                               try {
+                                       setLastLogin(cspaceDbRealm, userName);
+                               } catch (Exception e) {
+                                       // TODO Auto-generated catch block
+                                       e.printStackTrace();
+                               }
+                               System.out.println();
+                       }
+               }
        }
+       
+       private void setLastLogin(CSpaceDbRealm cspaceDbRealm, String username) throws AccountException {
+        Connection conn = null;
+        PreparedStatement ps = null;
+        ResultSet rs = null;
+        try {
+            conn = cspaceDbRealm.getConnection();
+            // Get the salt
+
+            ps = conn.prepareStatement(UPDATE_USER_SQL);
+            ps.setString(1, username);
+            rs = ps.executeQuery();
+            if (rs.next() == false) {
+                throw new AccountNotFoundException("No matching username found");
+            }
+        } catch (SQLException ex) {
+               // Assuming PostgreSQL
+            if (PSQLState.UNDEFINED_COLUMN.getState().equals(ex.getSQLState())) {
+               System.err.println("'USERS' table is missing 'lastlogin' column.");
+            } else {
+                AccountException ae = new AccountException("Authentication query failed: " + ex.getLocalizedMessage());
+                ae.initCause(ex);
+                throw ae;
+            }
+        } catch (AccountNotFoundException ex) {
+            throw ex;
+        } catch (Exception ex) {
+            AccountException ae = new AccountException("Unknown Exception");
+            ae.initCause(ex);
+            throw ae;
+        } finally {
+            if (rs != null) {
+                try {
+                    rs.close();
+                } catch (SQLException e) {
+                }
+            }
+            if (ps != null) {
+                try {
+                    ps.close();
+                } catch (SQLException e) {
+                }
+            }
+            if (conn != null) {
+                try {
+                    conn.close();
+                } catch (SQLException ex) {
+                }
+            }
+        }
+    }
+
 
 }
index 62045711143b0b4903b4cabdc555f683d0b65cdf..814ec72fa83010d21293e4e42755270dc73fd950 100644 (file)
@@ -77,6 +77,8 @@ import org.slf4j.LoggerFactory;
  * @author 
  */
 public class CSpaceDbRealm implements CSpaceRealm {
+       public static String DEFAULT_DATASOURCE_NAME = "CspaceDS";
+       
     private Logger logger = LoggerFactory.getLogger(CSpaceDbRealm.class);
     
     private String datasourceName;
@@ -128,6 +130,10 @@ public class CSpaceDbRealm implements CSpaceRealm {
        protected long getDelayBetweenAttemptsMillis() {
                return this.delayBetweenAttemptsMillis;
        }
+       
+       public CSpaceDbRealm() {
+        datasourceName = DEFAULT_DATASOURCE_NAME;
+       }
     
     /**
      * CSpace Database Realm
@@ -136,7 +142,7 @@ public class CSpaceDbRealm implements CSpaceRealm {
     public CSpaceDbRealm(Map<String, ?> options) {
         datasourceName = (String) options.get("dsJndiName");
         if (datasourceName == null) {
-            datasourceName = "java:/DefaultDS";
+            datasourceName = DEFAULT_DATASOURCE_NAME;
         }
         Object tmp = options.get("principalsQuery");
         if (tmp != null) {
@@ -172,7 +178,6 @@ public class CSpaceDbRealm implements CSpaceRealm {
             logger.trace("rolesQuery=" + rolesQuery);
             logger.trace("suspendResume=" + suspendResume);
         }
-
     }
 
     @Override
@@ -445,7 +450,7 @@ public class CSpaceDbRealm implements CSpaceRealm {
      * it will retry for the next 'getMaxRetrySeconds()' seconds.  If it is unable to get the connection then it will timeout and
      * throw an exception.
      */
-    private Connection getConnection() throws Exception {
+    public Connection getConnection() throws Exception {
         Connection result = null;
                boolean failed = true;
                Exception lastException = null;