@@ -27,7 +27,7 @@ public abstract class Connection implements AutoCloseable {
27
27
private final PropertyChangeSupport support ;
28
28
29
29
private TweetNaclFast .Box box ;
30
- private Credentials credentials ;
30
+ private Optional < Credentials > credentials ;
31
31
private final String clientID ;
32
32
private static final int nonceLength = 24 ;
33
33
private byte [] nonce ;
@@ -87,6 +87,7 @@ public Connection() {
87
87
new Random ().nextBytes (array );
88
88
clientID = b64encode (array );
89
89
nonce = TweetNaclFast .randombytes (nonceLength );
90
+ credentials = Optional .empty ();
90
91
support = new PropertyChangeSupport (this );
91
92
scheduler = Executors .newSingleThreadScheduledExecutor ();
92
93
}
@@ -115,18 +116,18 @@ public void run() {
115
116
if (!isSignal (response )) LOG .trace ("Response added to queue: {}" , response );
116
117
queue .offer (response );
117
118
errorCount = 0 ;
118
- continue ;
119
- }
120
- errorCount ++;
121
- if (errorCount > MAX_ERROR_COUNT ) {
122
- LOG .info ("Too much errors - stopping MessagePublisher" );
123
- doStop ();
124
- try {
125
- terminateConnection ();
126
- } catch (IOException e ) {
127
- LOG .error (e .toString (), e .getCause ());
119
+ } else {
120
+ errorCount ++;
121
+ if (errorCount > MAX_ERROR_COUNT ) {
122
+ LOG .info ("Too much errors - stopping MessagePublisher" );
123
+ doStop ();
124
+ try {
125
+ terminateConnection ();
126
+ } catch (IOException e ) {
127
+ LOG .error (e .toString (), e .getCause ());
128
+ }
129
+ reconnect ();
128
130
}
129
- reconnect ();
130
131
}
131
132
}
132
133
LOG .debug ("MessagePublisher stopped" );
@@ -269,13 +270,8 @@ private synchronized byte[] sendEncryptedMessage(Map<String, Object> msg) throws
269
270
throw new IllegalStateException (NOT_CONNECTED );
270
271
}
271
272
272
- var publicKey = Optional .ofNullable (credentials ).orElseThrow (
273
- () -> new IllegalStateException (KEYEXCHANGE_MISSING )
274
- ).getServerPublicKey ();
275
-
276
- var keyPair = Optional .ofNullable (credentials ).orElseThrow (
277
- () -> new IllegalStateException (KEYEXCHANGE_MISSING )
278
- ).getOwnKeypair ();
273
+ var publicKey = credentials .orElseThrow (() -> new IllegalStateException (KEYEXCHANGE_MISSING )).getServerPublicKey ();
274
+ var keyPair = credentials .orElseThrow (() -> new IllegalStateException (KEYEXCHANGE_MISSING )).getOwnKeypair ();
279
275
280
276
if (msg .containsKey ("triggerUnlock" ) && msg .get ("triggerUnlock" ).equals ("true" )) {
281
277
msg .remove ("triggerUnlock" );
@@ -390,11 +386,11 @@ protected void changePublicKeys() throws IOException, KeepassProxyAccessExceptio
390
386
var publicKey = b64decode (response .getString ("publicKey" ).getBytes ());
391
387
box = new TweetNaclFast .Box (publicKey , keyPair .getSecretKey ());
392
388
393
- if (Optional . ofNullable ( credentials ) .isEmpty ()) {
394
- setCredentials (new Credentials ());
389
+ if (credentials .isEmpty ()) {
390
+ setCredentials (Optional . of ( new Credentials () ));
395
391
}
396
- Optional . ofNullable ( credentials ) .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setOwnKeypair (keyPair );
397
- Optional . ofNullable ( credentials ) .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setServerPublicKey (publicKey );
392
+ credentials .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setOwnKeypair (keyPair );
393
+ credentials .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setServerPublicKey (publicKey );
398
394
support .firePropertyChange ("credentialsCreated" , null , credentials );
399
395
400
396
}
@@ -408,8 +404,7 @@ protected void changePublicKeys() throws IOException, KeepassProxyAccessExceptio
408
404
*/
409
405
public void associate () throws IOException , KeepassProxyAccessException {
410
406
var idKeyPair = TweetNaclFast .Box .keyPair ();
411
- var keyPair = Optional .ofNullable (credentials ).orElseThrow (
412
- () -> new IllegalStateException (KEYEXCHANGE_MISSING )).getOwnKeypair ();
407
+ var keyPair = credentials .orElseThrow (() -> new IllegalStateException (KEYEXCHANGE_MISSING )).getOwnKeypair ();
413
408
414
409
// Send associate request
415
410
var nonce = sendEncryptedMessage (Map .of (
@@ -432,12 +427,8 @@ public void associate() throws IOException, KeepassProxyAccessException {
432
427
LOG .error (e .toString (), e .getCause ());
433
428
}
434
429
assert response != null ;
435
- Optional .ofNullable (credentials ).orElseThrow (
436
- () -> new IllegalStateException (MISSING_CLASS )).setAssociateId (response .getString ("id" ));
437
-
438
- Optional .ofNullable (credentials ).orElseThrow (
439
- () -> new IllegalStateException (MISSING_CLASS )).setIdKeyPublicKey (idKeyPair .getPublicKey ());
440
-
430
+ credentials .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setAssociateId (response .getString ("id" ));
431
+ credentials .orElseThrow (() -> new IllegalStateException (MISSING_CLASS )).setIdKeyPublicKey (idKeyPair .getPublicKey ());
441
432
support .firePropertyChange ("associated" , null , credentials );
442
433
};
443
434
scheduler .schedule (lookupResponse , RESPONSE_DELAY_MS , TimeUnit .MILLISECONDS );
@@ -451,36 +442,32 @@ public void associate() throws IOException, KeepassProxyAccessException {
451
442
* @throws IOException Retrieving the hash failed due to technical reasons.
452
443
* @throws KeepassProxyAccessException It was impossible to get the hash.
453
444
*/
454
- public Optional < String > getDatabasehash () throws IOException , KeepassProxyAccessException {
445
+ public String getDatabasehash () throws IOException , KeepassProxyAccessException {
455
446
// Send get-databasehash request
456
447
var nonce = sendEncryptedMessage (Map .of ("action" , Message .GET_DATABASE_HASH .action ));
457
448
var response = getEncryptedResponseAndDecrypt (Message .GET_DATABASE_HASH .action , nonce );
458
449
459
- if (response .has ("hash" ) && !response .getString ("hash" ).isBlank ()) {
460
- return Optional .of (response .getString ("hash" ));
461
- }
462
-
463
- return Optional .empty ();
450
+ return response .getString ("hash" );
464
451
}
465
452
466
453
/**
467
454
* Request for receiving the database hash (SHA256) of the current active KeePassXC database.
468
455
* Sent together with a request to unlock the KeePassXC database.
469
456
*
470
457
* @param triggerUnlock When true, the KeePassXC application is brought to the front and unlock is requested from the user.
471
- * @return The database hash of the current active KeePassXC database.
458
+ * @return The database hash of the current active KeePassXC database. Empty if the database is locked.
472
459
* @throws IOException Retrieving the hash failed due to technical reasons.
473
460
* @throws KeepassProxyAccessException It was impossible to get the hash.
474
461
*/
475
- public Optional < String > getDatabasehash (boolean triggerUnlock ) throws IOException , KeepassProxyAccessException {
462
+ public String getDatabasehash (boolean triggerUnlock ) throws IOException , KeepassProxyAccessException {
476
463
// Send get-databasehash request with triggerUnlock, if needed
477
464
var map = new HashMap <String , Object >(); // Map.of can't be used here, because we need a mutable object
478
465
map .put ("action" , Message .GET_DATABASE_HASH .action );
479
466
map .put ("triggerUnlock" , Boolean .toString (triggerUnlock ));
480
467
var nonce = sendEncryptedMessage (map );
481
468
var response = getEncryptedResponseAndDecrypt (Message .GET_DATABASE_HASH .action , nonce );
482
469
483
- return Optional . ofNullable ( response .getString ("hash" ) );
470
+ return response .getString ("hash" );
484
471
}
485
472
486
473
/**
@@ -845,15 +832,15 @@ private JSONArray checkKeysList(List<Map<String, String>> list) throws KeepassPr
845
832
}
846
833
847
834
// Getters and Setters
848
- public Optional < String > getIdKeyPairPublicKey () {
849
- return Optional . ofNullable ( credentials ) .map (value -> b64encode (value .getIdKeyPublicKey ()));
835
+ public String getIdKeyPairPublicKey () {
836
+ return credentials .map (value -> b64encode (value .getIdKeyPublicKey ())). orElse ( "" );
850
837
}
851
838
852
- public Optional < String > getAssociateId () {
853
- return Optional . ofNullable ( this . credentials ). flatMap (Credentials ::getAssociateId );
839
+ public String getAssociateId () {
840
+ return credentials . map (Credentials ::getAssociateId ). orElse ( "" );
854
841
}
855
842
856
- public void setCredentials (Credentials credentials ) {
843
+ public void setCredentials (Optional < Credentials > credentials ) {
857
844
this .credentials = credentials ;
858
845
}
859
846
0 commit comments