5
5
import org .freedesktop .secret .simple .SimpleCollection ;
6
6
7
7
import java .io .IOException ;
8
+ import java .security .AccessControlException ;
8
9
import java .util .HashMap ;
9
10
import java .util .List ;
10
11
import java .util .Map ;
@@ -15,24 +16,19 @@ public class SecretServiceKeychainAccess implements KeychainAccessProvider {
15
16
16
17
@ Override
17
18
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 ();
24
20
}
25
21
26
22
@ Override
27
23
public void storePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
28
24
try (SimpleCollection keyring = new SimpleCollection ()) {
29
25
List <String > list = keyring .getItems (createAttributes (key ));
30
- if (list == null ) {
26
+ if (list == null || list . isEmpty () ) {
31
27
keyring .createItem (LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
32
28
} else {
33
29
changePassphrase (key , passphrase );
34
30
}
35
- } catch (IOException e ) {
31
+ } catch (IOException | AccessControlException e ) {
36
32
throw new KeychainAccessException ("Storing password failed." , e );
37
33
}
38
34
}
@@ -41,12 +37,12 @@ public void storePassphrase(String key, CharSequence passphrase) throws Keychain
41
37
public char [] loadPassphrase (String key ) throws KeychainAccessException {
42
38
try (SimpleCollection keyring = new SimpleCollection ()) {
43
39
List <String > list = keyring .getItems (createAttributes (key ));
44
- if (list != null ) {
40
+ if (list != null && ! list . isEmpty () ) {
45
41
return keyring .getSecret (list .get (0 ));
46
42
} else {
47
43
return null ;
48
44
}
49
- } catch (IOException e ) {
45
+ } catch (IOException | AccessControlException e ) {
50
46
throw new KeychainAccessException ("Loading password failed." , e );
51
47
}
52
48
}
@@ -55,10 +51,10 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
55
51
public void deletePassphrase (String key ) throws KeychainAccessException {
56
52
try (SimpleCollection keyring = new SimpleCollection ()) {
57
53
List <String > list = keyring .getItems (createAttributes (key ));
58
- if (list != null ) {
54
+ if (list != null && ! list . isEmpty () ) {
59
55
keyring .deleteItem (list .get (0 ));
60
56
}
61
- } catch (IOException e ) {
57
+ } catch (IOException | AccessControlException e ) {
62
58
throw new KeychainAccessException ("Deleting password failed." , e );
63
59
}
64
60
}
@@ -67,15 +63,16 @@ public void deletePassphrase(String key) throws KeychainAccessException {
67
63
public void changePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
68
64
try (SimpleCollection keyring = new SimpleCollection ()) {
69
65
List <String > list = keyring .getItems (createAttributes (key ));
70
- if (list != null ) {
66
+ if (list != null && ! list . isEmpty () ) {
71
67
keyring .updateItem (list .get (0 ), LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
72
68
}
73
- } catch (IOException e ) {
69
+ } catch (IOException | AccessControlException e ) {
74
70
throw new KeychainAccessException ("Changing password failed." , e );
75
71
}
76
72
}
77
73
78
74
private Map <String , String > createAttributes (String key ) {
79
75
return Map .of ("Vault" , key );
80
76
}
77
+
81
78
}
0 commit comments