forked from External/mage
Add a user registration dialog.
This commit is contained in:
parent
9ca716bf76
commit
835f08c18f
14 changed files with 801 additions and 268 deletions
|
|
@ -29,15 +29,19 @@ public class AuthorizedUser {
|
|||
@DatabaseField
|
||||
protected int hashIterations;
|
||||
|
||||
@DatabaseField
|
||||
protected String email;
|
||||
|
||||
public AuthorizedUser() {
|
||||
}
|
||||
|
||||
public AuthorizedUser(String name, Hash hash) {
|
||||
public AuthorizedUser(String name, Hash hash, String email) {
|
||||
this.name = name;
|
||||
this.password = hash.toBase64();
|
||||
this.salt = hash.getSalt().toBase64();
|
||||
this.hashAlgorithm = hash.getAlgorithmName();
|
||||
this.hashIterations = hash.getIterations();
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
public boolean doCredentialsMatch(String name, String password) {
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ public enum AuthorizedUserRepository {
|
|||
private static final String JDBC_URL = "jdbc:h2:file:./db/authorized_user.h2;AUTO_SERVER=TRUE";
|
||||
private static final String VERSION_ENTITY_NAME = "authorized_user";
|
||||
// raise this if db structure was changed
|
||||
private static final long DB_VERSION = 0;
|
||||
private static final long DB_VERSION = 1;
|
||||
private static final RandomNumberGenerator rng = new SecureRandomNumberGenerator();
|
||||
|
||||
private Dao<AuthorizedUser, Object> dao;
|
||||
|
|
@ -52,14 +52,14 @@ public enum AuthorizedUserRepository {
|
|||
}
|
||||
}
|
||||
|
||||
public void add(final String userName, final String password) {
|
||||
public void add(final String userName, final String password, final String email) {
|
||||
try {
|
||||
dao.callBatchTasks(new Callable<Object>() {
|
||||
@Override
|
||||
public Object call() throws Exception {
|
||||
try {
|
||||
Hash hash = new SimpleHash(Sha256Hash.ALGORITHM_NAME, password, rng.nextBytes(), 1024);
|
||||
AuthorizedUser user = new AuthorizedUser(userName, hash);
|
||||
AuthorizedUser user = new AuthorizedUser(userName, hash, email);
|
||||
dao.create(user);
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error adding a user to DB - ", ex);
|
||||
|
|
@ -68,6 +68,7 @@ public enum AuthorizedUserRepository {
|
|||
}
|
||||
});
|
||||
} catch (Exception ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error adding a authorized_user - ", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -81,6 +82,7 @@ public enum AuthorizedUserRepository {
|
|||
}
|
||||
return null;
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error getting a authorized_user - ", ex);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
|
@ -92,6 +94,7 @@ public enum AuthorizedUserRepository {
|
|||
conn.executeStatement("shutdown compact", 0);
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
Logger.getLogger(AuthorizedUserRepository.class).error("Error closing authorized_user repository - ", ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -105,6 +105,11 @@ public class MageServerImpl implements MageServer {
|
|||
ServerMessagesUtil.getInstance().getMessages();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean registerUser(String sessionId, String userName, String password, String email) throws MageException {
|
||||
return SessionManager.getInstance().registerUser(sessionId, userName, password, email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean registerClient(String userName, String sessionId, MageVersion version) throws MageException {
|
||||
// This method is deprecated, so just inform the server version.
|
||||
|
|
@ -114,14 +119,14 @@ public class MageServerImpl implements MageServer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean registerClientWithPassword(String userName, String password, String sessionId, MageVersion version) throws MageException {
|
||||
public boolean connectUser(String userName, String password, String sessionId, MageVersion version) throws MageException {
|
||||
try {
|
||||
if (version.compareTo(Main.getVersion()) != 0) {
|
||||
logger.info("MageVersionException: userName=" + userName + ", version=" + version);
|
||||
LogServiceImpl.instance.log(LogKeys.KEY_WRONG_VERSION, userName, version.toString(), Main.getVersion().toString(), sessionId);
|
||||
throw new MageVersionException(version, Main.getVersion());
|
||||
}
|
||||
return SessionManager.getInstance().registerUser(sessionId, userName, password);
|
||||
return SessionManager.getInstance().connectUser(sessionId, userName, password);
|
||||
} catch (MageException ex) {
|
||||
if (ex instanceof MageVersionException) {
|
||||
throw (MageVersionException) ex;
|
||||
|
|
@ -142,7 +147,7 @@ public class MageServerImpl implements MageServer {
|
|||
}
|
||||
|
||||
@Override
|
||||
public boolean registerAdmin(String adminPassword, String sessionId, MageVersion version) throws MageException {
|
||||
public boolean connectAdmin(String adminPassword, String sessionId, MageVersion version) throws MageException {
|
||||
try {
|
||||
if (version.compareTo(Main.getVersion()) != 0) {
|
||||
throw new MageException("Wrong client version " + version + ", expecting version " + Main.getVersion());
|
||||
|
|
@ -150,7 +155,7 @@ public class MageServerImpl implements MageServer {
|
|||
if (!adminPassword.equals(this.adminPassword)) {
|
||||
throw new MageException("Wrong password");
|
||||
}
|
||||
return SessionManager.getInstance().registerAdmin(sessionId);
|
||||
return SessionManager.getInstance().connectAdmin(sessionId);
|
||||
} catch (Exception ex) {
|
||||
handleException(ex);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -74,20 +74,24 @@ public class Session {
|
|||
this.lock = new ReentrantLock();
|
||||
}
|
||||
|
||||
public String registerUser(String userName, String password) throws MageException {
|
||||
String returnMessage = registerUserHandling(userName, password);
|
||||
if (returnMessage != null) {
|
||||
sendErrorMessageToClient(returnMessage);
|
||||
public String registerUser(String userName, String password, String email) throws MageException {
|
||||
synchronized(AuthorizedUserRepository.instance) {
|
||||
String returnMessage = validateUserName(userName);
|
||||
if (returnMessage != null) {
|
||||
sendErrorMessageToClient(returnMessage);
|
||||
return returnMessage;
|
||||
}
|
||||
returnMessage = validatePassword(password);
|
||||
if (returnMessage != null) {
|
||||
sendErrorMessageToClient(returnMessage);
|
||||
return returnMessage;
|
||||
}
|
||||
AuthorizedUserRepository.instance.add(userName, password, email);
|
||||
return null;
|
||||
}
|
||||
return returnMessage;
|
||||
}
|
||||
|
||||
public boolean isLocked() {
|
||||
return lock.isLocked();
|
||||
}
|
||||
|
||||
public String registerUserHandling(String userName, String password) throws MageException {
|
||||
this.isAdmin = false;
|
||||
|
||||
static private String validateUserName(String userName) {
|
||||
if (userName.equals("Admin")) {
|
||||
return "User name Admin already in use";
|
||||
}
|
||||
|
|
@ -102,22 +106,44 @@ public class Session {
|
|||
if (m.find()) {
|
||||
return "User name '" + userName + "' includes not allowed characters: use a-z, A-Z and 0-9";
|
||||
}
|
||||
|
||||
AuthorizedUser authorizedUser = AuthorizedUserRepository.instance.get(userName);
|
||||
if (authorizedUser == null) {
|
||||
// Do this in an explicit sign-up flow.
|
||||
AuthorizedUserRepository.instance.add(userName, password);
|
||||
} else {
|
||||
if (!authorizedUser.doCredentialsMatch(userName, password)) {
|
||||
return "Wrong username or password";
|
||||
}
|
||||
if (authorizedUser != null) {
|
||||
return "User name '" + userName + "' already in use";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static private String validatePassword(String password) {
|
||||
if (password.length() == 0) {
|
||||
return "Password needs to be non-empty";
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String connectUser(String userName, String password) throws MageException {
|
||||
String returnMessage = connectUserHandling(userName, password);
|
||||
if (returnMessage != null) {
|
||||
sendErrorMessageToClient(returnMessage);
|
||||
}
|
||||
return returnMessage;
|
||||
}
|
||||
|
||||
public boolean isLocked() {
|
||||
return lock.isLocked();
|
||||
}
|
||||
|
||||
public String connectUserHandling(String userName, String password) throws MageException {
|
||||
this.isAdmin = false;
|
||||
AuthorizedUser authorizedUser = AuthorizedUserRepository.instance.get(userName);
|
||||
if (authorizedUser == null || !authorizedUser.doCredentialsMatch(userName, password)) {
|
||||
return "Wrong username or password";
|
||||
}
|
||||
|
||||
// TODO: Do an authentication with userName and password.
|
||||
User user = UserManager.getInstance().createUser(userName, host);
|
||||
boolean reconnect = false;
|
||||
if (user == null) { // user already exists
|
||||
user = UserManager.getInstance().findUser(userName);
|
||||
// TODO: Remove this check since now we do a user authentication.
|
||||
if (user.getHost().equals(host)) {
|
||||
user.updateLastActivity(null); // minimizes possible expiration
|
||||
this.userId = user.getId();
|
||||
|
|
@ -147,7 +173,7 @@ public class Session {
|
|||
return null;
|
||||
}
|
||||
|
||||
public void registerAdmin() {
|
||||
public void connectAdmin() {
|
||||
this.isAdmin = true;
|
||||
User user = UserManager.getInstance().createUser("Admin", host);
|
||||
if (user == null) {
|
||||
|
|
|
|||
|
|
@ -70,14 +70,14 @@ public class SessionManager {
|
|||
sessions.put(sessionId, session);
|
||||
}
|
||||
|
||||
public boolean registerUser(String sessionId, String userName, String password) throws MageException {
|
||||
public boolean registerUser(String sessionId, String userName, String password, String email) throws MageException {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
String returnMessage = session.registerUser(userName, password);
|
||||
String returnMessage = session.registerUser(userName, password, email);
|
||||
if (returnMessage == null) {
|
||||
LogServiceImpl.instance.log(LogKeys.KEY_USER_CONNECTED, userName, session.getHost(), sessionId);
|
||||
LogServiceImpl.instance.log(LogKeys.KEY_USER_REGISTERED, userName, session.getHost(), sessionId);
|
||||
|
||||
logger.info(userName + " joined server");
|
||||
logger.info(userName + " registered");
|
||||
logger.debug("- userId: " + session.getUserId());
|
||||
logger.debug("- sessionId: " + sessionId);
|
||||
logger.debug("- host: " + session.getHost());
|
||||
|
|
@ -86,15 +86,36 @@ public class SessionManager {
|
|||
logger.debug(userName + " not registered: " + returnMessage);
|
||||
}
|
||||
} else {
|
||||
logger.error(userName + " tried to join with no sessionId");
|
||||
logger.error(userName + " tried to register with no sessionId");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean registerAdmin(String sessionId) {
|
||||
public boolean connectUser(String sessionId, String userName, String password) throws MageException {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
session.registerAdmin();
|
||||
String returnMessage = session.connectUser(userName, password);
|
||||
if (returnMessage == null) {
|
||||
LogServiceImpl.instance.log(LogKeys.KEY_USER_CONNECTED, userName, session.getHost(), sessionId);
|
||||
|
||||
logger.info(userName + " connected to server");
|
||||
logger.debug("- userId: " + session.getUserId());
|
||||
logger.debug("- sessionId: " + sessionId);
|
||||
logger.debug("- host: " + session.getHost());
|
||||
return true;
|
||||
} else {
|
||||
logger.debug(userName + " not connected: " + returnMessage);
|
||||
}
|
||||
} else {
|
||||
logger.error(userName + " tried to connect with no sessionId");
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean connectAdmin(String sessionId) {
|
||||
Session session = sessions.get(sessionId);
|
||||
if (session != null) {
|
||||
session.connectAdmin();
|
||||
LogServiceImpl.instance.log(LogKeys.KEY_ADMIN_CONNECTED, "Admin", session.getHost(), sessionId);
|
||||
logger.info("Admin connected from " + session.getHost());
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -7,6 +7,8 @@ public interface LogKeys {
|
|||
|
||||
String KEY_GAME_STARTED = "gameStarted";
|
||||
|
||||
String KEY_USER_REGISTERED = "userRegistered";
|
||||
|
||||
String KEY_USER_CONNECTED = "userConnected";
|
||||
|
||||
String KEY_ADMIN_CONNECTED = "adminConnected";
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue