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,12 +16,7 @@ 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
@@ -37,12 +33,12 @@ public boolean isLocked() {
37
33
public void storePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
38
34
try (SimpleCollection keyring = new SimpleCollection ()) {
39
35
List <String > list = keyring .getItems (createAttributes (key ));
40
- if (list == null ) {
36
+ if (list == null || list . isEmpty () ) {
41
37
keyring .createItem (LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
42
38
} else {
43
39
changePassphrase (key , passphrase );
44
40
}
45
- } catch (IOException e ) {
41
+ } catch (IOException | AccessControlException e ) {
46
42
throw new KeychainAccessException ("Storing password failed." , e );
47
43
}
48
44
}
@@ -51,12 +47,12 @@ public void storePassphrase(String key, CharSequence passphrase) throws Keychain
51
47
public char [] loadPassphrase (String key ) throws KeychainAccessException {
52
48
try (SimpleCollection keyring = new SimpleCollection ()) {
53
49
List <String > list = keyring .getItems (createAttributes (key ));
54
- if (list != null ) {
50
+ if (list != null && ! list . isEmpty () ) {
55
51
return keyring .getSecret (list .get (0 ));
56
52
} else {
57
53
return null ;
58
54
}
59
- } catch (IOException e ) {
55
+ } catch (IOException | AccessControlException e ) {
60
56
throw new KeychainAccessException ("Loading password failed." , e );
61
57
}
62
58
}
@@ -65,10 +61,10 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
65
61
public void deletePassphrase (String key ) throws KeychainAccessException {
66
62
try (SimpleCollection keyring = new SimpleCollection ()) {
67
63
List <String > list = keyring .getItems (createAttributes (key ));
68
- if (list != null ) {
64
+ if (list != null && ! list . isEmpty () ) {
69
65
keyring .deleteItem (list .get (0 ));
70
66
}
71
- } catch (IOException e ) {
67
+ } catch (IOException | AccessControlException e ) {
72
68
throw new KeychainAccessException ("Deleting password failed." , e );
73
69
}
74
70
}
@@ -77,17 +73,16 @@ public void deletePassphrase(String key) throws KeychainAccessException {
77
73
public void changePassphrase (String key , CharSequence passphrase ) throws KeychainAccessException {
78
74
try (SimpleCollection keyring = new SimpleCollection ()) {
79
75
List <String > list = keyring .getItems (createAttributes (key ));
80
- if (list != null ) {
76
+ if (list != null && ! list . isEmpty () ) {
81
77
keyring .updateItem (list .get (0 ), LABEL_FOR_SECRET_IN_KEYRING , passphrase , createAttributes (key ));
82
78
}
83
- } catch (IOException e ) {
79
+ } catch (IOException | AccessControlException e ) {
84
80
throw new KeychainAccessException ("Changing password failed." , e );
85
81
}
86
82
}
87
83
88
84
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 );
92
86
}
87
+
93
88
}
0 commit comments