Updated LoginCredentials constructor

This commit is contained in:
delvh 2020-02-07 22:37:48 +01:00
parent 51a58a111e
commit 2cbb708dca

View File

@ -17,26 +17,43 @@ import java.util.Formatter;
*/ */
public class LoginCredentials implements Serializable { public class LoginCredentials implements Serializable {
private final String name; private final String identifier;
private final long id;
private final byte[] passwordHash; private final byte[] passwordHash;
private final boolean registration; private final boolean registration;
private static final long serialVersionUID = -7395245059059523314L; 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 identifier the identifier of the user
* @param password the password of the user (will be converted to a hash) * @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
* @throws NoSuchAlgorithmException if the algorithm used is unknown * @throws NoSuchAlgorithmException if the algorithm used is unknown
* @since Envoy Common v0.2-alpha * @since Envoy Common v0.2-alpha
*/ */
public LoginCredentials(String name, char[] password, boolean registration) throws NoSuchAlgorithmException { public LoginCredentials(String identifier, char[] password) throws NoSuchAlgorithmException {
this.name = name; this.identifier = identifier;
this.id = -1;
passwordHash = getSha256(toByteArray(password)); 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); } private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); }
@ -53,7 +70,8 @@ public class LoginCredentials implements Serializable {
@Override @Override
public String toString() { public String toString() {
try (Formatter form = new Formatter()) { 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) for (byte element : passwordHash)
form.format("%02x", element); form.format("%02x", element);
return form.format(",registration=%b]", registration).toString(); 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 * @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 * @return the password hash of the user performing the login
@ -72,6 +90,12 @@ public class LoginCredentials implements Serializable {
*/ */
public byte[] getPasswordHash() { return passwordHash; } 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 * @return {@code true} if these credentials are used for user registration
* instead of user login * instead of user login