
org.cryptomator.windows.keychain.FileKeychain Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of integrations-win Show documentation
Show all versions of integrations-win Show documentation
Provides optional Windows services used by Cryptomator
The newest version!
package org.cryptomator.windows.keychain;
import com.fasterxml.jackson.core.JacksonException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.cryptomator.integrations.keychain.KeychainAccessException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.NoSuchFileException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Predicate;
import static java.nio.charset.StandardCharsets.UTF_8;
/**
* A file-based keychain. It's content is a utf-8 encoded JSON object.
*/
class FileKeychain implements WindowsKeychainAccessBase.Keychain {
private final static Logger LOG = LoggerFactory.getLogger(FileKeychain.class);
private static final ObjectMapper JSON_MAPPER = new ObjectMapper();
private final List keychainPaths;
private Map cache;
private volatile boolean loaded;
FileKeychain(String keychainPathsProperty) {
keychainPaths = parsePaths(System.getProperty(keychainPathsProperty, ""), System.getProperty("path.separator"));
cache = new ConcurrentHashMap<>();
}
//testing
FileKeychain(List paths) {
keychainPaths = paths;
cache = new ConcurrentHashMap<>();
}
synchronized void load() throws KeychainAccessException {
if (!loaded) {
loadInternal();
loaded = true;
}
}
//for testing
void loadInternal() throws KeychainAccessException {
if (keychainPaths.isEmpty()) {
throw new KeychainAccessException("No path specified to store keychain");
}
//Note: We are trying out all keychainPaths to see, if we have to migrate an old keychain file to a new location
boolean useExisting = false;
for (Path keychainPath : keychainPaths) {
Optional
© 2015 - 2025 Weber Informatics LLC | Privacy Policy