@@ -28,43 +28,47 @@ public KeePassXCAccess() {
2828 @ Override
2929 public boolean isLocked () { return proxy .getDatabasehash ().isEmpty (); }
3030
31- @ Override
32- public boolean needsAssociation () { return !proxy .connectionAvailable (); }
33-
34- @ Override
35- public boolean associate () { return proxy .associate (); }
31+ private void ensureAssociation () throws KeychainAccessException {
32+ if (!proxy .connectionAvailable ()) { // Proxy needs association
33+ if (!proxy .associate ()) {
34+ throw new KeychainAccessException ("Association with KeePassXC database failed" );
35+ }
36+ }
37+ }
3638
3739 public String unlock () { return proxy .getDatabasehash (true ); }
3840
3941 @ Override
4042 public void storePassphrase (String vault , CharSequence password ) throws KeychainAccessException {
4143 vault = URL_SCHEME + vault ;
4244 if (isLocked ()) {
43- throw new KeychainAccessException ("Storing of the passphrase failed " );
45+ throw new KeychainAccessException ("Failed to store password. KeePassXC database is locked. Needs to be unlocked first " );
4446 }
47+ ensureAssociation ();
4548 if (!proxy .loginExists (vault , null , false , List .of (proxy .exportConnection ()), password .toString ())
4649 && !proxy .setLogin (vault , null , null , "Vault" , password .toString (), "default" , "default" , "default" )) {
47- throw new KeychainAccessException ("Storing of the passphrase failed" );
50+ throw new KeychainAccessException ("Storing of the password failed" );
4851 }
4952 }
5053
5154 @ Override
5255 public char [] loadPassphrase (String vault ) throws KeychainAccessException {
5356 if (isLocked ()) {
54- throw new KeychainAccessException ("Loading of the passphrase failed " );
57+ throw new KeychainAccessException ("Failed to load password. KeePassXC database is locked. Needs to be unlocked first " );
5558 }
59+ ensureAssociation ();
5660 vault = URL_SCHEME + vault ;
57- Map < String , Object > answer = proxy .getLogins (vault , null , false , List .of (proxy .exportConnection ()));
61+ var answer = proxy .getLogins (vault , null , false , List .of (proxy .exportConnection ()));
5862 if (answer .isEmpty () || null == answer .get ("entries" )) {
59- throw new KeychainAccessException ("Loading of the passphrase failed" );
63+ throw new KeychainAccessException ("No password found for vault " + vault . substring ( URL_SCHEME . length ()) );
6064 }
61- List < Object > array = (ArrayList <Object >) answer .get ("entries" );
62- Map < String , Object > credentials = (HashMap <String , Object >) array .get (0 );
65+ var array = (ArrayList <Object >) answer .get ("entries" );
66+ var credentials = (HashMap <String , Object >) array .get (0 );
6367 if (credentials .get ("password" ) != null ) {
64- String password = (String ) credentials .get ("password" );
68+ var password = (String ) credentials .get ("password" );
6569 return password .toCharArray ();
6670 } else {
67- throw new KeychainAccessException ("Loading of the passphrase failed" );
71+ throw new KeychainAccessException ("Loading of the password failed" );
6872 }
6973 }
7074
0 commit comments