Token Based Authentication #30

Merged
kske merged 6 commits from f/token-based-authentication into develop 2020-09-19 14:31:02 +02:00
3 changed files with 35 additions and 16 deletions
Showing only changes of commit 31cb22035b - Show all commits

View File

@ -46,6 +46,9 @@ public final class LoginScene implements EventListener {
@FXML @FXML
private Button loginButton; private Button loginButton;
@FXML
private CheckBox cbStaySignedIn;
@FXML @FXML
private Button offlineModeButton; private Button offlineModeButton;
@ -55,7 +58,7 @@ public final class LoginScene implements EventListener {
@FXML @FXML
private ImageView logo; private ImageView logo;
private boolean registration = false; private boolean registration;
private static final Logger logger = EnvoyLog.getLogger(LoginScene.class); private static final Logger logger = EnvoyLog.getLogger(LoginScene.class);
private static final ClientConfig config = ClientConfig.getInstance(); private static final ClientConfig config = ClientConfig.getInstance();
@ -76,6 +79,7 @@ public final class LoginScene implements EventListener {
@FXML @FXML
private void loginButtonPressed() { private void loginButtonPressed() {
final String user = userTextField.getText(), pass = passwordField.getText(), repeatPass = repeatPasswordField.getText(); final String user = userTextField.getText(), pass = passwordField.getText(), repeatPass = repeatPasswordField.getText();
final boolean requestToken = cbStaySignedIn.isSelected();
// Prevent registration with unequal passwords // Prevent registration with unequal passwords
if (registration && !pass.equals(repeatPass)) { if (registration && !pass.equals(repeatPass)) {
@ -86,8 +90,8 @@ public final class LoginScene implements EventListener {
userTextField.clear(); userTextField.clear();
} else { } else {
Instant lastSync = Startup.loadLastSync(userTextField.getText()); Instant lastSync = Startup.loadLastSync(userTextField.getText());
Startup.performHandshake(registration ? LoginCredentials.registration(user, pass, Startup.VERSION, lastSync) Startup.performHandshake(registration ? LoginCredentials.registration(user, pass, requestToken, Startup.VERSION, lastSync)
: LoginCredentials.login(user, pass, Startup.VERSION, lastSync)); : LoginCredentials.login(user, pass, requestToken, Startup.VERSION, lastSync));
} }
} }

View File

@ -2,6 +2,7 @@
<?import javafx.geometry.Insets?> <?import javafx.geometry.Insets?>
<?import javafx.scene.control.Button?> <?import javafx.scene.control.Button?>
<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.control.Label?> <?import javafx.scene.control.Label?>
<?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.PasswordField?>
<?import javafx.scene.control.TextField?> <?import javafx.scene.control.TextField?>
@ -32,9 +33,7 @@
<Insets bottom="5.0" left="5.0" right="5.0" top="5.0" /> <Insets bottom="5.0" left="5.0" right="5.0" top="5.0" />
</padding> </padding>
</Label> </Label>
<Label alignment="TOP_CENTER" contentDisplay="CENTER" <Label alignment="TOP_CENTER" contentDisplay="CENTER" prefHeight="33.0" prefWidth="110.0" text="LOGIN" textAlignment="CENTER">
prefHeight="33.0" prefWidth="110.0" text="LOGIN"
textAlignment="CENTER">
<font> <font>
<Font size="26.0" /> <Font size="26.0" />
</font> </font>
@ -92,6 +91,11 @@
<Insets bottom="2.0" left="125.0" right="125.0" top="2.0" /> <Insets bottom="2.0" left="125.0" right="125.0" top="2.0" />
</padding> </padding>
</Button> </Button>
<CheckBox fx:id="cbStaySignedIn" mnemonicParsing="false" text="Keep me signed in">
<VBox.margin>
<Insets bottom="10.0" top="10.0" />
</VBox.margin>
</CheckBox>
<HBox alignment="CENTER" prefHeight="30.0" prefWidth="200.0"> <HBox alignment="CENTER" prefHeight="30.0" prefWidth="200.0">
<children> <children>
<Label fx:id="registerTextLabel" text="No account yet?" /> <Label fx:id="registerTextLabel" text="No account yet?" />

View File

@ -20,16 +20,18 @@ import java.time.Instant;
public final class LoginCredentials implements Serializable { public final class LoginCredentials implements Serializable {
private final String identifier, password, clientVersion; private final String identifier, password, clientVersion;
private final boolean registration, token; private final boolean registration, token, requestToken;
private final Instant lastSync; private final Instant lastSync;
private static final long serialVersionUID = 3; private static final long serialVersionUID = 4;
private LoginCredentials(String identifier, String password, boolean registration, boolean token, String clientVersion, Instant lastSync) { private LoginCredentials(String identifier, String password, boolean registration, boolean token, boolean requestToken, String clientVersion,
Instant lastSync) {
this.identifier = identifier; this.identifier = identifier;
this.password = password; this.password = password;
this.registration = registration; this.registration = registration;
this.token = token; this.token = token;
this.requestToken = requestToken;
this.clientVersion = clientVersion; this.clientVersion = clientVersion;
this.lastSync = lastSync; this.lastSync = lastSync;
} }
@ -39,13 +41,14 @@ public final class LoginCredentials implements Serializable {
* *
* @param identifier the identifier of the user * @param identifier the identifier of the user
* @param password the password 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 clientVersion the version of the client sending these credentials
* @param lastSync the timestamp of the last synchronization * @param lastSync the timestamp of the last synchronization
* @return the created login credentials * @return the created login credentials
* @since Envoy Common v0.2-beta * @since Envoy Common v0.2-beta
*/ */
public static LoginCredentials login(String identifier, String password, String clientVersion, Instant lastSync) { public static LoginCredentials login(String identifier, String password, boolean requestToken, String clientVersion, Instant lastSync) {
return new LoginCredentials(identifier, password, false, false, clientVersion, lastSync); return new LoginCredentials(identifier, password, false, false, requestToken, clientVersion, lastSync);
} }
/** /**
@ -59,7 +62,7 @@ public final class LoginCredentials implements Serializable {
* @since Envoy Common v0.2-beta * @since Envoy Common v0.2-beta
*/ */
public static LoginCredentials loginWithToken(String identifier, String token, String clientVersion, Instant lastSync) { public static LoginCredentials loginWithToken(String identifier, String token, String clientVersion, Instant lastSync) {
return new LoginCredentials(identifier, token, false, true, clientVersion, lastSync); return new LoginCredentials(identifier, token, false, true, false, clientVersion, lastSync);
} }
/** /**
@ -67,21 +70,23 @@ public final class LoginCredentials implements Serializable {
* *
* @param identifier the identifier of the user * @param identifier the identifier of the user
* @param password the password 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 clientVersion the version of the client sending these credentials
* @param lastSync the timestamp of the last synchronization * @param lastSync the timestamp of the last synchronization
* @return the created login credentials * @return the created login credentials
* @since Envoy Common v0.2-beta * @since Envoy Common v0.2-beta
*/ */
public static LoginCredentials registration(String identifier, String password, String clientVersion, Instant lastSync) { public static LoginCredentials registration(String identifier, String password, boolean requestToken, String clientVersion, Instant lastSync) {
return new LoginCredentials(identifier, password, true, false, clientVersion, lastSync); return new LoginCredentials(identifier, password, true, false, requestToken, clientVersion, lastSync);
} }
@Override @Override
public String toString() { public String toString() {
return String.format("LoginCredentials[identifier=%s,registration=%b,token=%b,clientVersion=%s,lastSync=%s]", return String.format("LoginCredentials[identifier=%s,registration=%b,token=%b,requestToken=%b,clientVersion=%s,lastSync=%s]",
identifier, identifier,
registration, registration,
token, token,
requestToken,
clientVersion, clientVersion,
lastSync); lastSync);
} }
@ -112,6 +117,12 @@ public final class LoginCredentials implements Serializable {
*/ */
public boolean usesToken() { return token; } 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 * @return the version of the client sending these credentials
* @since Envoy Common v0.1-beta * @since Envoy Common v0.1-beta
@ -119,7 +130,7 @@ public final class LoginCredentials implements Serializable {
public String getClientVersion() { return clientVersion; } public String getClientVersion() { return clientVersion; }
/** /**
* @return the time stamp of the last synchronization * @return the timestamp of the last synchronization
* @since Envoy Common v0.2-beta * @since Envoy Common v0.2-beta
*/ */
public Instant getLastSync() { return lastSync; } public Instant getLastSync() { return lastSync; }