1010import org .purejava .secret .api .EncryptedSession ;
1111import org .purejava .secret .api .Item ;
1212import org .purejava .secret .api .Static ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
1315
16+ import java .util .ArrayList ;
17+ import java .util .List ;
1418import java .util .Map ;
1519
1620@ Priority (900 )
1721@ OperatingSystem (OperatingSystem .Value .LINUX )
1822@ DisplayName ("Secret Service" )
1923public class SecretServiceKeychainAccess implements KeychainAccessProvider {
2024
25+ private static final Logger LOG = LoggerFactory .getLogger (SecretServiceKeychainAccess .class );
2126 private final EncryptedSession session = new EncryptedSession ();
2227 private final Collection collection = new Collection (new DBusPath (Static .DBusPath .DEFAULT_COLLECTION ));
2328
29+ public SecretServiceKeychainAccess () {
30+ session .getService ().addCollectionChangedHandler (collection -> LOG .debug ("Collection {} changed" , collection .getPath ()));
31+ session .getService ().addCollectionCreatedHandler (collection -> LOG .debug ("Collection {} created" , collection .getPath ()));
32+ session .getService ().addCollectionDeletedHandler (collection -> LOG .debug ("Collection {} deleted" , collection .getPath ()));
33+ collection .addItemChangedHandler (item -> LOG .debug ("Item {} changed" , item .getPath ()));
34+ collection .addItemCreatedHandler (item -> LOG .debug ("Item {} created" , item .getPath ()));
35+ collection .addItemDeletedHandler (item -> LOG .debug ("Item {} deleted" , item .getPath ()));
36+ }
37+
2438 @ Override
2539 public void storePassphrase (String key , String displayName , CharSequence passphrase ) throws KeychainAccessException {
2640 try {
2741 var call = collection .searchItems (createAttributes (key ));
2842 if (call .isSuccess ()) {
2943 if (call .value ().isEmpty ()) {
44+ List <DBusPath > lockable = new ArrayList <>();
45+ lockable .add (new DBusPath (collection .getDBusPath ()));
46+ session .getService ().unlock (lockable );
3047 var itemProps = Item .createProperties (displayName , createAttributes (key ));
3148 var secret = session .encrypt (passphrase );
3249 var created = collection .createItem (itemProps , secret , false );
@@ -51,6 +68,7 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
5168 if (call .isSuccess ()) {
5269 if (!call .value ().isEmpty ()) {
5370 var path = call .value ().getFirst ();
71+ session .getService ().ensureUnlocked (path );
5472 var secret = new Item (path ).getSecret (session .getSession ());
5573 return session .decrypt (secret );
5674 } else {
@@ -71,6 +89,7 @@ public void deletePassphrase(String key) throws KeychainAccessException {
7189 if (call .isSuccess ()) {
7290 if (!call .value ().isEmpty ()) {
7391 var path = call .value ().getFirst ();
92+ session .getService ().ensureUnlocked (path );
7493 var item = new Item (path );
7594 var deleted = item .delete ();
7695 if (!deleted .isSuccess ()) {
@@ -94,6 +113,7 @@ public void changePassphrase(String key, String displayName, CharSequence passph
94113 var call = collection .searchItems (createAttributes (key ));
95114 if (call .isSuccess ()) {
96115 if (!call .value ().isEmpty ()) {
116+ session .getService ().ensureUnlocked (call .value ().getFirst ());
97117 var secret = session .encrypt (passphrase );
98118 var itemProps = Item .createProperties (displayName , createAttributes (key ));
99119 var updated = collection .createItem (itemProps , secret , true );
0 commit comments