55import org .freedesktop .secret .simple .SimpleCollection ;
66
77import java .io .IOException ;
8+ import java .security .AccessControlException ;
89import java .util .HashMap ;
910import java .util .List ;
1011import java .util .Map ;
@@ -15,24 +16,19 @@ public class SecretServiceKeychainAccess implements KeychainAccessProvider {
1516
1617 @ Override
1718 public boolean isSupported () {
18- try (@ SuppressWarnings ("unused" ) SimpleCollection keyring = new SimpleCollection ()) {
19- // seems like we're able to access the keyring.
20- return true ;
21- } catch (IOException | ExceptionInInitializerError | RuntimeException e ) {
22- return false ;
23- }
19+ return SimpleCollection .isAvailable ();
2420 }
2521
2622 @ Override
2723 public void storePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
2824 try (SimpleCollection keyring = new SimpleCollection ()) {
2925 List <String > list = keyring .getItems (createAttributes (key ));
30- if (list == null ) {
26+ if (list == null || list . isEmpty () ) {
3127 keyring .createItem (LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
3228 } else {
3329 changePassphrase (key , passphrase );
3430 }
35- } catch (IOException e ) {
31+ } catch (IOException | AccessControlException e ) {
3632 throw new KeychainAccessException ("Storing password failed." , e );
3733 }
3834 }
@@ -41,12 +37,12 @@ public void storePassphrase(String key, CharSequence passphrase) throws Keychain
4137 public char [] loadPassphrase (String key ) throws KeychainAccessException {
4238 try (SimpleCollection keyring = new SimpleCollection ()) {
4339 List <String > list = keyring .getItems (createAttributes (key ));
44- if (list != null ) {
40+ if (list != null && ! list . isEmpty () ) {
4541 return keyring .getSecret (list .get (0 ));
4642 } else {
4743 return null ;
4844 }
49- } catch (IOException e ) {
45+ } catch (IOException | AccessControlException e ) {
5046 throw new KeychainAccessException ("Loading password failed." , e );
5147 }
5248 }
@@ -55,10 +51,10 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
5551 public void deletePassphrase (String key ) throws KeychainAccessException {
5652 try (SimpleCollection keyring = new SimpleCollection ()) {
5753 List <String > list = keyring .getItems (createAttributes (key ));
58- if (list != null ) {
54+ if (list != null && ! list . isEmpty () ) {
5955 keyring .deleteItem (list .get (0 ));
6056 }
61- } catch (IOException e ) {
57+ } catch (IOException | AccessControlException e ) {
6258 throw new KeychainAccessException ("Deleting password failed." , e );
6359 }
6460 }
@@ -67,15 +63,16 @@ public void deletePassphrase(String key) throws KeychainAccessException {
6763 public void changePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
6864 try (SimpleCollection keyring = new SimpleCollection ()) {
6965 List <String > list = keyring .getItems (createAttributes (key ));
70- if (list != null ) {
66+ if (list != null && ! list . isEmpty () ) {
7167 keyring .updateItem (list .get (0 ), LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
7268 }
73- } catch (IOException e ) {
69+ } catch (IOException | AccessControlException e ) {
7470 throw new KeychainAccessException ("Changing password failed." , e );
7571 }
7672 }
7773
7874 private Map <String , String > createAttributes (String key ) {
7975 return Map .of ("Vault" , key );
8076 }
77+
8178}
0 commit comments