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,12 +16,7 @@ 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
@@ -37,12 +33,12 @@ public boolean isLocked() {
3733 public void storePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
3834 try (SimpleCollection keyring = new SimpleCollection ()) {
3935 List <String > list = keyring .getItems (createAttributes (key ));
40- if (list == null ) {
36+ if (list == null || list . isEmpty () ) {
4137 keyring .createItem (LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
4238 } else {
4339 changePassphrase (key , passphrase );
4440 }
45- } catch (IOException e ) {
41+ } catch (IOException | AccessControlException e ) {
4642 throw new KeychainAccessException ("Storing password failed." , e );
4743 }
4844 }
@@ -51,12 +47,12 @@ public void storePassphrase(String key, CharSequence passphrase) throws Keychain
5147 public char [] loadPassphrase (String key ) throws KeychainAccessException {
5248 try (SimpleCollection keyring = new SimpleCollection ()) {
5349 List <String > list = keyring .getItems (createAttributes (key ));
54- if (list != null ) {
50+ if (list != null && ! list . isEmpty () ) {
5551 return keyring .getSecret (list .get (0 ));
5652 } else {
5753 return null ;
5854 }
59- } catch (IOException e ) {
55+ } catch (IOException | AccessControlException e ) {
6056 throw new KeychainAccessException ("Loading password failed." , e );
6157 }
6258 }
@@ -65,10 +61,10 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
6561 public void deletePassphrase (String key ) throws KeychainAccessException {
6662 try (SimpleCollection keyring = new SimpleCollection ()) {
6763 List <String > list = keyring .getItems (createAttributes (key ));
68- if (list != null ) {
64+ if (list != null && ! list . isEmpty () ) {
6965 keyring .deleteItem (list .get (0 ));
7066 }
71- } catch (IOException e ) {
67+ } catch (IOException | AccessControlException e ) {
7268 throw new KeychainAccessException ("Deleting password failed." , e );
7369 }
7470 }
@@ -77,17 +73,16 @@ public void deletePassphrase(String key) throws KeychainAccessException {
7773 public void changePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
7874 try (SimpleCollection keyring = new SimpleCollection ()) {
7975 List <String > list = keyring .getItems (createAttributes (key ));
80- if (list != null ) {
76+ if (list != null && ! list . isEmpty () ) {
8177 keyring .updateItem (list .get (0 ), LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
8278 }
83- } catch (IOException e ) {
79+ } catch (IOException | AccessControlException e ) {
8480 throw new KeychainAccessException ("Changing password failed." , e );
8581 }
8682 }
8783
8884 private Map <String , String > createAttributes (String key ) {
89- Map <String , String > attributes = new HashMap ();
90- attributes .put ("Vault" , key );
91- return attributes ;
85+ return Map .of ("Vault" , key );
9286 }
87+
9388}
0 commit comments