Persist credentials back into the PasswordSafe (#465)

This commit is contained in:
Simon Svensson 2024-04-17 11:04:40 +02:00 committed by GitHub
parent 2221d72430
commit 7d075f6905
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,14 +12,23 @@ object CredentialsStore {
CredentialKey.values().forEach {
val credentialAttributes = CredentialAttributes(generateServiceName("CodeGPT", it.name))
val password = PasswordSafe.instance.getPassword(credentialAttributes)
setCredential(it, password)
// Avoid calling setCredential here since it will persist
// the password back into the PasswordSafe unnecessarily.
credentialsMap[it] = password
}
}
fun getCredential(key: CredentialKey): String? = credentialsMap[key]
fun setCredential(key: CredentialKey, password: String?) {
val prevPassword = credentialsMap[key]
credentialsMap[key] = password
if (prevPassword != password) {
val credentialAttributes = CredentialAttributes(generateServiceName("CodeGPT", key.name))
PasswordSafe.instance.setPassword(credentialAttributes, password)
}
}
fun isCredentialSet(key: CredentialKey): Boolean = !getCredential(key).isNullOrEmpty()