Archived
1
0
Fork 0

Add documentation to server package

This commit is contained in:
Ethanell 2022-01-19 10:41:34 +01:00
parent b5e8b96f65
commit 54b1eb7299
9 changed files with 325 additions and 22 deletions

View file

@ -17,6 +17,9 @@ import java.net.Socket;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
/**
* Server client connection management
*/
public class ConnectedClient implements Runnable { public class ConnectedClient implements Runnable {
private static int idCounter = 0; private static int idCounter = 0;
private final int id = idCounter++; private final int id = idCounter++;
@ -26,6 +29,12 @@ public class ConnectedClient implements Runnable {
private ObjectInputStream in; private ObjectInputStream in;
private UserModel user; private UserModel user;
/**
* Create a client connection management
* @param server server socket
* @param socket client socket
* @throws IOException if a connection error occur with the client
*/
ConnectedClient(Server server, Socket socket) throws IOException { ConnectedClient(Server server, Socket socket) throws IOException {
this.server = server; this.server = server;
this.socket = socket; this.socket = socket;
@ -33,12 +42,31 @@ public class ConnectedClient implements Runnable {
this.in = new ObjectInputStream(socket.getInputStream()); this.in = new ObjectInputStream(socket.getInputStream());
} }
public Message sendMessage(Message message) throws IOException { /**
out.writeObject(new Command(CommandType.message, List.of(message))); * Send command to the client
* @param cmd the command
* @throws IOException if a connection error occur with the client
*/
private void send(Command cmd) throws IOException {
out.writeObject(cmd);
out.flush(); out.flush();
return message;
} }
/**
* Send a message to the client
* @param message the message
* @throws IOException if a connection error occur with the client
*/
public void sendMessage(Message message) throws IOException {
send(new Command(CommandType.message, List.of(message)));
}
/**
* Client command handler
* @param command the command
* @throws IOException if a connection error occur with the client
* @throws ChatException chat runtime error send to the user
*/
private void actionCommand(Command command) throws IOException, ChatException { private void actionCommand(Command command) throws IOException, ChatException {
CommandType type = command.getType(); CommandType type = command.getType();
if (user == null && type != CommandType.login) if (user == null && type != CommandType.login)
@ -53,7 +81,14 @@ public class ConnectedClient implements Runnable {
} }
} }
private void commandLogin(Command cmd) throws IOException, ChatException { /**
* Handel's user authentication
* ToDo avoid re auth
* @param cmd the login command
* @throws IOException if a connection error occur with the client
* @throws LoginInvalid if the user credentials are invalid
*/
private void commandLogin(Command cmd) throws IOException, LoginInvalid {
List<Object> args = cmd.getArgs(); List<Object> args = cmd.getArgs();
String username = (String) args.get(0); String username = (String) args.get(0);
@ -71,13 +106,17 @@ public class ConnectedClient implements Runnable {
else if (!user.checkPassword(password)) else if (!user.checkPassword(password))
throw new LoginInvalid("Password invalid"); throw new LoginInvalid("Password invalid");
else { else {
out.writeObject(new Command(CommandType.login, null)); send(new Command(CommandType.login, null));
out.flush();
this.user = user; this.user = user;
System.out.println("Client "+user.getUsername()+" is connected !"); System.out.println("Client "+user.getUsername()+" is connected !");
} }
} }
/**
* Message receive handler
* @param cmd the message command
* @throws NotInChannel if the user is not in the channel
*/
private void commandMessage(Command cmd) throws NotInChannel { private void commandMessage(Command cmd) throws NotInChannel {
Message msg = (Message) cmd.getArgs().get(0); Message msg = (Message) cmd.getArgs().get(0);
msg.setSender(this.user); msg.setSender(this.user);
@ -89,16 +128,27 @@ public class ConnectedClient implements Runnable {
server.broadcastMessage(msg, id); server.broadcastMessage(msg, id);
} }
/**
* Command user list handler
* @throws IOException if a connection error occur with the client
*/
private void commandList() throws IOException { private void commandList() throws IOException {
out.writeObject(new Command(CommandType.list, Collections.singletonList(server.getUsers()))); send(new Command(CommandType.list, Collections.singletonList(server.getUsers())));
out.flush();
} }
/**
* Command channel list handler
* @throws IOException if a connection error occur with the client
*/
private void commandListChannels() throws IOException { private void commandListChannels() throws IOException {
out.writeObject(new Command(CommandType.listChannels, Collections.singletonList((List<Channel>)(List<?>) ChannelModel.getAll()))); send(new Command(CommandType.listChannels, Collections.singletonList((List<Channel>)(List<?>) ChannelModel.getAll())));
out.flush();
} }
/**
* Channel join command handler
* @param cmd the join command
* @throws IOException if a connection error occur with the client
*/
private void commandJoin(Command cmd) throws IOException { private void commandJoin(Command cmd) throws IOException {
String name = (String) cmd.getArgs().get(0); String name = (String) cmd.getArgs().get(0);
ChannelModel chan = ChannelModel.get(name); ChannelModel chan = ChannelModel.get(name);
@ -110,12 +160,14 @@ public class ConnectedClient implements Runnable {
if (!chan.have(user)) if (!chan.have(user))
chan.addUser(user); chan.addUser(user);
out.writeObject(new Command(CommandType.join, List.of((Channel) chan))); send(new Command(CommandType.join, List.of((Channel) chan)));
out.flush();
server.broadcastMessage(new Message(chan, Server.getServerUser(), user.getUsername()+" joined the channel !"), -1); server.broadcastMessage(new Message(chan, Server.getServerUser(), user.getUsername()+" joined the channel !"), -1);
} }
/**
* Man thread of user connection
*/
public void run() { public void run() {
try { try {
while (true) { while (true) {
@ -143,6 +195,10 @@ public class ConnectedClient implements Runnable {
} }
} }
/**
* Close connection to client
* @throws IOException if a connection error occur with the client
*/
public void closeClient() throws IOException { public void closeClient() throws IOException {
if (in != null) if (in != null)
in.close(); in.close();
@ -150,10 +206,18 @@ public class ConnectedClient implements Runnable {
socket.close(); socket.close();
} }
/**
* Get the client connection id
* @return connection id
*/
public int getId() { public int getId() {
return id; return id;
} }
/**
* Get the user
* @return the user
*/
public User getUser() { public User getUser() {
return user; return user;
} }

View file

@ -7,15 +7,28 @@ import java.io.IOException;
import java.net.ServerSocket; import java.net.ServerSocket;
import java.net.Socket; import java.net.Socket;
/**
* Server connection manager
*/
public class Connection implements Runnable { public class Connection implements Runnable {
private final Server server; private final Server server;
private final ServerSocket serverSocket; private final ServerSocket serverSocket;
/**
* Create a server connection manager
* @param server the server
* @throws IOException if a connection error occur with the client
*/
Connection(Server server) throws IOException { Connection(Server server) throws IOException {
this.server = server; this.server = server;
this.serverSocket = initSSL(); this.serverSocket = initSSL();
} }
/**
* Initialise the SSL client WebSocket connection
* @return the socket
* @throws IOException if a connection error occur with the client
*/
private SSLServerSocket initSSL() throws IOException { private SSLServerSocket initSSL() throws IOException {
SSLContext ctx = ChatSSL.getSSLContext(); SSLContext ctx = ChatSSL.getSSLContext();
@ -29,6 +42,9 @@ public class Connection implements Runnable {
return sslListener; return sslListener;
} }
/**
* Main thread
*/
public void run() { public void run() {
while (true) { while (true) {
Socket clientSocket; Socket clientSocket;

View file

@ -10,10 +10,16 @@ import java.sql.DriverManager;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.Properties; import java.util.Properties;
/**
* Server database management
*/
public class Database { public class Database {
private static Database database; private static Database database;
private Connection connection; private Connection connection;
/**
* Create database object and establish connection
*/
private Database() { private Database() {
Database.database = this; Database.database = this;
try { try {
@ -26,7 +32,12 @@ public class Database {
init(); init();
} }
private String[] getCredentials() throws NullPointerException, IOException { /**
* Get database credentials
* @return credentials
* @throws IOException when an error occur with the configuration file
*/
private String[] getCredentials() throws IOException {
Properties props = new Properties(); Properties props = new Properties();
File f = new File("server.properties"); File f = new File("server.properties");
@ -43,6 +54,12 @@ public class Database {
return new String[]{props.getProperty("db.url"), props.getProperty("db.user"), props.getProperty("db.password")}; return new String[]{props.getProperty("db.url"), props.getProperty("db.user"), props.getProperty("db.password")};
} }
/**
* Get the database connection
* @return the connection
* @throws SQLException if a connection error occur with the database
* @throws IOException when failed to get the credentials
*/
private Connection getConnexion() throws SQLException, IOException { private Connection getConnexion() throws SQLException, IOException {
String[] credentials = getCredentials(); String[] credentials = getCredentials();
@ -56,16 +73,27 @@ public class Database {
return DriverManager.getConnection(credentials[0], credentials[1], credentials[2]); return DriverManager.getConnection(credentials[0], credentials[1], credentials[2]);
} }
/**
* Get the database connection
* @return the connection
*/
public Connection getConnection() { public Connection getConnection() {
return connection; return connection;
} }
/**
* Get the database instance
* @return the database
*/
public static Database getDatabase() { public static Database getDatabase() {
if (Database.database == null) if (Database.database == null)
return new Database(); return new Database();
return Database.database; return Database.database;
} }
/**
* Initialise the database tables from models
*/
private void init() { private void init() {
UserModel.generateTable(); UserModel.generateTable();
ChannelModel.generateTable(); ChannelModel.generateTable();

View file

@ -2,6 +2,9 @@ package fr.univ.lyon1.server;
import java.io.IOException; import java.io.IOException;
/**
* Main server program
*/
public class MainServer { public class MainServer {
public static void main(String[] args) { public static void main(String[] args) {
try { try {
@ -16,6 +19,9 @@ public class MainServer {
} }
} }
/**
* Help usage for arguments
*/
private static void printUsage() { private static void printUsage() {
System.out.println("java server.Server <port>"); System.out.println("java server.Server <port>");
System.out.println("\t<port>: server's port"); System.out.println("\t<port>: server's port");

View file

@ -9,11 +9,19 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* Main server management
*/
public class Server { public class Server {
private final int port; private final int port;
private List<ConnectedClient> clients = new ArrayList<>(); private final List<ConnectedClient> clients = new ArrayList<>();
private static User serverUser = new User(UUID.fromString("3539b6bf-5eb3-41d4-893f-cbf0caa9ca74"), "server"); private static final User serverUser = new User(UUID.fromString("3539b6bf-5eb3-41d4-893f-cbf0caa9ca74"), "server");
/**
* Create server
* @param port the listening port
* @throws IOException if a connection error occur
*/
Server(int port) throws IOException { Server(int port) throws IOException {
this.port = port; this.port = port;
Database.getDatabase(); Database.getDatabase();
@ -21,12 +29,20 @@ public class Server {
connection.start(); connection.start();
} }
public ConnectedClient addClient(ConnectedClient newClient) { /**
* Add client handler
* @param newClient the client
*/
public void addClient(ConnectedClient newClient) {
clients.add(newClient); clients.add(newClient);
return newClient;
} }
public int broadcastMessage(Message message, int id) { /**
* Send a message to all clients
* @param message the message
* @param id the sender id
*/
public void broadcastMessage(Message message, int id) {
List<UUID> users = UserChannelModel.getUsers(message.getChannel()).stream().map(User::getUUID).toList(); List<UUID> users = UserChannelModel.getUsers(message.getChannel()).stream().map(User::getUUID).toList();
for (ConnectedClient client : clients.stream().filter(connectedClient -> users.contains(connectedClient.getUser().getUUID())).toList()) { for (ConnectedClient client : clients.stream().filter(connectedClient -> users.contains(connectedClient.getUser().getUUID())).toList()) {
if (id == -1 || client.getId() != id) if (id == -1 || client.getId() != id)
@ -37,10 +53,13 @@ public class Server {
e.printStackTrace(); e.printStackTrace();
} }
} }
return id;
} }
public ConnectedClient disconnectedClient(ConnectedClient client) { /**
* Close client connection
* @param client the client connection manager
*/
public void disconnectedClient(ConnectedClient client) {
try { try {
client.closeClient(); client.closeClient();
} catch (IOException e) { } catch (IOException e) {
@ -51,17 +70,28 @@ public class Server {
clients.remove(client); clients.remove(client);
System.out.println("Client "+client.getId()+" disconnected"); System.out.println("Client "+client.getId()+" disconnected");
return client;
} }
/**
* Get the server listening port
* @return the server listening port
*/
public int getPort() { public int getPort() {
return port; return port;
} }
/**
* Get the server user
* @return the server user
*/
public static User getServerUser() { public static User getServerUser() {
return serverUser; return serverUser;
} }
/**
* Get the list of connection client to the server
* @return list of connected client to the server
*/
public List<User> getUsers() { public List<User> getUsers() {
return clients.stream().map(ConnectedClient::getUser).toList(); return clients.stream().map(ConnectedClient::getUser).toList();
} }

View file

@ -10,26 +10,53 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* Database model of a channel
*/
public class ChannelModel extends Channel implements Model { public class ChannelModel extends Channel implements Model {
private static final String TABLE_NAME = "Channel"; private static final String TABLE_NAME = "Channel";
/**
* Create a new channel from a name
* @param name the name
*/
public ChannelModel(String name) { public ChannelModel(String name) {
super(name); super(name);
create(); create();
} }
/**
* Model from existing channel
* @param uuid
* @param name
*/
private ChannelModel(UUID uuid, String name) { private ChannelModel(UUID uuid, String name) {
super(uuid, name); super(uuid, name);
} }
/**
* Add a user to the channel
* ToDo on user reconnection rejoin all connected channels
* @param user the user
*/
public void addUser(User user) { public void addUser(User user) {
new UserChannelModel(user, this); new UserChannelModel(user, this);
} }
/**
* Check if a user is in this channel
* @param user the user
* @return true if is else false
*/
public boolean have(User user) { public boolean have(User user) {
return UserChannelModel.exist(user, this); return UserChannelModel.exist(user, this);
} }
/**
* Get a channel from a name
* @param name the name
* @return the channel or null if not found
*/
public static ChannelModel get(String name) { public static ChannelModel get(String name) {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE name = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE name = ?");
@ -46,6 +73,11 @@ public class ChannelModel extends Channel implements Model {
return null; return null;
} }
/**
* Get a channel from the unique id
* @param uuid the unique id
* @return the channel or null if not found
*/
public static ChannelModel get(UUID uuid) { public static ChannelModel get(UUID uuid) {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE UUID = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE UUID = ?");
@ -65,6 +97,10 @@ public class ChannelModel extends Channel implements Model {
return null; return null;
} }
/**
* Get all channels
* @return a list of channels
*/
public static List<ChannelModel> getAll() { public static List<ChannelModel> getAll() {
List<ChannelModel> channels = new ArrayList<>(); List<ChannelModel> channels = new ArrayList<>();
try { try {
@ -84,6 +120,10 @@ public class ChannelModel extends Channel implements Model {
return channels; return channels;
} }
/**
* Check of the channel exists in the database
* @return true if the channel exists else false
*/
private boolean exist() { private boolean exist() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?");
@ -96,6 +136,10 @@ public class ChannelModel extends Channel implements Model {
} }
} }
/**
* Register the channel in the database
* @return true if the register is successful else false
*/
private boolean create() { private boolean create() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (UUID, name) VALUES (?, ?)"); PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (UUID, name) VALUES (?, ?)");
@ -108,6 +152,10 @@ public class ChannelModel extends Channel implements Model {
} }
} }
/**
* Update the channel in the database
* @return true if the update is successful else false
*/
public boolean save() { public boolean save() {
if (!exist()) if (!exist())
return create(); return create();
@ -122,6 +170,9 @@ public class ChannelModel extends Channel implements Model {
} }
} }
/**
* Generate the channel model table in the database
*/
public static void generateTable() { public static void generateTable() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, name varchar(16) unique )"); PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, name varchar(16) unique )");

View file

@ -2,7 +2,9 @@ package fr.univ.lyon1.server.models;
import fr.univ.lyon1.server.Database; import fr.univ.lyon1.server.Database;
/**
* Base model of a database type
*/
public interface Model { public interface Model {
Database database = Database.getDatabase(); Database database = Database.getDatabase();
} }

View file

@ -10,12 +10,20 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
/**
* Database model of the relation between user and channel
*/
public class UserChannelModel implements Model { public class UserChannelModel implements Model {
private User user; private User user;
private Channel channel; private Channel channel;
private static final String TABLE_NAME = "UserChannel"; private static final String TABLE_NAME = "UserChannel";
/**
* Create a user channel relation and save it in database if necessary
* @param user
* @param channel
*/
public UserChannelModel(User user, Channel channel) { public UserChannelModel(User user, Channel channel) {
this.user = user; this.user = user;
this.channel = channel; this.channel = channel;
@ -24,6 +32,11 @@ public class UserChannelModel implements Model {
create(); create();
} }
/**
* Get the list of users in a specific channel
* @param channel the channel
* @return the list of users
*/
public static List<User> getUsers(Channel channel) { public static List<User> getUsers(Channel channel) {
List<User> users = new ArrayList<>(); List<User> users = new ArrayList<>();
@ -42,6 +55,11 @@ public class UserChannelModel implements Model {
return users; return users;
} }
/**
* Get a list of channel where a user is in
* @param user the user
* @return the list of channels
*/
public static List<Channel> getChannels(User user) { public static List<Channel> getChannels(User user) {
List<Channel> channels = new ArrayList<>(); List<Channel> channels = new ArrayList<>();
@ -60,6 +78,12 @@ public class UserChannelModel implements Model {
return channels; return channels;
} }
/**
* Check if the relation exists in the database
* @param user the user
* @param channel the channel
* @return true if present else false
*/
public static boolean exist(User user, Channel channel) { public static boolean exist(User user, Channel channel) {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT 1 FROM "+TABLE_NAME+" WHERE userUUID = ? AND channelUUID = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT 1 FROM "+TABLE_NAME+" WHERE userUUID = ? AND channelUUID = ?");
@ -73,6 +97,10 @@ public class UserChannelModel implements Model {
} }
} }
/**
* Save the ration in the database
* @return true if succeed else false
*/
private boolean create() { private boolean create() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (userUUID, channelUUID) VALUES (?, ?)"); PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (userUUID, channelUUID) VALUES (?, ?)");
@ -85,6 +113,9 @@ public class UserChannelModel implements Model {
} }
} }
/**
* Generate the user channel relation model table in the database
*/
public static void generateTable() { public static void generateTable() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (userUUID varchar(40) not null references User(UUID), channelUUID varchar(40) not null references Channel(UUID), PRIMARY KEY (userUUID, channelUUID))"); PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" (userUUID varchar(40) not null references User(UUID), channelUUID varchar(40) not null references Channel(UUID), PRIMARY KEY (userUUID, channelUUID))");

View file

@ -17,6 +17,9 @@ import java.util.UUID;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
/**
* Database model of a user type
*/
public class UserModel extends User implements Model { public class UserModel extends User implements Model {
private String passwordHash; private String passwordHash;
@ -28,17 +31,33 @@ public class UserModel extends User implements Model {
private static final Pattern LAYOUT = Pattern.compile("\\$1\\$(\\d\\d?)\\$(.{43})"); private static final Pattern LAYOUT = Pattern.compile("\\$1\\$(\\d\\d?)\\$(.{43})");
private static final String TABLE_NAME = "User"; private static final String TABLE_NAME = "User";
/**
* Create a new user
* @param username the username
* @param password the password
*/
public UserModel(String username, String password) { public UserModel(String username, String password) {
super(username); super(username);
setPassword(password); setPassword(password);
create(); create();
} }
/**
* Create an existing user
* @param uuid the unique id
* @param username the username
* @param passwordHash the password hash
*/
private UserModel(UUID uuid, String username, String passwordHash) { private UserModel(UUID uuid, String username, String passwordHash) {
super(uuid, username); super(uuid, username);
this.passwordHash = passwordHash; this.passwordHash = passwordHash;
} }
/**
* Get a user from his username
* @param username the username
* @return the user of null if not found
*/
public static UserModel get(String username) { public static UserModel get(String username) {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE username = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE username = ?");
@ -55,6 +74,11 @@ public class UserModel extends User implements Model {
return null; return null;
} }
/**
* Get a user from his unique id
* @param uuid the unique id
* @return the user of null if not found
*/
public static UserModel get(UUID uuid) { public static UserModel get(UUID uuid) {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE UUID = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT * FROM "+TABLE_NAME+" WHERE UUID = ?");
@ -75,6 +99,10 @@ public class UserModel extends User implements Model {
return null; return null;
} }
/**
* Check if the user exists in the database
* @return true if present else false
*/
private boolean exist() { private boolean exist() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?"); PreparedStatement ps = database.getConnection().prepareStatement("SELECT UUID FROM "+TABLE_NAME+" WHERE UUID = ?");
@ -87,6 +115,10 @@ public class UserModel extends User implements Model {
} }
} }
/**
* Create a user in the database
* @return true if the update is successful else false
*/
private boolean create() { private boolean create() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (UUID, username, password) VALUES (?, ?, ?)"); PreparedStatement ps = database.getConnection().prepareStatement("INSERT INTO "+TABLE_NAME+" (UUID, username, password) VALUES (?, ?, ?)");
@ -100,6 +132,10 @@ public class UserModel extends User implements Model {
} }
} }
/**
* Update a user in the database
* @return true if the update is successful else false
*/
public boolean save() { public boolean save() {
if (!exist()) if (!exist())
return create(); return create();
@ -116,6 +152,9 @@ public class UserModel extends User implements Model {
} }
} }
/**
* Generate the channel model table in the database
*/
public static void generateTable() { public static void generateTable() {
try { try {
PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, username varchar(16) unique, password varchar(256) )"); PreparedStatement ps = database.getConnection().prepareStatement("CREATE TABLE IF NOT EXISTS "+TABLE_NAME+" ( UUID varchar(40) primary key, username varchar(16) unique, password varchar(256) )");
@ -125,41 +164,77 @@ public class UserModel extends User implements Model {
} }
} }
/**
* Get the password hash
* @return password hash
*/
public String getPasswordHash() { public String getPasswordHash() {
return passwordHash; return passwordHash;
} }
/**
* Set the password as a hash
* @param password the plain password
*/
public void setPassword(String password) { public void setPassword(String password) {
// Generate a new salt
byte[] passwordSalt = new byte[SIZE / 8]; byte[] passwordSalt = new byte[SIZE / 8];
random.nextBytes(passwordSalt); random.nextBytes(passwordSalt);
// Generate the hash from the password and the salt
byte[] dk = pbkdf2(password.toCharArray(), passwordSalt, 1 << COST); byte[] dk = pbkdf2(password.toCharArray(), passwordSalt, 1 << COST);
byte[] hash = new byte[passwordSalt.length + dk.length]; byte[] hash = new byte[passwordSalt.length + dk.length];
System.arraycopy(passwordSalt, 0, hash, 0, passwordSalt.length); System.arraycopy(passwordSalt, 0, hash, 0, passwordSalt.length);
System.arraycopy(dk, 0, hash, passwordSalt.length, dk.length); System.arraycopy(dk, 0, hash, passwordSalt.length, dk.length);
Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding(); Base64.Encoder enc = Base64.getUrlEncoder().withoutPadding();
// Format the password hash
passwordHash = ID + COST + '$' + enc.encodeToString(hash); passwordHash = ID + COST + '$' + enc.encodeToString(hash);
} }
/**
* Check a password against the password hash
* @param password the plain password to test
* @return true if the password match else false
*/
public boolean checkPassword(String password) { public boolean checkPassword(String password) {
// Check the password hash integrity
Matcher m = LAYOUT.matcher(passwordHash); Matcher m = LAYOUT.matcher(passwordHash);
if (!m.matches()) if (!m.matches())
throw new IllegalArgumentException("Invalid token format"); throw new IllegalArgumentException("Invalid token format");
// Gather hash data
int iterations = iterations(Integer.parseInt(m.group(1))); int iterations = iterations(Integer.parseInt(m.group(1)));
byte[] hash = Base64.getUrlDecoder().decode(m.group(2)); byte[] hash = Base64.getUrlDecoder().decode(m.group(2));
byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8); byte[] salt = Arrays.copyOfRange(hash, 0, SIZE / 8);
byte[] check = pbkdf2(password.toCharArray(), salt, iterations); byte[] check = pbkdf2(password.toCharArray(), salt, iterations);
// Check if the password match the hash
int zero = 0; int zero = 0;
for (int idx = 0; idx < check.length; ++idx) for (int idx = 0; idx < check.length; ++idx)
zero |= hash[salt.length + idx] ^ check[idx]; zero |= hash[salt.length + idx] ^ check[idx];
return zero == 0; return zero == 0;
} }
/**
* Get the hash iteration
* @param cost the has cost
* @return the iterations
*/
private static int iterations(int cost) { private static int iterations(int cost) {
if ((cost < 0) || (cost > 30)) if ((cost < 0) || (cost > 30))
throw new IllegalArgumentException("cost: " + cost); throw new IllegalArgumentException("cost: " + cost);
return 1 << cost; return 1 << cost;
} }
/**
* Generate the password encryption
* @param password the plain password
* @param salt the salt
* @param iterations the hash iterations
* @return the password encoded hash
*/
private static byte[] pbkdf2(char[] password, byte[] salt, int iterations) { private static byte[] pbkdf2(char[] password, byte[] salt, int iterations) {
KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE); KeySpec spec = new PBEKeySpec(password, salt, iterations, SIZE);
try { try {