Merge branch 'feature/422-save-pw-linux' into develop

fixes #422
This commit is contained in:
Sebastian Stenzel
2019-07-02 16:12:45 +02:00
8 changed files with 149 additions and 7 deletions

View File

@@ -62,6 +62,7 @@
<configuration>
<outputDirectory>${project.build.directory}/libs</outputDirectory>
<excludeClassifiers>linux,mac,win</excludeClassifiers>
<excludeArtifactIds>dbus-java,secret-service,hkdf,java-utils</excludeArtifactIds>
</configuration>
</execution>
<execution>
@@ -76,6 +77,17 @@
<classifier>linux</classifier>
</configuration>
</execution>
<execution>
<id>copy-linux-secret-service</id>
<phase>prepare-package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/linux-libs</outputDirectory>
<includeArtifactIds>dbus-java,secret-service,hkdf,java-utils</includeArtifactIds>
</configuration>
</execution>
<execution>
<id>copy-mac-libs</id>
<phase>prepare-package</phase>

View File

@@ -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>

View File

@@ -0,0 +1,9 @@
package org.cryptomator.keychain;
public interface GnomeKeyringAccess {
public void storePassword(String key, CharSequence passphrase);
public char[] loadPassword(String key);
public void deletePassword(String key);
}

View File

@@ -0,0 +1,52 @@
package org.cryptomator.keychain;
import org.freedesktop.secret.simple.SimpleCollection;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GnomeKeyringAccessImpl implements GnomeKeyringAccess {
private static final Logger LOG = LoggerFactory.getLogger(GnomeKeyringAccessImpl.class);
private SimpleCollection keyring;
public GnomeKeyringAccessImpl() {
try {
keyring = new SimpleCollection();
} catch (IOException e) {
LOG.error("D-Bus reports a problem.", e);
}
}
public void storePassword(String key, CharSequence passphrase) {
List<String> list = keyring.getItems(createAttributes(key));
if (list == null) {
keyring.createItem("Cryptomator", passphrase, createAttributes(key));
}
}
public char[] loadPassword(String key) {
List<String> list = keyring.getItems(createAttributes(key));
if (list != null) {
return keyring.getSecret(list.get(0));
} else {
return null;
}
}
public void deletePassword(String key) {
List<String> list = keyring.getItems(createAttributes(key));
if (list != null) {
keyring.deleteItem(list.get(0));
}
}
private Map<String, String> createAttributes(String key) {
Map<String, String> attributes = new HashMap();
attributes.put("Vault", key);
return attributes;
}
}

View File

@@ -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

View File

@@ -0,0 +1,25 @@
package org.cryptomator.keychain;
import java.util.Optional;
public class LinuxKeychainTester {
public static boolean secretServiceIsAvailable() {
try {
Class.forName("org.freedesktop.secret.simple.SimpleCollection");
return true;
} catch (ClassNotFoundException e) {
return false;
}
}
public static Optional<GnomeKeyringAccess> getSecretService() {
if (!secretServiceIsAvailable()) return Optional.empty();
try {
Class clazz = Class.forName("org.cryptomator.keychain.GnomeKeyringAccessImpl");
GnomeKeyringAccess keyring = (GnomeKeyringAccess) clazz.getDeclaredConstructor().newInstance();
return Optional.of(keyring);
} catch (Exception e) {
return Optional.empty();
}
}
}

View File

@@ -0,0 +1,39 @@
package org.cryptomator.keychain;
import com.google.common.base.Preconditions;
import org.apache.commons.lang3.SystemUtils;
import javax.inject.Inject;
import java.util.Optional;
public class LinuxSecretServiceAccess implements KeychainAccessStrategy {
private final Optional<GnomeKeyringAccess> gnomeLoginKeyring;
@Inject
public LinuxSecretServiceAccess() {
gnomeLoginKeyring = LinuxKeychainTester.getSecretService();
}
@Override
public boolean isSupported() {
return SystemUtils.IS_OS_LINUX && LinuxKeychainTester.getSecretService().isPresent();
}
@Override
public void storePassphrase(String key, CharSequence passphrase) {
Preconditions.checkState(gnomeLoginKeyring.isPresent());
gnomeLoginKeyring.get().storePassword(key, passphrase);
}
@Override
public char[] loadPassphrase(String key) {
Preconditions.checkState(gnomeLoginKeyring.isPresent());
return gnomeLoginKeyring.get().loadPassword(key);
}
@Override
public void deletePassphrase(String key) {
Preconditions.checkState(gnomeLoginKeyring.isPresent());
gnomeLoginKeyring.get().deletePassword(key);
}
}

View File

@@ -7,13 +7,11 @@ package org.cryptomator.keychain;
import java.util.Set;
import com.google.common.collect.Sets;
public class TestKeychainModule extends KeychainModule {
@Override
Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain) {
return Sets.newHashSet(new MapKeychainAccess());
Set<KeychainAccessStrategy> provideKeychainAccessStrategies(MacSystemKeychainAccess macKeychain, WindowsProtectedKeychainAccess winKeychain, LinuxSecretServiceAccess linKeychain) {
return Set.of(new MapKeychainAccess());
}
}