diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java index 3ddca838..58df560d 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/AuthHandler.java @@ -2,6 +2,7 @@ import pro.gravit.launchserver.auth.provider.AuthProviderResult; import pro.gravit.launchserver.auth.AuthException; +import pro.gravit.launchserver.auth.provider.HibernateAuthProvider; import pro.gravit.utils.ProviderMap; import java.io.IOException; @@ -23,6 +24,7 @@ public static void registerHandlers() { providers.register("memory", MemoryAuthHandler.class); providers.register("mysql", MySQLAuthHandler.class); providers.register("request", RequestAuthHandler.class); + providers.register("hibernate", HibernateAuthHandler.class); registredHandl = true; } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java new file mode 100644 index 00000000..0b1bab5a --- /dev/null +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/auth/handler/HibernateAuthHandler.java @@ -0,0 +1,48 @@ +package pro.gravit.launchserver.auth.handler; + +import pro.gravit.launchserver.hibernate.User; +import pro.gravit.launchserver.hibernate.UserService; + +import java.io.IOException; +import java.util.UUID; + +public class HibernateAuthHandler extends CachedAuthHandler { + @Override + protected Entry fetchEntry(String username) throws IOException { + UserService service = new UserService(); + User user = service.findUserByUsername(username); + if(user == null) return null; + return new Entry(user.uuid, username, user.getAccessToken(), user.serverID); + } + + @Override + protected Entry fetchEntry(UUID uuid) throws IOException { + UserService service = new UserService(); + User user = service.findUserByUUID(uuid); + if(user == null) return null; + return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID); + } + + @Override + protected boolean updateAuth(UUID uuid, String username, String accessToken) throws IOException { + UserService service = new UserService(); + User user = service.findUserByUUID(uuid); + user.setAccessToken(accessToken); + service.updateUser(user); + return true; + } + + @Override + protected boolean updateServerID(UUID uuid, String serverID) throws IOException { + UserService service = new UserService(); + User user = service.findUserByUUID(uuid); + user.serverID = serverID; + service.updateUser(user); + return true; + } + + @Override + public void close() throws IOException { + + } +} diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java index 43f90d94..54672c27 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/User.java @@ -8,6 +8,7 @@ import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Arrays; +import java.util.UUID; @Entity @Table(name = "users") @@ -17,8 +18,12 @@ public class User { private long id; @Column(unique = true) public String username; + @Column(unique = true) + public UUID uuid; @Column(name = "password") private byte[] password; + private String accessToken; + public String serverID; private String password_salt; public void setPassword(String password) { @@ -46,4 +51,12 @@ public boolean verifyPassword(String password) LogHelper.info(Arrays.toString(this.password)); return Arrays.equals(enpassword, this.password); } + + public String getAccessToken() { + return accessToken; + } + + public void setAccessToken(String accessToken) { + this.accessToken = accessToken; + } } diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java index 513b1a6d..7cf563b7 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAO.java @@ -1,10 +1,12 @@ package pro.gravit.launchserver.hibernate; import java.util.List; +import java.util.UUID; public interface UserDAO { User findById(int id); User findByUsername(String username); + User findByUUID(UUID uuid); void save(User user); void update(User user); void delete(User user); diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java index 68e01008..fb8d13d1 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserDAOImpl.java @@ -8,6 +8,7 @@ import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; import java.util.List; +import java.util.UUID; public class UserDAOImpl implements UserDAO { @@ -22,9 +23,21 @@ public User findByUsername(String username) { CriteriaQuery personCriteria = cb.createQuery(User.class); Root rootUser = personCriteria.from(User.class); personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username)); - User ret = em.createQuery(personCriteria).getSingleResult(); + List ret = em.createQuery(personCriteria).getResultList(); em.close(); - return ret; + return ret.size() == 0 ? null : ret.get(0); + } + + public User findByUUID(UUID uuid) { + EntityManager em = HibernateManager.sessionFactory.createEntityManager(); + em.getTransaction().begin(); + CriteriaBuilder cb = em.getCriteriaBuilder(); + CriteriaQuery personCriteria = cb.createQuery(User.class); + Root rootUser = personCriteria.from(User.class); + personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid)); + List ret = em.createQuery(personCriteria).getResultList(); + em.close(); + return ret.size() == 0 ? null : ret.get(0); } public void save(User user) { diff --git a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java index fade65f8..437c6319 100644 --- a/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java +++ b/LaunchServer/src/main/java/pro/gravit/launchserver/hibernate/UserService.java @@ -1,6 +1,7 @@ package pro.gravit.launchserver.hibernate; import java.util.List; +import java.util.UUID; public class UserService { @@ -16,6 +17,9 @@ public User findUser(int id) { public User findUserByUsername(String username) { return usersDao.findByUsername(username); } + public User findUserByUUID(UUID uuid) { + return usersDao.findByUUID(uuid); + } public void saveUser(User user) { usersDao.save(user);