138 lines
4.6 KiB
Java
138 lines
4.6 KiB
Java
package envoy.data;
|
|
|
|
import java.io.Serializable;
|
|
import java.time.Instant;
|
|
|
|
/**
|
|
* Contains a {@link User}'s login / registration information as well as the
|
|
* client version.
|
|
* <p>
|
|
* If the authentication is performed with a token, the token is stored instead
|
|
* of the password.
|
|
* <p>
|
|
* Project: <strong>envoy-common</strong><br>
|
|
* File: <strong>LoginCredentials.java</strong><br>
|
|
* Created: <strong>29.12.2019</strong><br>
|
|
*
|
|
* @author Kai S. K. Engelbart
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public final class LoginCredentials implements Serializable {
|
|
|
|
private final String identifier, password, clientVersion;
|
|
private final boolean registration, token, requestToken;
|
|
private final Instant lastSync;
|
|
|
|
private static final long serialVersionUID = 4;
|
|
|
|
private LoginCredentials(String identifier, String password, boolean registration, boolean token, boolean requestToken, String clientVersion,
|
|
Instant lastSync) {
|
|
this.identifier = identifier;
|
|
this.password = password;
|
|
this.registration = registration;
|
|
this.token = token;
|
|
this.requestToken = requestToken;
|
|
this.clientVersion = clientVersion;
|
|
this.lastSync = lastSync;
|
|
}
|
|
|
|
/**
|
|
* Creates login credentials for a regular login.
|
|
*
|
|
* @param identifier the identifier of the user
|
|
* @param password the password of the user
|
|
* @param requestToken requests the server to generate an authentication token
|
|
* @param clientVersion the version of the client sending these credentials
|
|
* @param lastSync the timestamp of the last synchronization
|
|
* @return the created login credentials
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public static LoginCredentials login(String identifier, String password, boolean requestToken, String clientVersion, Instant lastSync) {
|
|
return new LoginCredentials(identifier, password, false, false, requestToken, clientVersion, lastSync);
|
|
}
|
|
|
|
/**
|
|
* Creates login credentials for a login with an authentication token.
|
|
*
|
|
* @param identifier the identifier of the user
|
|
* @param token the authentication token of the user
|
|
* @param clientVersion the version of the client sending these credentials
|
|
* @param lastSync the timestamp of the last synchronization
|
|
* @return the created login credentials
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public static LoginCredentials loginWithToken(String identifier, String token, String clientVersion, Instant lastSync) {
|
|
return new LoginCredentials(identifier, token, false, true, false, clientVersion, lastSync);
|
|
}
|
|
|
|
/**
|
|
* Creates login credentials for a registration.
|
|
*
|
|
* @param identifier the identifier of the user
|
|
* @param password the password of the user
|
|
* @param requestToken requests the server to generate an authentication token
|
|
* @param clientVersion the version of the client sending these credentials
|
|
* @param lastSync the timestamp of the last synchronization
|
|
* @return the created login credentials
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public static LoginCredentials registration(String identifier, String password, boolean requestToken, String clientVersion, Instant lastSync) {
|
|
return new LoginCredentials(identifier, password, true, false, requestToken, clientVersion, lastSync);
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return String.format("LoginCredentials[identifier=%s,registration=%b,token=%b,requestToken=%b,clientVersion=%s,lastSync=%s]",
|
|
identifier,
|
|
registration,
|
|
token,
|
|
requestToken,
|
|
clientVersion,
|
|
lastSync);
|
|
}
|
|
|
|
/**
|
|
* @return the identifier of the user performing the login
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public String getIdentifier() { return identifier; }
|
|
|
|
/**
|
|
* @return the password of the user performing the login
|
|
* @since Envoy Common v0.1-beta
|
|
*/
|
|
public String getPassword() { return password; }
|
|
|
|
/**
|
|
* @return {@code true} if these credentials are used for user registration
|
|
* instead of user login
|
|
* @since Envoy Common v0.2-alpha
|
|
*/
|
|
public boolean isRegistration() { return registration; }
|
|
|
|
/**
|
|
* @return {@code true} if these credentials use an authentication token instead
|
|
* of a password
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public boolean usesToken() { return token; }
|
|
|
|
/**
|
|
* @return {@code true} if the server should generate a new authentication token
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public boolean requestToken() { return requestToken; }
|
|
|
|
/**
|
|
* @return the version of the client sending these credentials
|
|
* @since Envoy Common v0.1-beta
|
|
*/
|
|
public String getClientVersion() { return clientVersion; }
|
|
|
|
/**
|
|
* @return the timestamp of the last synchronization
|
|
* @since Envoy Common v0.2-beta
|
|
*/
|
|
public Instant getLastSync() { return lastSync; }
|
|
}
|