Bläddra i källkod

Add access functionality for Gnome login keyring

Ralph Plawetzki 5 år sedan
förälder
incheckning
63383866ea

+ 8 - 1
main/keychain/pom.xml

@@ -39,7 +39,14 @@
 			<groupId>com.google.dagger</groupId>
 			<artifactId>dagger</artifactId>
 		</dependency>
-		
+
+		<!-- secret-service lib -->
+		<dependency>
+			<groupId>de.swiesend</groupId>
+			<artifactId>secret-service</artifactId>
+			<version>1.0.0-RC.3</version>
+		</dependency>
+
 		<!-- Logging -->
 		<dependency>
 			<groupId>org.slf4j</groupId>

+ 2 - 2
main/keychain/src/main/java/org/cryptomator/keychain/KeychainModule.java

@@ -31,8 +31,8 @@ public class KeychainModule {
 
 	@Provides
 	@ElementsIntoSet
-	Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain) {
-		return Sets.newHashSet(macKeychain, winKeychain);
+	Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain, LinuxSecretServiceAccess linKeychain) {
+		return Sets.newHashSet(macKeychain, winKeychain, linKeychain);
 	}
 
 	@Provides

+ 67 - 0
main/keychain/src/main/java/org/cryptomator/keychain/LinuxSecretServiceAccess.java

@@ -0,0 +1,67 @@
+package org.cryptomator.keychain;
+
+import com.google.common.base.Preconditions;
+import org.apache.commons.lang3.SystemUtils;
+import org.freedesktop.secret.simple.SimpleCollection;
+
+import javax.inject.Inject;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+
+public class LinuxSecretServiceAccess implements KeychainAccessStrategy {
+	private final Optional<SimpleCollection> gnomeLoginKeyring;
+
+	@Inject
+	public LinuxSecretServiceAccess() {
+		SimpleCollection keyring = null;
+		try {
+			keyring = new SimpleCollection();
+		} catch (Exception e) {
+			// Accessing secret-service DBus API failed
+		} finally {
+			gnomeLoginKeyring = Optional.ofNullable(keyring);
+		}
+	}
+
+	@Override
+	public boolean isSupported() {
+		return SystemUtils.IS_OS_LINUX && gnomeLoginKeyring.isPresent();
+	}
+
+	@Override
+	public void storePassphrase(String key, CharSequence passphrase) {
+		Preconditions.checkState(gnomeLoginKeyring.isPresent());
+		List<String> list = gnomeLoginKeyring.get().getItems(createAttributes(key));
+		if (list == null) {
+			gnomeLoginKeyring.get().createItem("Cryptomator", passphrase, createAttributes(key));
+		}
+	}
+
+	@Override
+	public char[] loadPassphrase(String key) {
+		Preconditions.checkState(gnomeLoginKeyring.isPresent());
+		List<String> list = gnomeLoginKeyring.get().getItems(createAttributes(key));
+		if (list != null) {
+			return gnomeLoginKeyring.get().getSecret(list.get(0));
+		} else {
+			return null;
+		}
+	}
+
+	@Override
+	public void deletePassphrase(String key) {
+		Preconditions.checkState(gnomeLoginKeyring.isPresent());
+		List<String> list = gnomeLoginKeyring.get().getItems(createAttributes(key));
+		if (list != null) {
+			gnomeLoginKeyring.get().deleteItem(list.get(0));
+		}
+	}
+
+	private Map<String, String> createAttributes(String key) {
+		Map<String, String> attributes = new HashMap();
+		attributes.put("Vault", key);
+		return attributes;
+	}
+}

+ 1 - 1
main/keychain/src/test/java/org/cryptomator/keychain/TestKeychainModule.java

@@ -12,7 +12,7 @@ import com.google.common.collect.Sets;
 public class TestKeychainModule extends KeychainModule {
 
 	@Override
-	Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain) {
+	Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain, LinuxSecretServiceAccess linKeychain) {
 		return Sets.newHashSet(new MapKeychainAccess());
 	}