Commit cb74abb5 authored by Mathias Vehus's avatar Mathias Vehus
Browse files

Added UserName.java and Password.java and implemented them.

parent 980aa59d
No preview for this file type
......@@ -36,8 +36,8 @@ public final class Account {
* @param password The login password for this account.
**/
public static Account create(final Stored<User> user,
final String password) {
return new Account(user,List.empty(), SCryptUtil.scrypt(password, 16384, 8, 1));
final Password password) {
return new Account(user,List.empty(), SCryptUtil.scrypt(password.toString(), 16384, 8, 1));
}
/**
......
......@@ -99,8 +99,13 @@ public class Handler extends AbstractHandler
System.err.println("Registering user: \"" + username
+ "\" with password \"" + password + "\"");
inchat.register(username,password).forEach(sessionBuilder);
try {
Password pw = new Password(password);
inchat.register(new UserName(username),pw).forEach(sessionBuilder);
} catch (IllegalArgumentException e){
System.err.println("Invalid Password");
}
} catch (Maybe.NothingException e) {
// Not enough data suppied for login
System.err.println("Broken usage of register");
......@@ -214,7 +219,7 @@ public class Handler extends AbstractHandler
if(target.equals("/joinChannel")) {
out.println("<!DOCTYPE html>");
out.println("<html lang=\"en-GB\">");
printStandardHead(out, "inChat: " + account.value.user.value.name);
printStandardHead(out, "inChat: " + account.value.user.value.userName);
out.println("<body>");
printStandardTop(out, "inChat – Join a channel!");
......@@ -344,9 +349,9 @@ public class Handler extends AbstractHandler
if(target.equals("/")) {
out.println("<!DOCTYPE html>");
out.println("<html lang=\"en-GB\">");
printStandardHead(out, "inChat: " + account.value.user.value.name);
printStandardHead(out, "inChat: " + account.value.user.value.userName);
out.println("<body>");
printStandardTop(out, "inChat: " + account.value.user.value.name);
printStandardTop(out, "inChat: " + account.value.user.value.userName);
out.println("<div class=\"main\">");
printChannelList(out, account.value, "");
out.println("<div class=\"channel\">Hello!</div>");
......@@ -564,7 +569,7 @@ public class Handler extends AbstractHandler
accountStore,sessionStore,connection);
connection.setAutoCommit(false);
try {
final Stored<Session> admin = inchat.register("admin","pa$$w0rd").get();
final Stored<Session> admin = inchat.register(new UserName("admin"),new Password("pa$$w0rd")).get();
final Stored<Channel> debug = inchat.createChannel(admin.value.account, "debug").get();
(new Thread(){ public void run() {
Mutable<Stored<Channel>> chan = new Mutable<Stored<Channel>>(debug);
......
......@@ -100,11 +100,11 @@ public class InChat {
/**
* Register a new user.
*/
public Maybe<Stored<Session>> register(final String username,
final String password) {
public Maybe<Stored<Session>> register(final UserName username,
final Password password) {
return atomic(result -> {
final Stored<User> user =
userStore.save(User.create(username));
userStore.save(User.create(username.toString()));
final Stored<Account> account =
accountStore.save(Account.create(user, password));
final Stored<Session> session =
......@@ -157,7 +157,7 @@ public class InChat {
= channelStore.eventStore.save(
Channel.Event.createJoinEvent(channelID,
Instant.now(),
account.value.user.value.name));
account.value.user.value.userName.toString()));
result.accept(
Util.updateSingle(channel,
channelStore,
......@@ -175,7 +175,7 @@ public class InChat {
Stored<Channel.Event> event
= channelStore.eventStore.save(
Channel.Event.createMessageEvent(channel.identity,Instant.now(),
account.value.user.value.name, message));
account.value.user.value.userName.toString(), message));
result.accept (
Util.updateSingle(channel,
channelStore,
......
package inf226.inchat;
public class Password {
private String password;
public Password(String password) throws IllegalArgumentException{
if (checkPassword(password)) {
this.password = password;
} else {
throw new IllegalArgumentException("Invalid Password");
}
}
private boolean checkPassword(String password) {
return password.length() >= 8;
}
@Override
public String toString() {
return password;
}
}
......@@ -6,12 +6,12 @@ import java.time.Instant;
* about a user.
**/
public final class User {
public final String name;
public final UserName userName;
public final Instant joined;
public User(String name,
public User(UserName userName,
Instant joined) {
this.name = name;
this.userName = userName;
this.joined = joined;
}
......@@ -19,7 +19,7 @@ public final class User {
* Create a new user.
*/
public static User create(String name) {
return new User(name, Instant.now());
return new User(new UserName(name), Instant.now());
}
}
package inf226.inchat;
public class UserName {
private String userName;
public UserName(String userName){
this.userName = userName;
}
@Override
public String toString() {
return userName;
}
}
......@@ -32,7 +32,7 @@ public final class UserStorage
PreparedStatement statement = connection.prepareStatement("INSERT INTO User VALUES(?,?,?,?)");
statement.setObject(1, stored.identity);
statement.setObject(2, stored.version);
statement.setString(3, user.name);
statement.setString(3, user.userName.toString());
statement.setString(4, user.joined.toString());
statement.executeUpdate();
return stored;
......@@ -50,7 +50,7 @@ public final class UserStorage
PreparedStatement statement = connection.prepareStatement("UPDATE User SET (version,name,joined) =(?,?,?) WHERE id=?");
statement.setObject(1, updated.version);
statement.setString(2, new_user.name);
statement.setString(2, new_user.userName.toString());
statement.setString(3, new_user.joined.toString());
statement.setObject(4, updated.identity);
statement.executeUpdate();
......@@ -89,7 +89,7 @@ public final class UserStorage
final String name = rs.getString("name");
final Instant joined = Instant.parse(rs.getString("joined"));
return (new Stored<User>
(new User(name,joined),id,version));
(new User(new UserName(name),joined),id,version));
} else {
throw new DeletedException();
}
......
......@@ -31,8 +31,8 @@ public class InchatTest{
= new SessionStorage(connection,accountStore);
connection.setAutoCommit(false);
InChat inchat = new InChat(userStore,channelStore,accountStore,sessionStore,connection);
Stored<Session> aliceSession = inchat.register("Alice","badpassword").get();
inchat.register("Bob","worse").get();
Stored<Session> aliceSession = inchat.register(new UserName("Alice"),new Password("badpassword")).get();
inchat.register(new UserName("Bob"),new Password("worse")).get();
Stored<Session> bobSession = inchat.login("Bob","worse").get();
Stored<Channel> channel = inchat.createChannel(aliceSession.value.account,"Awesome").get();
inchat.postMessage(aliceSession.value.account,channel, "Test message.").get();
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment