Updated LoginCredentials constructor
This commit is contained in:
parent
3069b8cf50
commit
bcbe0aeb3f
@ -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
|
||||
|
Reference in New Issue
Block a user