From 2cbb708dcab3aff63b2815c30ccecf592f5c17be Mon Sep 17 00:00:00 2001 From: delvh Date: Fri, 7 Feb 2020 22:37:48 +0100 Subject: [PATCH] Updated LoginCredentials constructor --- .../java/envoy/data/LoginCredentials.java | 48 ++++++++++++++----- 1 file changed, 36 insertions(+), 12 deletions(-) diff --git a/src/main/java/envoy/data/LoginCredentials.java b/src/main/java/envoy/data/LoginCredentials.java index 9b29c07..e9b9b1c 100644 --- a/src/main/java/envoy/data/LoginCredentials.java +++ b/src/main/java/envoy/data/LoginCredentials.java @@ -17,26 +17,43 @@ import java.util.Formatter; */ public class LoginCredentials implements Serializable { - private final String name; + private final String identifier; + private final long id; private final byte[] passwordHash; private final boolean registration; private static final long serialVersionUID = -7395245059059523314L; /** - * Creates an in stance of {@link LoginCredentials}. + * Creates an instance of {@link LoginCredentials} for a new {@link User}. * - * @param name the name of the user - * @param password the password of the user (will be converted to a hash) - * @param registration signifies that these credentials are used for user - * registration instead of user login + * @param identifier the identifier of the user + * @param password the password of the user (will be converted to a hash) * @throws NoSuchAlgorithmException if the algorithm used is unknown * @since Envoy Common v0.2-alpha */ - public LoginCredentials(String name, char[] password, boolean registration) throws NoSuchAlgorithmException { - this.name = name; + public LoginCredentials(String identifier, char[] password) throws NoSuchAlgorithmException { + this.identifier = identifier; + this.id = -1; passwordHash = getSha256(toByteArray(password)); - this.registration = registration; + this.registration = true; + } + + /** + * Creates an instance of {@link LoginCredentials} for an {@link User} who + * already registered himself (knows his id). + * + * @param id the id of the user + * @param password the password of the user (will be converted to a hash) + * @throws NoSuchAlgorithmException if the algorithm used is unknown + * @since Envoy Common v0.2-alpha + */ + public LoginCredentials(long id, char[] password) throws NoSuchAlgorithmException { + if (id <= 0) throw new IllegalArgumentException("Entered an illegal Id. Ids can not be below 1. Id was " + id); + this.id = id; + passwordHash = getSha256(toByteArray(password)); + this.registration = false; + this.identifier = ""; } private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); } @@ -53,7 +70,8 @@ public class LoginCredentials implements Serializable { @Override public String toString() { try (Formatter form = new Formatter()) { - form.format("LoginCredentials[name=%s,passwordHash=", name); + if (registration) form.format("LoginCredentials[identifier=%s,passwordHash=", identifier); + else form.format("LoginCredentials[id=%d,passwordHash=", id); for (byte element : passwordHash) form.format("%02x", element); return form.format(",registration=%b]", registration).toString(); @@ -61,10 +79,10 @@ public class LoginCredentials implements Serializable { } /** - * @return the name of the user performing the login + * @return the identifier of the user performing the login * @since Envoy Common v0.2-alpha */ - public String getName() { return name; } + public String getIdentifier() { return identifier; } /** * @return the password hash of the user performing the login @@ -72,6 +90,12 @@ public class LoginCredentials implements Serializable { */ public byte[] getPasswordHash() { return passwordHash; } + /** + * @return the id of the underlying user + * @since Envoy Common v0.2-alpha + */ + public long getId() { return id; } + /** * @return {@code true} if these credentials are used for user registration * instead of user login