mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-23 00:51:01 +03:00
[FEATURE][EXPERIMENTAL] New Hibernate DAO
This commit is contained in:
parent
f0ca39cde9
commit
43e1533c87
22 changed files with 314 additions and 301 deletions
|
@ -7,31 +7,31 @@
|
||||||
public class HibernateAuthHandler extends CachedAuthHandler {
|
public class HibernateAuthHandler extends CachedAuthHandler {
|
||||||
@Override
|
@Override
|
||||||
protected Entry fetchEntry(String username) {
|
protected Entry fetchEntry(String username) {
|
||||||
User user = srv.config.dao.userService.findUserByUsername(username);
|
User user = srv.config.dao.userDAO.findByUsername(username);
|
||||||
if (user == null) return null;
|
if (user == null) return null;
|
||||||
return new Entry(user.uuid, username, user.getAccessToken(), user.serverID);
|
return new Entry(user.getUuid(), username, user.getAccessToken(), user.getServerID());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Entry fetchEntry(UUID uuid) {
|
protected Entry fetchEntry(UUID uuid) {
|
||||||
User user = srv.config.dao.userService.findUserByUUID(uuid);
|
User user = srv.config.dao.userDAO.findByUUID(uuid);
|
||||||
if (user == null) return null;
|
if (user == null) return null;
|
||||||
return new Entry(user.uuid, user.username, user.getAccessToken(), user.serverID);
|
return new Entry(user.getUuid(), user.getUsername(), user.getAccessToken(), user.getServerID());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean updateAuth(UUID uuid, String username, String accessToken) {
|
protected boolean updateAuth(UUID uuid, String username, String accessToken) {
|
||||||
User user = srv.config.dao.userService.findUserByUUID(uuid);
|
User user = srv.config.dao.userDAO.findByUUID(uuid);
|
||||||
user.setAccessToken(accessToken);
|
user.setAccessToken(accessToken);
|
||||||
srv.config.dao.userService.updateUser(user);
|
srv.config.dao.userDAO.update(user);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected boolean updateServerID(UUID uuid, String serverID) {
|
protected boolean updateServerID(UUID uuid, String serverID) {
|
||||||
User user = srv.config.dao.userService.findUserByUUID(uuid);
|
User user = srv.config.dao.userDAO.findByUUID(uuid);
|
||||||
user.serverID = serverID;
|
user.setServerID(serverID);
|
||||||
srv.config.dao.userService.updateUser(user);
|
srv.config.dao.userDAO.update(user);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,17 +7,17 @@ public class HibernatePermissionsHandler extends PermissionsHandler {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public ClientPermissions getPermissions(String username) {
|
public ClientPermissions getPermissions(String username) {
|
||||||
User user = srv.config.dao.userService.findUserByUsername(username);
|
User user = srv.config.dao.userDAO.findByUsername(username);
|
||||||
if (user == null) return ClientPermissions.DEFAULT;
|
if (user == null) return ClientPermissions.DEFAULT;
|
||||||
return user.getPermissions();
|
return user.getPermissions();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void setPermissions(String username, ClientPermissions permissions) {
|
public void setPermissions(String username, ClientPermissions permissions) {
|
||||||
User user = srv.config.dao.userService.findUserByUsername(username);
|
User user = srv.config.dao.userDAO.findByUsername(username);
|
||||||
if (user == null) return;
|
if (user == null) return;
|
||||||
user.setPermissions(permissions);
|
user.setPermissions(permissions);
|
||||||
srv.config.dao.userService.updateUser(user);
|
srv.config.dao.userDAO.update(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -13,7 +13,7 @@ public class HibernateAuthProvider extends AuthProvider {
|
||||||
@Override
|
@Override
|
||||||
public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws Exception {
|
public AuthProviderResult auth(String login, AuthRequest.AuthPasswordInterface password, String ip) throws Exception {
|
||||||
if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported");
|
if (!(password instanceof AuthPlainPassword)) throw new AuthException("This password type not supported");
|
||||||
User user = srv.config.dao.userService.findUserByUsername(login);
|
User user = srv.config.dao.userDAO.findByUsername(login);
|
||||||
if (user == null && autoReg) {
|
if (user == null && autoReg) {
|
||||||
AuthHookManager.RegContext context = new AuthHookManager.RegContext(login, ((AuthPlainPassword) password).password, ip, false);
|
AuthHookManager.RegContext context = new AuthHookManager.RegContext(login, ((AuthPlainPassword) password).password, ip, false);
|
||||||
if (srv.authHookManager.registraion.hook(context)) {
|
if (srv.authHookManager.registraion.hook(context)) {
|
||||||
|
|
|
@ -23,8 +23,8 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) {
|
public void invoke(String... args) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (User user : server.config.dao.userService.findAllUsers()) {
|
for (User user : server.config.dao.userDAO.findAll()) {
|
||||||
LogHelper.subInfo("[%s] UUID: %s", user.username, user.uuid.toString());
|
LogHelper.subInfo("[%s] UUID: %s", user.getUsername(), user.getUuid().toString());
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
LogHelper.info("Print %d users", count);
|
LogHelper.info("Print %d users", count);
|
||||||
|
|
|
@ -23,12 +23,12 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) throws Exception {
|
public void invoke(String... args) throws Exception {
|
||||||
verifyArgs(args, 1);
|
verifyArgs(args, 1);
|
||||||
User user = server.config.dao.userService.findUserByUsername(args[0]);
|
User user = server.config.dao.userDAO.findByUsername(args[0]);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
LogHelper.error("User %s not found", args[0]);
|
LogHelper.error("User %s not found", args[0]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
LogHelper.info("[%s] UUID: %s", user.username, user.uuid.toString());
|
LogHelper.info("[%s] UUID: %s", user.getUsername(), user.getUuid().toString());
|
||||||
//for(UserHWID hwid : user.hwids)
|
//for(UserHWID hwid : user.hwids)
|
||||||
//{
|
//{
|
||||||
// LogHelper.info("[%s] HWID: memory: %d | serial %s | hwdiskserial: %s | processorID %s | macAddr %s", user.username, hwid.totalMemory, hwid.serialNumber, hwid.HWDiskSerial, hwid.processorID, hwid.macAddr);
|
// LogHelper.info("[%s] HWID: memory: %d | serial %s | hwdiskserial: %s | processorID %s | macAddr %s", user.username, hwid.totalMemory, hwid.serialNumber, hwid.HWDiskSerial, hwid.processorID, hwid.macAddr);
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
import pro.gravit.launchserver.command.Command;
|
import pro.gravit.launchserver.command.Command;
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.impl.UserHibernateImpl;
|
||||||
import pro.gravit.utils.helper.LogHelper;
|
import pro.gravit.utils.helper.LogHelper;
|
||||||
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -25,11 +25,11 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) throws Exception {
|
public void invoke(String... args) throws Exception {
|
||||||
verifyArgs(args, 2);
|
verifyArgs(args, 2);
|
||||||
User user = new User();
|
UserHibernateImpl user = new UserHibernateImpl();
|
||||||
user.username = args[0];
|
user.username = args[0];
|
||||||
user.setPassword(args[1]);
|
user.setPassword(args[1]);
|
||||||
user.uuid = UUID.randomUUID();
|
user.uuid = UUID.randomUUID();
|
||||||
server.config.dao.userService.saveUser(user);
|
server.config.dao.userDAO.save(user);
|
||||||
LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString());
|
LogHelper.info("User %s registered. UUID: %s", user.username, user.uuid.toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,13 +24,13 @@ public String getUsageDescription() {
|
||||||
@Override
|
@Override
|
||||||
public void invoke(String... args) throws Exception {
|
public void invoke(String... args) throws Exception {
|
||||||
verifyArgs(args, 2);
|
verifyArgs(args, 2);
|
||||||
User user = server.config.dao.userService.findUserByUsername(args[0]);
|
User user = server.config.dao.userDAO.findByUsername(args[0]);
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
LogHelper.error("User %s not found", args[1]);
|
LogHelper.error("User %s not found", args[1]);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
user.setPassword(args[1]);
|
user.setPassword(args[1]);
|
||||||
server.config.dao.userService.updateUser(user);
|
server.config.dao.userDAO.update(user);
|
||||||
LogHelper.info("[%s] UUID: %s | New Password: %s", user.username, user.uuid.toString(), args[1]);
|
LogHelper.info("[%s] UUID: %s | New Password: %s", user.getUsername(), user.getUuid().toString(), args[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,19 @@
|
||||||
|
package pro.gravit.launchserver.dao;
|
||||||
|
|
||||||
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface HwidDAO {
|
||||||
|
UserHWID findById(long id);
|
||||||
|
|
||||||
|
List<? extends UserHWID> findHWIDs(HWID hwid);
|
||||||
|
|
||||||
|
void save(UserHWID user);
|
||||||
|
|
||||||
|
void update(UserHWID user);
|
||||||
|
|
||||||
|
void delete(UserHWID user);
|
||||||
|
|
||||||
|
List<UserHWID> findAll();
|
||||||
|
}
|
|
@ -1,71 +1,19 @@
|
||||||
package pro.gravit.launchserver.dao;
|
package pro.gravit.launchserver.dao;
|
||||||
|
|
||||||
import pro.gravit.launcher.ClientPermissions;
|
import pro.gravit.launcher.ClientPermissions;
|
||||||
import pro.gravit.utils.helper.LogHelper;
|
|
||||||
import pro.gravit.utils.helper.SecurityHelper;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
|
||||||
import java.nio.charset.StandardCharsets;
|
|
||||||
import java.security.MessageDigest;
|
|
||||||
import java.security.NoSuchAlgorithmException;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
|
||||||
@Entity
|
public interface User {
|
||||||
@Table(name = "users")
|
String getUsername();
|
||||||
public class User {
|
ClientPermissions getPermissions();
|
||||||
@Id
|
void setPermissions(ClientPermissions permissions);
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
boolean verifyPassword(String password);
|
||||||
private long id;
|
void setPassword(String password);
|
||||||
@Column(unique = true)
|
String getAccessToken();
|
||||||
public String username;
|
void setAccessToken(String accessToken);
|
||||||
public String email;
|
String getServerID();
|
||||||
@Column(unique = true)
|
void setServerID(String serverID);
|
||||||
public UUID uuid;
|
UUID getUuid();
|
||||||
@Column(name = "password")
|
void setUuid(UUID uuid);
|
||||||
private byte[] password;
|
|
||||||
private String accessToken;
|
|
||||||
public String serverID;
|
|
||||||
private String password_salt;
|
|
||||||
public long permissions;
|
|
||||||
|
|
||||||
public void setPassword(String password) {
|
|
||||||
password_salt = SecurityHelper.randomStringAESKey();
|
|
||||||
MessageDigest digest;
|
|
||||||
try {
|
|
||||||
digest = MessageDigest.getInstance("SHA-256");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
LogHelper.error(e);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
|
||||||
}
|
|
||||||
|
|
||||||
public boolean verifyPassword(String password) {
|
|
||||||
MessageDigest digest;
|
|
||||||
try {
|
|
||||||
digest = MessageDigest.getInstance("SHA-256");
|
|
||||||
} catch (NoSuchAlgorithmException e) {
|
|
||||||
LogHelper.error(e);
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
|
||||||
return Arrays.equals(enpassword, this.password);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ClientPermissions getPermissions() {
|
|
||||||
return new ClientPermissions(permissions);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setPermissions(ClientPermissions permissions) {
|
|
||||||
this.permissions = permissions.toLong();
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getAccessToken() {
|
|
||||||
return accessToken;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void setAccessToken(String accessToken) {
|
|
||||||
this.accessToken = accessToken;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
package pro.gravit.launchserver.dao;
|
package pro.gravit.launchserver.dao;
|
||||||
|
|
||||||
import pro.gravit.launcher.hwid.OshiHWID;
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.UUID;
|
import java.util.UUID;
|
||||||
|
@ -12,8 +12,6 @@ public interface UserDAO {
|
||||||
|
|
||||||
User findByUUID(UUID uuid);
|
User findByUUID(UUID uuid);
|
||||||
|
|
||||||
List<UserHWID> findHWID(OshiHWID hwid);
|
|
||||||
|
|
||||||
void save(User user);
|
void save(User user);
|
||||||
|
|
||||||
void update(User user);
|
void update(User user);
|
||||||
|
|
|
@ -1,63 +1,8 @@
|
||||||
package pro.gravit.launchserver.dao;
|
package pro.gravit.launchserver.dao;
|
||||||
|
|
||||||
import pro.gravit.launcher.hwid.HWID;
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
import pro.gravit.launcher.hwid.OshiHWID;
|
|
||||||
|
|
||||||
import javax.persistence.*;
|
public interface UserHWID {
|
||||||
import java.util.function.Supplier;
|
boolean isBanned();
|
||||||
|
HWID toHWID();
|
||||||
@Entity
|
|
||||||
@Table(name = "users_hwids")
|
|
||||||
public class UserHWID implements HWID {
|
|
||||||
private final transient Supplier<OshiHWID> oshiSupp = () -> {
|
|
||||||
OshiHWID hwid = new OshiHWID();
|
|
||||||
hwid.HWDiskSerial = this.HWDiskSerial;
|
|
||||||
hwid.macAddr = this.macAddr;
|
|
||||||
hwid.processorID = this.processorID;
|
|
||||||
hwid.serialNumber = this.serialNumber;
|
|
||||||
hwid.totalMemory = this.totalMemory;
|
|
||||||
return hwid;
|
|
||||||
};
|
|
||||||
private transient OshiHWID oshi = null;
|
|
||||||
@Id
|
|
||||||
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
|
||||||
private long id;
|
|
||||||
|
|
||||||
public final long totalMemory = 0;
|
|
||||||
public String serialNumber;
|
|
||||||
public String HWDiskSerial;
|
|
||||||
public String processorID;
|
|
||||||
public String macAddr;
|
|
||||||
|
|
||||||
public boolean banned;
|
|
||||||
|
|
||||||
public OshiHWID toHWID() {
|
|
||||||
if (oshi == null) oshi = oshiSupp.get();
|
|
||||||
return oshi;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getLevel() {
|
|
||||||
return toHWID().getLevel();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int getAntiLevel() {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public int compare(HWID hwid) {
|
|
||||||
return toHWID().compare(hwid);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean isNull() {
|
|
||||||
return toHWID().isNull();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void normalize() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,51 +0,0 @@
|
||||||
package pro.gravit.launchserver.dao;
|
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class UserService {
|
|
||||||
|
|
||||||
private final UserDAO usersDao;
|
|
||||||
|
|
||||||
public UserService(UserDAO usersDAO) {
|
|
||||||
this.usersDao = usersDAO;
|
|
||||||
}
|
|
||||||
|
|
||||||
public User findUser(int id) {
|
|
||||||
return usersDao.findById(id);
|
|
||||||
}
|
|
||||||
|
|
||||||
public User findUserByUsername(String username) {
|
|
||||||
return usersDao.findByUsername(username);
|
|
||||||
}
|
|
||||||
|
|
||||||
public User findUserByUUID(UUID uuid) {
|
|
||||||
return usersDao.findByUUID(uuid);
|
|
||||||
}
|
|
||||||
|
|
||||||
public User registerNewUser(String username, String password) {
|
|
||||||
User user = new User();
|
|
||||||
user.username = username;
|
|
||||||
user.setPassword(password);
|
|
||||||
user.uuid = UUID.randomUUID();
|
|
||||||
return user;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void saveUser(User user) {
|
|
||||||
usersDao.save(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void deleteUser(User user) {
|
|
||||||
usersDao.delete(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void updateUser(User user) {
|
|
||||||
usersDao.update(user);
|
|
||||||
}
|
|
||||||
|
|
||||||
public List<User> findAllUsers() {
|
|
||||||
return usersDao.findAll();
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,56 +0,0 @@
|
||||||
package pro.gravit.launchserver.dao.impl;
|
|
||||||
|
|
||||||
import pro.gravit.launcher.hwid.OshiHWID;
|
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
|
||||||
import pro.gravit.launchserver.dao.User;
|
|
||||||
import pro.gravit.launchserver.dao.UserDAO;
|
|
||||||
import pro.gravit.launchserver.dao.UserHWID;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.UUID;
|
|
||||||
|
|
||||||
public class DefaultUserDAOImpl implements UserDAO {
|
|
||||||
public DefaultUserDAOImpl(LaunchServer srv) {
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User findById(int id) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User findByUsername(String username) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public User findByUUID(UUID uuid) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<UserHWID> findHWID(OshiHWID hwid) {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void save(User user) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void update(User user) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void delete(User user) {
|
|
||||||
throw new UnsupportedOperationException();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<User> findAll() {
|
|
||||||
return new ArrayList<>();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package pro.gravit.launchserver.dao.impl;
|
||||||
|
|
||||||
|
import org.hibernate.Session;
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
|
import org.hibernate.Transaction;
|
||||||
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
|
import pro.gravit.launcher.hwid.OshiHWID;
|
||||||
|
import pro.gravit.launchserver.dao.HwidDAO;
|
||||||
|
import pro.gravit.launchserver.dao.User;
|
||||||
|
import pro.gravit.launchserver.dao.UserHWID;
|
||||||
|
|
||||||
|
import javax.persistence.EntityManager;
|
||||||
|
import javax.persistence.criteria.CriteriaBuilder;
|
||||||
|
import javax.persistence.criteria.CriteriaQuery;
|
||||||
|
import javax.persistence.criteria.Root;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class HibernateHwidDAOImpl implements HwidDAO {
|
||||||
|
private final SessionFactory factory;
|
||||||
|
|
||||||
|
public HibernateHwidDAOImpl(SessionFactory factory) {
|
||||||
|
this.factory = factory;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public List<UserHWIDImpl> findHWIDs(HWID hwid) {
|
||||||
|
if(!(hwid instanceof OshiHWID)) throw new UnsupportedOperationException();
|
||||||
|
OshiHWID oshiHWID = (OshiHWID) hwid;
|
||||||
|
EntityManager em = factory.createEntityManager();
|
||||||
|
em.getTransaction().begin();
|
||||||
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
|
CriteriaQuery<UserHWIDImpl> personCriteria = cb.createQuery(UserHWIDImpl.class);
|
||||||
|
Root<UserHWIDImpl> rootUser = personCriteria.from(UserHWIDImpl.class);
|
||||||
|
personCriteria.select(rootUser).where(
|
||||||
|
cb.or(
|
||||||
|
cb.equal(rootUser.get("totalMemory"), oshiHWID.totalMemory),
|
||||||
|
cb.equal(rootUser.get("HWDiskSerial"), oshiHWID.HWDiskSerial),
|
||||||
|
cb.equal(rootUser.get("serialNumber"), oshiHWID.serialNumber),
|
||||||
|
cb.equal(rootUser.get("processorID"), oshiHWID.processorID),
|
||||||
|
cb.equal(rootUser.get("macAddr"), oshiHWID.macAddr)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
List<UserHWIDImpl> list = em.createQuery(personCriteria).getResultList();
|
||||||
|
em.close();
|
||||||
|
return list;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserHWIDImpl findById(long id) {
|
||||||
|
try (Session s = factory.openSession()) {
|
||||||
|
return s.get(UserHWIDImpl.class, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public void save(UserHWID user) {
|
||||||
|
try (Session session = factory.openSession()) {
|
||||||
|
Transaction tx1 = session.beginTransaction();
|
||||||
|
session.save(user);
|
||||||
|
tx1.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void update(UserHWID user) {
|
||||||
|
try (Session session = factory.openSession()) {
|
||||||
|
Transaction tx1 = session.beginTransaction();
|
||||||
|
session.update(user);
|
||||||
|
tx1.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void delete(UserHWID user) {
|
||||||
|
try (Session session = factory.openSession()) {
|
||||||
|
Transaction tx1 = session.beginTransaction();
|
||||||
|
session.delete(user);
|
||||||
|
tx1.commit();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("unchecked")
|
||||||
|
public List<UserHWID> findAll() {
|
||||||
|
try (Session s = factory.openSession()) {
|
||||||
|
return (List<UserHWID>) s.createQuery("From user_hwids").list();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -3,6 +3,7 @@
|
||||||
import org.hibernate.Session;
|
import org.hibernate.Session;
|
||||||
import org.hibernate.SessionFactory;
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.Transaction;
|
import org.hibernate.Transaction;
|
||||||
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
import pro.gravit.launcher.hwid.OshiHWID;
|
import pro.gravit.launcher.hwid.OshiHWID;
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.User;
|
||||||
import pro.gravit.launchserver.dao.UserDAO;
|
import pro.gravit.launchserver.dao.UserDAO;
|
||||||
|
@ -22,57 +23,36 @@ public HibernateUserDAOImpl(SessionFactory factory) {
|
||||||
this.factory = factory;
|
this.factory = factory;
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findById(int id) {
|
public UserHibernateImpl findById(int id) {
|
||||||
try (Session s = factory.openSession()) {
|
try (Session s = factory.openSession()) {
|
||||||
return s.get(User.class, id);
|
return s.get(UserHibernateImpl.class, id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUsername(String username) {
|
public UserHibernateImpl findByUsername(String username) {
|
||||||
EntityManager em = factory.createEntityManager();
|
EntityManager em = factory.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
CriteriaQuery<UserHibernateImpl> personCriteria = cb.createQuery(UserHibernateImpl.class);
|
||||||
Root<User> rootUser = personCriteria.from(User.class);
|
Root<UserHibernateImpl> rootUser = personCriteria.from(UserHibernateImpl.class);
|
||||||
personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username));
|
personCriteria.select(rootUser).where(cb.equal(rootUser.get("username"), username));
|
||||||
List<User> ret = em.createQuery(personCriteria).getResultList();
|
List<UserHibernateImpl> ret = em.createQuery(personCriteria).getResultList();
|
||||||
em.close();
|
em.close();
|
||||||
return ret.size() == 0 ? null : ret.get(0);
|
return ret.size() == 0 ? null : ret.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
public User findByUUID(UUID uuid) {
|
public UserHibernateImpl findByUUID(UUID uuid) {
|
||||||
EntityManager em = factory.createEntityManager();
|
EntityManager em = factory.createEntityManager();
|
||||||
em.getTransaction().begin();
|
em.getTransaction().begin();
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
CriteriaBuilder cb = em.getCriteriaBuilder();
|
||||||
CriteriaQuery<User> personCriteria = cb.createQuery(User.class);
|
CriteriaQuery<UserHibernateImpl> personCriteria = cb.createQuery(UserHibernateImpl.class);
|
||||||
Root<User> rootUser = personCriteria.from(User.class);
|
Root<UserHibernateImpl> rootUser = personCriteria.from(UserHibernateImpl.class);
|
||||||
personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid));
|
personCriteria.select(rootUser).where(cb.equal(rootUser.get("uuid"), uuid));
|
||||||
List<User> ret = em.createQuery(personCriteria).getResultList();
|
List<UserHibernateImpl> ret = em.createQuery(personCriteria).getResultList();
|
||||||
em.close();
|
em.close();
|
||||||
return ret.size() == 0 ? null : ret.get(0);
|
return ret.size() == 0 ? null : ret.get(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public List<UserHWID> findHWID(OshiHWID hwid) {
|
|
||||||
EntityManager em = factory.createEntityManager();
|
|
||||||
em.getTransaction().begin();
|
|
||||||
CriteriaBuilder cb = em.getCriteriaBuilder();
|
|
||||||
CriteriaQuery<UserHWID> personCriteria = cb.createQuery(UserHWID.class);
|
|
||||||
Root<UserHWID> rootUser = personCriteria.from(UserHWID.class);
|
|
||||||
personCriteria.select(rootUser).where(
|
|
||||||
cb.or(
|
|
||||||
cb.equal(rootUser.get("totalMemory"), hwid.totalMemory),
|
|
||||||
cb.equal(rootUser.get("HWDiskSerial"), hwid.HWDiskSerial),
|
|
||||||
cb.equal(rootUser.get("serialNumber"), hwid.serialNumber),
|
|
||||||
cb.equal(rootUser.get("processorID"), hwid.processorID),
|
|
||||||
cb.equal(rootUser.get("macAddr"), hwid.macAddr)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
List<UserHWID> ret = em.createQuery(personCriteria).getResultList();
|
|
||||||
em.close();
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void save(User user) {
|
public void save(User user) {
|
||||||
try (Session session = factory.openSession()) {
|
try (Session session = factory.openSession()) {
|
||||||
Transaction tx1 = session.beginTransaction();
|
Transaction tx1 = session.beginTransaction();
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
package pro.gravit.launchserver.dao.impl;
|
||||||
|
|
||||||
|
import pro.gravit.launcher.hwid.HWID;
|
||||||
|
import pro.gravit.launcher.hwid.OshiHWID;
|
||||||
|
import pro.gravit.launchserver.dao.UserHWID;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users_hwids")
|
||||||
|
public class UserHWIDImpl implements UserHWID {
|
||||||
|
private final transient Supplier<OshiHWID> oshiSupp = () -> {
|
||||||
|
OshiHWID hwid = new OshiHWID();
|
||||||
|
hwid.HWDiskSerial = this.HWDiskSerial;
|
||||||
|
hwid.macAddr = this.macAddr;
|
||||||
|
hwid.processorID = this.processorID;
|
||||||
|
hwid.serialNumber = this.serialNumber;
|
||||||
|
hwid.totalMemory = this.totalMemory;
|
||||||
|
return hwid;
|
||||||
|
};
|
||||||
|
private transient OshiHWID oshi = null;
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
|
||||||
|
public final long totalMemory = 0;
|
||||||
|
public String serialNumber;
|
||||||
|
public String HWDiskSerial;
|
||||||
|
public String processorID;
|
||||||
|
public String macAddr;
|
||||||
|
|
||||||
|
public boolean banned;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isBanned() {
|
||||||
|
return banned;
|
||||||
|
}
|
||||||
|
|
||||||
|
public OshiHWID toHWID() {
|
||||||
|
if (oshi == null) oshi = oshiSupp.get();
|
||||||
|
return oshi;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,99 @@
|
||||||
|
package pro.gravit.launchserver.dao.impl;
|
||||||
|
|
||||||
|
import pro.gravit.launcher.ClientPermissions;
|
||||||
|
import pro.gravit.launchserver.dao.User;
|
||||||
|
import pro.gravit.utils.helper.LogHelper;
|
||||||
|
import pro.gravit.utils.helper.SecurityHelper;
|
||||||
|
|
||||||
|
import javax.persistence.*;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
import java.security.MessageDigest;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
public class UserHibernateImpl implements User {
|
||||||
|
@Id
|
||||||
|
@GeneratedValue(strategy = GenerationType.IDENTITY)
|
||||||
|
private long id;
|
||||||
|
@Column(unique = true)
|
||||||
|
public String username;
|
||||||
|
public String email;
|
||||||
|
@Column(unique = true)
|
||||||
|
public UUID uuid;
|
||||||
|
@Column(name = "password")
|
||||||
|
private byte[] password;
|
||||||
|
private String accessToken;
|
||||||
|
public String serverID;
|
||||||
|
private String password_salt;
|
||||||
|
public long permissions;
|
||||||
|
|
||||||
|
public void setPassword(String password) {
|
||||||
|
password_salt = SecurityHelper.randomStringAESKey();
|
||||||
|
MessageDigest digest;
|
||||||
|
try {
|
||||||
|
digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
LogHelper.error(e);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.password = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean verifyPassword(String password) {
|
||||||
|
MessageDigest digest;
|
||||||
|
try {
|
||||||
|
digest = MessageDigest.getInstance("SHA-256");
|
||||||
|
} catch (NoSuchAlgorithmException e) {
|
||||||
|
LogHelper.error(e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
byte[] enpassword = digest.digest(password.concat(password_salt).getBytes(StandardCharsets.UTF_8));
|
||||||
|
return Arrays.equals(enpassword, this.password);
|
||||||
|
}
|
||||||
|
|
||||||
|
public ClientPermissions getPermissions() {
|
||||||
|
return new ClientPermissions(permissions);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPermissions(ClientPermissions permissions) {
|
||||||
|
this.permissions = permissions.toLong();
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAccessToken() {
|
||||||
|
return accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setAccessToken(String accessToken) {
|
||||||
|
this.accessToken = accessToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getUsername() {
|
||||||
|
return username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUsername(String username) {
|
||||||
|
this.username = username;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UUID getUuid() {
|
||||||
|
return uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUuid(UUID uuid) {
|
||||||
|
this.uuid = uuid;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServerID() {
|
||||||
|
return serverID;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setServerID(String serverID) {
|
||||||
|
this.serverID = serverID;
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,14 +1,14 @@
|
||||||
package pro.gravit.launchserver.dao.provider;
|
package pro.gravit.launchserver.dao.provider;
|
||||||
|
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
|
import pro.gravit.launchserver.dao.HwidDAO;
|
||||||
import pro.gravit.launchserver.dao.UserDAO;
|
import pro.gravit.launchserver.dao.UserDAO;
|
||||||
import pro.gravit.launchserver.dao.UserService;
|
|
||||||
import pro.gravit.utils.ProviderMap;
|
import pro.gravit.utils.ProviderMap;
|
||||||
|
|
||||||
public abstract class DaoProvider {
|
public abstract class DaoProvider {
|
||||||
public static final ProviderMap<DaoProvider> providers = new ProviderMap<>("DaoProvider");
|
public static final ProviderMap<DaoProvider> providers = new ProviderMap<>("DaoProvider");
|
||||||
public UserDAO userDAO;
|
public UserDAO userDAO;
|
||||||
public UserService userService;
|
public HwidDAO hwidDao;
|
||||||
|
|
||||||
public static void registerProviders() {
|
public static void registerProviders() {
|
||||||
providers.register("hibernate", HibernateDaoProvider.class);
|
providers.register("hibernate", HibernateDaoProvider.class);
|
||||||
|
|
|
@ -1,10 +1,11 @@
|
||||||
package pro.gravit.launchserver.dao.provider;
|
package pro.gravit.launchserver.dao.provider;
|
||||||
|
|
||||||
|
import org.hibernate.SessionFactory;
|
||||||
import org.hibernate.cfg.Configuration;
|
import org.hibernate.cfg.Configuration;
|
||||||
import pro.gravit.launchserver.LaunchServer;
|
import pro.gravit.launchserver.LaunchServer;
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.impl.HibernateHwidDAOImpl;
|
||||||
import pro.gravit.launchserver.dao.UserHWID;
|
import pro.gravit.launchserver.dao.impl.UserHibernateImpl;
|
||||||
import pro.gravit.launchserver.dao.UserService;
|
import pro.gravit.launchserver.dao.impl.UserHWIDImpl;
|
||||||
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
import pro.gravit.launchserver.dao.impl.HibernateUserDAOImpl;
|
||||||
import pro.gravit.utils.helper.CommonHelper;
|
import pro.gravit.utils.helper.CommonHelper;
|
||||||
|
|
||||||
|
@ -19,13 +20,14 @@ public class HibernateDaoProvider extends DaoProvider {
|
||||||
public String pool_size;
|
public String pool_size;
|
||||||
public String hibernateConfig;
|
public String hibernateConfig;
|
||||||
public boolean parallelHibernateInit;
|
public boolean parallelHibernateInit;
|
||||||
|
private transient SessionFactory sessionFactory;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void init(LaunchServer server) {
|
public void init(LaunchServer server) {
|
||||||
Runnable init = () -> {
|
Runnable init = () -> {
|
||||||
Configuration cfg = new Configuration()
|
Configuration cfg = new Configuration()
|
||||||
.addAnnotatedClass(User.class)
|
.addAnnotatedClass(UserHibernateImpl.class)
|
||||||
.addAnnotatedClass(UserHWID.class)
|
.addAnnotatedClass(UserHWIDImpl.class)
|
||||||
.setProperty("hibernate.connection.driver_class", driver)
|
.setProperty("hibernate.connection.driver_class", driver)
|
||||||
.setProperty("hibernate.connection.url", url)
|
.setProperty("hibernate.connection.url", url)
|
||||||
.setProperty("hibernate.connection.username", username)
|
.setProperty("hibernate.connection.username", username)
|
||||||
|
@ -35,8 +37,9 @@ public void init(LaunchServer server) {
|
||||||
cfg.setProperty("hibernate.dialect", dialect);
|
cfg.setProperty("hibernate.dialect", dialect);
|
||||||
if (hibernateConfig != null)
|
if (hibernateConfig != null)
|
||||||
cfg.configure(Paths.get(hibernateConfig).toFile());
|
cfg.configure(Paths.get(hibernateConfig).toFile());
|
||||||
userDAO = new HibernateUserDAOImpl(cfg.buildSessionFactory());
|
sessionFactory = cfg.buildSessionFactory();
|
||||||
userService = new UserService(userDAO);
|
userDAO = new HibernateUserDAOImpl(sessionFactory);
|
||||||
|
hwidDao = new HibernateHwidDAOImpl(sessionFactory);
|
||||||
};
|
};
|
||||||
if (parallelHibernateInit)
|
if (parallelHibernateInit)
|
||||||
CommonHelper.newThread("Hibernate Thread", true, init);
|
CommonHelper.newThread("Hibernate Thread", true, init);
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import pro.gravit.launchserver.dao.User;
|
import pro.gravit.launchserver.dao.User;
|
||||||
|
import pro.gravit.launchserver.dao.impl.UserHibernateImpl;
|
||||||
import pro.gravit.launchserver.socket.Client;
|
import pro.gravit.launchserver.socket.Client;
|
||||||
import pro.gravit.launchserver.socket.response.SimpleResponse;
|
import pro.gravit.launchserver.socket.response.SimpleResponse;
|
||||||
|
|
||||||
|
@ -24,17 +25,17 @@ public void execute(ChannelHandlerContext ctx, Client client) throws Exception {
|
||||||
sendError("Hash invalid");
|
sendError("Hash invalid");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
User checkUser = server.config.dao.userService.findUserByUsername(login);
|
User checkUser = server.config.dao.userDAO.findByUsername(login);
|
||||||
if (checkUser != null) {
|
if (checkUser != null) {
|
||||||
sendError("User already register");
|
sendError("User already register");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
User user = new User();
|
UserHibernateImpl user = new UserHibernateImpl();
|
||||||
user.username = login;
|
user.username = login;
|
||||||
user.email = email;
|
user.email = email;
|
||||||
user.setPassword(password);
|
user.setPassword(password);
|
||||||
user.uuid = UUID.randomUUID();
|
user.uuid = UUID.randomUUID();
|
||||||
server.config.dao.userService.saveUser(user);
|
server.config.dao.userDAO.save(user);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -31,11 +31,11 @@ public void execute(ChannelHandlerContext ctx, Client client) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (username != null) {
|
if (username != null) {
|
||||||
User user = server.config.dao.userService.findUserByUsername(username);
|
User user = server.config.dao.userDAO.findByUsername(username);
|
||||||
user.setPassword(newPassword);
|
user.setPassword(newPassword);
|
||||||
sendResult(new SetPasswordRequestEvent());
|
sendResult(new SetPasswordRequestEvent());
|
||||||
} else {
|
} else {
|
||||||
User user = server.config.dao.userService.findUserByUsername(client.username);
|
User user = server.config.dao.userDAO.findByUsername(client.username);
|
||||||
if (user.verifyPassword(oldPassword)) {
|
if (user.verifyPassword(oldPassword)) {
|
||||||
user.setPassword(newPassword);
|
user.setPassword(newPassword);
|
||||||
sendResult(new SetPasswordRequestEvent());
|
sendResult(new SetPasswordRequestEvent());
|
||||||
|
|
2
modules
2
modules
|
@ -1 +1 @@
|
||||||
Subproject commit 9cb09c549abea86348597137e05a0ec29bc36b62
|
Subproject commit 12cb835489667b39453e874f43bd314a67f0719e
|
Loading…
Reference in a new issue