Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / whatsapp / WhatsAppAuthData.kt
WhatsAppAuthData.kt116 lines · 5.4 KiBRaw
| 1 | package com.vayunmathur.communicate.data.whatsapp |
| 2 | |
| 3 | import android.content.Context |
| 4 | import androidx.core.content.edit |
| 5 | import com.vayunmathur.communicate.data.KeystoreSecretSealer |
| 6 | import com.vayunmathur.communicate.data.SecretSealer |
| 7 | import kotlinx.serialization.Serializable |
| 8 | import kotlinx.serialization.encodeToString |
| 9 | import kotlinx.serialization.json.Json as KotlinJson |
| 10 | |
| 11 | /** |
| 12 | * Persistent auth data for the WhatsApp **primary client** (own phone-number registration). |
| 13 | * |
| 14 | * Pruned vs. the messages companion port: dropped `advSecretKey`, `accountSignedDeviceIdentity` |
| 15 | * and `lid` (QR/linked-device only). Extended with the `/v2/register` outputs (`new_jid`, |
| 16 | * `server_time`, `login` token) needed to bring up the primary Noise session. |
| 17 | * |
| 18 | * Persisted under the `communicate_whatsapp_auth` namespace so it never collides with the |
| 19 | * companion's `whatsapp_auth` in the messages module. |
| 20 | */ |
| 21 | @Serializable |
| 22 | data class WhatsAppAuthData( |
| 23 | val phoneNumber: String, |
| 24 | val pushName: String, |
| 25 | // Primary JID assigned by /v2/register (`new_jid`), e.g. "[email protected]". |
| 26 | val wid: String, |
| 27 | // Noise key pair (X25519) — used in every handshake. |
| 28 | val noisePrivateKey: String, // Base64 |
| 29 | val noisePublicKey: String, // Base64 |
| 30 | // Signal identity key pair (Curve25519) — used for E2E encryption. |
| 31 | val identityPrivateKey: String, // Base64 |
| 32 | val identityPublicKey: String, // Base64 |
| 33 | // Signal Protocol registration ID. |
| 34 | val registrationId: Int, |
| 35 | // Signal signed pre-key. |
| 36 | val signedPreKeyId: Int, |
| 37 | val signedPreKeyPublic: String, // Base64 |
| 38 | val signedPreKeyPrivate: String, // Base64 |
| 39 | val signedPreKeySignature: String, // Base64 |
| 40 | // Primary device id is always 0 (the phone itself, not a companion). |
| 41 | val deviceId: Int = 0, |
| 42 | // ---- /v2/register outputs ---- |
| 43 | // Server-side login token (`login`) returned by register/security; replayed on subsequent calls. |
| 44 | val loginToken: String = "", |
| 45 | // Server clock at registration (`server_time`), seconds. |
| 46 | val serverTime: Long = 0L, |
| 47 | // Whether /v2/register returned ok (primary line is live). |
| 48 | val registered: Boolean = false, |
| 49 | // Timezone reported to the server on login. |
| 50 | val timezone: String = "", |
| 51 | val loggedInAt: Long = 0L, |
| 52 | val platformType: String = "ANDROID", |
| 53 | // Vestigial for the primary client (populated only by companion/QR pairing, which we don't use). |
| 54 | // Kept so the ported message engine compiles; both remain empty for a primary line. |
| 55 | val lid: String = "", |
| 56 | val accountSignedDeviceIdentity: String = "", |
| 57 | // ---- Post-quantum (Kyber1024 / ML-KEM) last-resort prekey (e_pq_* bundle, Phase B 2d) ---- |
| 58 | // All-or-none: emitted together in RegistrationKeys.bundleFields when the id is non-zero. |
| 59 | val pqLastResortKeyId: Int = 0, |
| 60 | val pqLastResortPublic: String = "", // Base64 (raw 1568B Kyber1024 pub) |
| 61 | val pqLastResortSecret: String = "", // Base64 (serialized KEM secret key) |
| 62 | val pqLastResortSignature: String = "", // Base64 (64B XEdDSA sig over 0x08||pub) |
| 63 | // Profile name sent to /v2/* for new clients (profile_name, w2.md §2.2). |
| 64 | val profileName: String = "", |
| 65 | ) { |
| 66 | /** Preference keys and factories for [WhatsAppAuthData]. */ |
| 67 | companion object { |
| 68 | // Distinct from the companion's "whatsapp_auth" in the messages module. |
| 69 | private const val PREFS_NAME = "communicate_whatsapp_auth" |
| 70 | private const val KEY_AUTH_DATA = "auth_data" |
| 71 | |
| 72 | /** |
| 73 | * Load the sealed (or legacy plaintext) auth JSON. |
| 74 | * |
| 75 | * Sealed: unseal then parse; unseal/parse failure returns null |
| 76 | * (signed-out) and leaves the blob intact. Legacy plaintext: parse |
| 77 | * first and rewrite sealed ONLY after a successful parse |
| 78 | * (verify-before-overwrite — a botched rewrite would force |
| 79 | * re-registration). Idempotent: the second load sees sealed form. |
| 80 | */ |
| 81 | fun load(context: Context, sealer: SecretSealer = KeystoreSecretSealer.shared): WhatsAppAuthData? { |
| 82 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 83 | val raw = prefs.getString(KEY_AUTH_DATA, null) ?: return null |
| 84 | if (sealer.isSealed(raw)) { |
| 85 | val json = sealer.unseal(raw) ?: return null |
| 86 | return try { |
| 87 | KotlinJson.decodeFromString<WhatsAppAuthData>(json) |
| 88 | } catch (ignored: Exception) { |
| 89 | null |
| 90 | } |
| 91 | } |
| 92 | val parsed = try { |
| 93 | KotlinJson.decodeFromString<WhatsAppAuthData>(raw) |
| 94 | } catch (ignored: Exception) { |
| 95 | return null |
| 96 | } |
| 97 | sealer.seal(raw)?.let { sealed -> |
| 98 | prefs.edit { putString(KEY_AUTH_DATA, sealed) } |
| 99 | } |
| 100 | return parsed |
| 101 | } |
| 102 | |
| 103 | /** Save the whole auth JSON sealed; on seal failure nothing is written (never plaintext). */ |
| 104 | fun save(context: Context, authData: WhatsAppAuthData, sealer: SecretSealer = KeystoreSecretSealer.shared) { |
| 105 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 106 | val json = KotlinJson.encodeToString(authData) |
| 107 | val sealed = sealer.seal(json) ?: return |
| 108 | prefs.edit { putString(KEY_AUTH_DATA, sealed) } |
| 109 | } |
| 110 | |
| 111 | fun clear(context: Context) { |
| 112 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 113 | prefs.edit { remove(KEY_AUTH_DATA) } |
| 114 | } |
| 115 | } |
| 116 | } |