com.baulsupp.oksocial.credentials.PreferencesCredentialsStore.kt Maven / Gradle / Ivy
The newest version!
package com.baulsupp.oksocial.credentials
import java.util.prefs.Preferences
class PreferencesCredentialsStore : CredentialsStore {
private val userNode = Preferences.userNodeForPackage(this.javaClass)
override fun get(serviceDefinition: ServiceDefinition, tokenSet: String): T? {
val credentialsString = userNode.get(tokenKey(serviceDefinition.apiHost(), tokenSet), null)
return credentialsString?.let { serviceDefinition.parseCredentialsString(it) }
}
private fun tokenKey(name: String, tokenSet: String): String {
return "$name.token.$tokenSet"
}
override fun set(
serviceDefinition: ServiceDefinition,
tokenSet: String,
credentials: T
) {
val credentialsString = serviceDefinition.formatCredentialsString(credentials)
userNode.put(tokenKey(serviceDefinition.apiHost(), tokenSet), credentialsString)
}
override fun remove(serviceDefinition: ServiceDefinition, tokenSet: String) {
userNode.remove(tokenKey(serviceDefinition.apiHost(), tokenSet))
}
}