15
15
import java .io .IOException ;
16
16
import java .io .ObjectInputStream ;
17
17
import java .io .ObjectOutputStream ;
18
+ import java .lang .module .ModuleDescriptor .Version ;
18
19
import java .nio .file .Files ;
19
20
import java .nio .file .Path ;
20
21
import java .nio .file .StandardCopyOption ;
@@ -36,6 +37,8 @@ public class KeepassProxyAccess implements PropertyChangeListener {
36
37
private final AtomicReference <ScheduledFuture <?>> scheduledSaveCmd = new AtomicReference <>();
37
38
private final ScheduledExecutorService scheduler ;
38
39
40
+ private final String V2_7_0 = "2.7.0" ;
41
+
39
42
public KeepassProxyAccess () {
40
43
if (SystemUtils .IS_OS_LINUX || SystemUtils .IS_OS_MAC_OSX ) {
41
44
connection = new LinuxMacConnection ();
@@ -322,8 +325,12 @@ public JSONObject getDatabaseGroups() {
322
325
*/
323
326
public String generatePassword () {
324
327
try {
325
- var response = connection .generatePassword ().getJSONArray ("entries" );
326
- return response .getJSONObject (0 ).getString ("password" );
328
+ var response = connection .generatePassword ();
329
+ if (isMinimiumVersion (response .getString ("version" ), V2_7_0 )) {
330
+ return response .getString ("password" );
331
+ } else {
332
+ return response .getJSONArray ("entries" ).getJSONObject (0 ).getString ("password" );
333
+ }
327
334
} catch (IOException | IllegalStateException | KeepassProxyAccessException | JSONException e ) {
328
335
log .info (e .toString (), e .getCause ());
329
336
return "" ;
@@ -458,6 +465,17 @@ private void traverse(List<Object> children, Map<String, String> groups) {
458
465
});
459
466
}
460
467
468
+ /**
469
+ * Compare two software version strings to check for a minimum software version.
470
+ *
471
+ * @param v1 The first version string.
472
+ * @param v2 The second version string.
473
+ * @return True in case v1 >= v2, false otherwise.
474
+ */
475
+ private boolean isMinimiumVersion (String v1 , String v2 ) {
476
+ return Version .parse (v1 ).compareTo (Version .parse (v2 )) >= 0 ;
477
+ }
478
+
461
479
@ Override
462
480
public void propertyChange (PropertyChangeEvent event ) {
463
481
scheduleSave ((Optional <Credentials >) event .getNewValue ());
0 commit comments