From 6e64cdb96e3351c94fbaca2be2c4d94331993bdc Mon Sep 17 00:00:00 2001 From: crschnick Date: Tue, 27 Feb 2024 05:23:46 +0000 Subject: [PATCH] Rework git askpass cache --- .../java/io/xpipe/app/util/SecretQuery.java | 4 ++++ .../io/xpipe/app/util/SecretQueryProgress.java | 17 +++++++++++++---- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/io/xpipe/app/util/SecretQuery.java b/app/src/main/java/io/xpipe/app/util/SecretQuery.java index 038a5abf7..3d30dfe3b 100644 --- a/app/src/main/java/io/xpipe/app/util/SecretQuery.java +++ b/app/src/main/java/io/xpipe/app/util/SecretQuery.java @@ -74,4 +74,8 @@ public interface SecretQuery { boolean cache(); boolean retryOnFail(); + + default boolean respectDontCacheSetting() { + return true; + } } diff --git a/app/src/main/java/io/xpipe/app/util/SecretQueryProgress.java b/app/src/main/java/io/xpipe/app/util/SecretQueryProgress.java index 2dd2025e4..d7d140d0e 100644 --- a/app/src/main/java/io/xpipe/app/util/SecretQueryProgress.java +++ b/app/src/main/java/io/xpipe/app/util/SecretQueryProgress.java @@ -55,6 +55,8 @@ public class SecretQueryProgress { } var firstSeenIndex = seenPrompts.indexOf(prompt); + var ref = new SecretReference(storeId, firstSeenIndex); + if (firstSeenIndex >= suppliers.size()) { countDown.pause(); var r = fallback.query(prompt); @@ -63,14 +65,14 @@ public class SecretQueryProgress { requestCancelled = true; return null; } + if (shouldCache(fallback, prompt)) { + SecretManager.set(ref, r.getSecret()); + } return r.getSecret(); } - var ref = new SecretReference(storeId, firstSeenIndex); var sup = suppliers.get(firstSeenIndex); - var shouldCache = sup.cache() - && SecretManager.shouldCacheForPrompt(prompt) - && !AppPrefs.get().dontCachePasswords().get(); + var shouldCache = shouldCache(sup, prompt); var wasLastPrompt = firstSeenIndex == seenPrompts.size() - 1; // Clear cache if secret was wrong/queried again @@ -114,4 +116,11 @@ public class SecretQueryProgress { } return r.getSecret(); } + + private boolean shouldCache(SecretQuery query, String prompt) { + var shouldCache = query.cache() + && SecretManager.shouldCacheForPrompt(prompt) + && (!query.respectDontCacheSetting() || !AppPrefs.get().dontCachePasswords().get()); + return shouldCache; + } }