clove/apps
email/app/src/main/java/gay/doughmination/mail/data/TokenStore.kt
back to email/app/src/main/java/gay/doughmination/mail/data
package gay.doughmination.mail.data
import android.content.Context
import android.security.keystore.KeyGenParameterSpec
import android.security.keystore.KeyProperties
import android.util.Base64
import java.security.KeyStore
import javax.crypto.Cipher
import javax.crypto.KeyGenerator
import javax.crypto.SecretKey
import javax.crypto.spec.GCMParameterSpec
/**
* Holds the session token, encrypted with a key that never leaves the
* Android Keystore, so a copy of the app's files alone doesn't sign anyone in.
* Also keeps the PKCE verifier of a sign-in in progress, which has to survive
* the app being killed while the browser tab is open.
*/
class TokenStore(context: Context) {
private val prefs = context.getSharedPreferences("auth", Context.MODE_PRIVATE)
@Volatile
private var cached: String? = null
var token: String?
get() = cached ?: prefs.getString(KEY_TOKEN, null)?.let(::decrypt)?.also { cached = it }
set(value) {
cached = value
prefs.edit().apply {
if (value == null) remove(KEY_TOKEN) else putString(KEY_TOKEN, encrypt(value))
}.apply()
}
var pendingVerifier: String?
get() = prefs.getString(KEY_VERIFIER, null)
set(value) = prefs.edit().apply {
if (value == null) remove(KEY_VERIFIER) else putString(KEY_VERIFIER, value)
}.apply()
private fun key(): SecretKey {
val store = KeyStore.getInstance("AndroidKeyStore").apply { load(null) }
(store.getKey(ALIAS, null) as SecretKey?)?.let { return it }
val generator = KeyGenerator.getInstance(KeyProperties.KEY_ALGORITHM_AES, "AndroidKeyStore")
generator.init(
KeyGenParameterSpec.Builder(ALIAS, KeyProperties.PURPOSE_ENCRYPT or KeyProperties.PURPOSE_DECRYPT)
.setBlockModes(KeyProperties.BLOCK_MODE_GCM)
.setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_NONE)
.build(),
)
return generator.generateKey()
}
private fun encrypt(plain: String): String {
val cipher = Cipher.getInstance(TRANSFORM).apply { init(Cipher.ENCRYPT_MODE, key()) }
val sealed = cipher.iv + cipher.doFinal(plain.toByteArray())
return Base64.encodeToString(sealed, Base64.NO_WRAP)
}
/** Null when it can't be read (the key was wiped, say); that just means signed out. */
private fun decrypt(stored: String): String? = runCatching {
val sealed = Base64.decode(stored, Base64.NO_WRAP)
val cipher = Cipher.getInstance(TRANSFORM)
cipher.init(Cipher.DECRYPT_MODE, key(), GCMParameterSpec(128, sealed, 0, IV_BYTES))
String(cipher.doFinal(sealed, IV_BYTES, sealed.size - IV_BYTES))
}.getOrNull()
private companion object {
const val ALIAS = "inbox-session"
const val TRANSFORM = "AES/GCM/NoPadding"
const val IV_BYTES = 12
const val KEY_TOKEN = "token"
const val KEY_VERIFIER = "verifier"
}
}