|
| 1 | +package me.hatter.integrations.keychain; |
| 2 | + |
| 3 | +import org.cryptomator.integrations.keychain.KeychainAccessException; |
| 4 | +import org.cryptomator.integrations.keychain.KeychainAccessProvider; |
| 5 | +import org.slf4j.Logger; |
| 6 | +import org.slf4j.LoggerFactory; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author hatterjiang |
| 10 | + */ |
| 11 | +public class GnuPGAccessProvider implements KeychainAccessProvider { |
| 12 | + |
| 13 | + private static final Logger LOG = LoggerFactory.getLogger(GnuPGAccessProvider.class); |
| 14 | + |
| 15 | + private GnuPGConfig gnuPGConfig; |
| 16 | + |
| 17 | + public GnuPGAccessProvider() { |
| 18 | + try { |
| 19 | + gnuPGConfig = Utils.loadGnuPGConfig(); |
| 20 | + if (!Utils.checkGnuPGReady(gnuPGConfig)) { |
| 21 | + LOG.error("Check GnuPG command failed"); |
| 22 | + gnuPGConfig = null; |
| 23 | + } |
| 24 | + } catch (KeychainAccessException e) { |
| 25 | + gnuPGConfig = null; |
| 26 | + LOG.error("Load GnuPG config failed", e); |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public String displayName() { |
| 32 | + return "GnuPG"; |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + public boolean isSupported() { |
| 37 | + return gnuPGConfig != null; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public boolean isLocked() { |
| 42 | + // No lock status |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public void storePassphrase(String vault, CharSequence password) throws KeychainAccessException { |
| 48 | + storePassphrase(vault, "Vault", password); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void storePassphrase(String vault, String name, CharSequence password) throws KeychainAccessException { |
| 53 | + Utils.storePassword(gnuPGConfig, vault, name, password); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public char[] loadPassphrase(String vault) throws KeychainAccessException { |
| 58 | + final String password = Utils.loadPassword(gnuPGConfig, vault); |
| 59 | + return password.toCharArray(); |
| 60 | + } |
| 61 | + |
| 62 | + @Override |
| 63 | + public void deletePassphrase(String vault) throws KeychainAccessException { |
| 64 | + if (isLocked()) { |
| 65 | + LOG.info("Failed to delete password. KeePassXC database is locked. Needs to be unlocked first."); |
| 66 | + return; |
| 67 | + } |
| 68 | + Utils.deletePassword(gnuPGConfig, vault); |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void changePassphrase(String vault, CharSequence password) throws KeychainAccessException { |
| 73 | + changePassphrase(vault, "Vault", password); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public void changePassphrase(String vault, String name, CharSequence password) throws KeychainAccessException { |
| 78 | + LOG.info("Change password for: " + vault); |
| 79 | + Utils.storePassword(gnuPGConfig, vault, name, password); |
| 80 | + } |
| 81 | +} |
0 commit comments