0

I have this method

public String getCredentials(String entry) throws IOException {
    KeePassFile database = KeePassDatabase
            .getInstance(config.getProperty("keyPassDataBasePath"))
            .openDatabase(new File(config.getProperty("keyPassKeyPath")));
    Entry sampleEntry = database.getEntryByTitle(entry);
    return sampleEntry.getPassword();
}

Which is basically going to a KeePass DB, retrieves a password according to the title of its belonging account.

There are a LOT of methods that need 2 passwords, so 2 entries are used. I don't want to call that method everytime as I think it's a waste of resources. How can I save the returning value, and use it in other classes where the methods need those values?

Would this work? I feel like it's gonna make the method call several times anyways

    private static String pwd1;
    private static String pwd2;

    public void setValues() throws IOException {
        pwd1 = getCredentials("accountName1");
        pwd2 = getCredentials("accountName2");
    }

    public String getPwd1(){
        return pwd1;
    }

    public String getPwd2(){
        return pwd2;
    }
  • So you are just doing this for fun, right? This isn't for security on a company's production system? Because security is so so complicated these days, and coming to SO for advice on how to set up a secure system is not what you want to do. A better alternative is online security conferences – ControlAltDel Aug 12 '20 at 17:25
  • It's actually a clientId and clientSecret, I dont deal with passwords, other than my own, and this does not reach production for a company, its personal use – notPercival Aug 12 '20 at 22:00

1 Answers1

1

Store them in a HasMap with the key being the entry and the password being the value:

class CachedCredentials {
  private Map<String, String> storedPasswords = new HashMap<>();

  private Properties config;
  
  public CachedCredentials(Properties config) {
     this.config = config;
  }
  
  public String getCredentials(String entry) {
    if (!storedPasswords.containsKey(entry)) {
      KeePassFile database = KeePassDatabase
        .getInstance(config.getProperty("keyPassDataBasePath"))
        .openDatabase(new File(config.getProperty("keyPassKeyPath")));
  
      Entry sampleEntry = database.getEntryByTitle(entry);   
      storedPasswords.put(entry, sampleEntry.getPassword());
    }

    return storedPasswords.get(entry);
  }

then in your setValues method you can do:

private cachedCreds; //initialize this in your constructor

public void setValues() throws IOException {
    pwd1 = cachedCreds.getCredentials("accountName1");
    pwd2 = cachedCreds.getCredentials("accountName2");
}

This solution might provide to be insecure if someone is doing a memory snooping while the program is running. Might want to think of a way to obfuscate the cached password by either base64encoding it or actually encrypting it but that goes above what is being asked.

locus2k
  • 2,802
  • 1
  • 14
  • 21
  • Nice approach, and the encryption will be a nice addition too, might be an overkill for a clientId and clientSecret (sorry I said passwords), but you can never be too secure, thank you for your help – notPercival Aug 13 '20 at 13:25