Skip to content

Commit

Permalink
Merge branch 'develop' into keychain-isLocked
Browse files Browse the repository at this point in the history
  • Loading branch information
Armin Schrenk authored Feb 23, 2021
2 parents e186400 + dfd81a0 commit 62a94fe
Showing 1 changed file with 12 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.freedesktop.secret.simple.SimpleCollection;

import java.io.IOException;
import java.security.AccessControlException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -15,12 +16,7 @@ public class SecretServiceKeychainAccess implements KeychainAccessProvider {

@Override
public boolean isSupported() {
try (@SuppressWarnings("unused") SimpleCollection keyring = new SimpleCollection()) {
// seems like we're able to access the keyring.
return true;
} catch (IOException | ExceptionInInitializerError | RuntimeException e) {
return false;
}
return SimpleCollection.isAvailable();
}

@Override
Expand All @@ -37,12 +33,12 @@ public boolean isLocked() {
public void storePassphrase(String key, CharSequence passphrase) throws KeychainAccessException {
try (SimpleCollection keyring = new SimpleCollection()) {
List<String> list = keyring.getItems(createAttributes(key));
if (list == null) {
if (list == null || list.isEmpty()) {
keyring.createItem(LABEL_FOR_SECRET_IN_KEYRING, passphrase, createAttributes(key));
} else {
changePassphrase(key, passphrase);
}
} catch (IOException e) {
} catch (IOException | AccessControlException e) {
throw new KeychainAccessException("Storing password failed.", e);
}
}
Expand All @@ -51,12 +47,12 @@ public void storePassphrase(String key, CharSequence passphrase) throws Keychain
public char[] loadPassphrase(String key) throws KeychainAccessException {
try (SimpleCollection keyring = new SimpleCollection()) {
List<String> list = keyring.getItems(createAttributes(key));
if (list != null) {
if (list != null && !list.isEmpty()) {
return keyring.getSecret(list.get(0));
} else {
return null;
}
} catch (IOException e) {
} catch (IOException | AccessControlException e) {
throw new KeychainAccessException("Loading password failed.", e);
}
}
Expand All @@ -65,10 +61,10 @@ public char[] loadPassphrase(String key) throws KeychainAccessException {
public void deletePassphrase(String key) throws KeychainAccessException {
try (SimpleCollection keyring = new SimpleCollection()) {
List<String> list = keyring.getItems(createAttributes(key));
if (list != null) {
if (list != null && !list.isEmpty()) {
keyring.deleteItem(list.get(0));
}
} catch (IOException e) {
} catch (IOException | AccessControlException e) {
throw new KeychainAccessException("Deleting password failed.", e);
}
}
Expand All @@ -77,17 +73,16 @@ public void deletePassphrase(String key) throws KeychainAccessException {
public void changePassphrase(String key, CharSequence passphrase) throws KeychainAccessException {
try (SimpleCollection keyring = new SimpleCollection()) {
List<String> list = keyring.getItems(createAttributes(key));
if (list != null) {
if (list != null && !list.isEmpty()) {
keyring.updateItem(list.get(0), LABEL_FOR_SECRET_IN_KEYRING, passphrase, createAttributes(key));
}
} catch (IOException e) {
} catch (IOException | AccessControlException e) {
throw new KeychainAccessException("Changing password failed.", e);
}
}

private Map<String, String> createAttributes(String key) {
Map<String, String> attributes = new HashMap();
attributes.put("Vault", key);
return attributes;
return Map.of("Vault", key);
}

}

0 comments on commit 62a94fe

Please sign in to comment.