Removed ID artifact from LoginCredentials

This commit is contained in:
delvh 2020-02-08 09:47:59 +01:00
parent d7eb2d904c
commit 6d614580ef

View File

@ -18,7 +18,6 @@ import java.util.Formatter;
public class LoginCredentials implements Serializable {
private final String identifier;
private final long id;
private final byte[] passwordHash;
private final boolean registration;
@ -27,33 +26,17 @@ public class LoginCredentials implements Serializable {
/**
* Creates an instance of {@link LoginCredentials} for a new {@link User}.
*
* @param identifier the identifier of the user
* @param password the password of the user (will be converted to a hash)
* @param identifier the identifier of the user
* @param password the password of the user (will be converted to a hash)
* @param registration if true, the user registers himself with this
* {@link LoginCredentials}
* @throws NoSuchAlgorithmException if the algorithm used is unknown
* @since Envoy Common v0.2-alpha
*/
public LoginCredentials(String identifier, char[] password) throws NoSuchAlgorithmException {
public LoginCredentials(String identifier, char[] password, boolean registration) throws NoSuchAlgorithmException {
this.identifier = identifier;
this.id = -1;
passwordHash = getSha256(toByteArray(password));
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 = "";
this.registration = registration;
}
private byte[] getSha256(byte[] input) throws NoSuchAlgorithmException { return MessageDigest.getInstance("SHA-256").digest(input); }
@ -70,8 +53,7 @@ public class LoginCredentials implements Serializable {
@Override
public String toString() {
try (Formatter form = new Formatter()) {
if (registration) form.format("LoginCredentials[identifier=%s,passwordHash=", identifier);
else form.format("LoginCredentials[id=%d,passwordHash=", id);
form.format("LoginCredentials[identifier=%s,passwordHash=", identifier);
for (byte element : passwordHash)
form.format("%02x", element);
return form.format(",registration=%b]", registration).toString();
@ -90,12 +72,6 @@ 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