Keep the typed address, know an instance signed in to before, heed min_clients
CI / check (push) Canceled after 0s
CI / check (push) Canceled after 0s
- A new sign-in stores the normalised typed address; the instance's own base_url is not stored and is looked at only to recognise an instance already on the phone (same account, stored base equal to the typed address or to what the instance says): that entry is refreshed under its id, no twin is added. Existing entries are not migrated: ids are opaque keys. - The device-code endpoints of the discovery document are used only on the typed address's origin; otherwise the sign-in says so. - min_clients.android in the discovery document, above this app's version (by numbers, beta-aware, a build's suffix left off), is "update the app" with both versions, at sign-in and at the drawer's check. Any other shape of the field is read as nothing said. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
19 files changed
+339
-25
No files matched your search
@@ -49,3 +49,23 @@ fun address(typed: String): Address {
|
||||
else -> Address.NotHttp
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether two stored or said bases name one instance: compared as [address] reads them, so that
|
||||
* a capital, a trailing slash or the scheme's own port makes no difference. One that the rule
|
||||
* does not read (an `http://` base a server once said of itself) is compared as it stands.
|
||||
*/
|
||||
fun sameBase(one: String, other: String): Boolean = canonical(one) == canonical(other)
|
||||
|
||||
private fun canonical(base: String): String = (address(base) as? Address.Base)?.url ?: base.trim().trimEnd('/').lowercase()
|
||||
|
||||
/**
|
||||
* Whether [url] is on the origin of [base]: the same scheme, host and port. An endpoint a
|
||||
* discovery document names is used only then -- the token and the sign-in go to the address the
|
||||
* person typed and to no other host, whatever the document says.
|
||||
*/
|
||||
fun onOrigin(base: String, url: String): Boolean {
|
||||
val home = base.toHttpUrlOrNull() ?: return false
|
||||
val there = url.toHttpUrlOrNull() ?: return false
|
||||
return home.scheme == there.scheme && home.host == there.host && home.port == there.port
|
||||
}
|
||||
@@ -1,5 +1,8 @@
|
||||
package eu.llembas.android.api
|
||||
|
||||
import kotlinx.serialization.json.JsonObject
|
||||
import kotlinx.serialization.json.JsonPrimitive
|
||||
|
||||
/**
|
||||
* The app API this app reads: 2, the server's 2.0 (no canvas, no `knowledge_bases`, the `show`
|
||||
* card and its route, and the person's files). A server with a newer one is told about, not
|
||||
@@ -29,3 +32,38 @@ fun fit(found: Discovery): ServerFit {
|
||||
else -> ServerFit.FITS
|
||||
}
|
||||
}
|
||||
|
||||
/** This app's name among the products a discovery document's `min_clients` lists. */
|
||||
const val PRODUCT = "android"
|
||||
|
||||
/** The oldest version of [product] the instance says it works with, or null: nothing said, or not in words this app reads. */
|
||||
fun Discovery.minClient(product: String = PRODUCT): String? {
|
||||
val said = (minClients as? JsonObject)?.get(product) as? JsonPrimitive ?: return null
|
||||
return said.takeIf { it.isString }?.content?.trim()?.ifEmpty { null }
|
||||
}
|
||||
|
||||
private val VERSION = Regex("""^v?(\d+)\.(\d+)\.(\d+)(?:-beta\.(\d+))?""")
|
||||
|
||||
/**
|
||||
* A version as numbers that compare: `1.2.3` or `1.2.3-beta.4`, a release above every beta of
|
||||
* itself. Whatever follows is a build's own mark (`-debug.12`, a flavour) and is left off. Null
|
||||
* for what is no such version.
|
||||
*/
|
||||
fun versionNumbers(text: String): List<Int>? {
|
||||
val match = VERSION.find(text.trim()) ?: return null
|
||||
val (major, minor, patch, beta) = match.destructured
|
||||
val parts = listOf(major.toIntOrNull(), minor.toIntOrNull(), patch.toIntOrNull(), if (beta.isEmpty()) Int.MAX_VALUE else beta.toIntOrNull())
|
||||
return if (parts.any { it == null }) null else parts.filterNotNull()
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the version [mine] is older than [needed]. By numbers, never as text: `1.0.0-beta.10`
|
||||
* is newer than `1.0.0-beta.9`, and `1.0.0` newer than both. False when either cannot be read:
|
||||
* a document that says nonsense locks nobody out.
|
||||
*/
|
||||
fun olderThan(mine: String, needed: String): Boolean {
|
||||
val own = versionNumbers(mine) ?: return false
|
||||
val least = versionNumbers(needed) ?: return false
|
||||
for (i in own.indices) if (own[i] != least[i]) return own[i] < least[i]
|
||||
return false
|
||||
}
|
||||
@@ -33,6 +33,12 @@ data class Discovery(
|
||||
@SerialName("app_api") val appApi: Int = 0,
|
||||
val login: Login = Login(),
|
||||
val scopes: List<String> = emptyList(),
|
||||
/**
|
||||
* The oldest version of each client the instance works with, by product (`{"android": "1.0.0"}`).
|
||||
* Kept as whatever the document says -- read through [minClient], so that a shape this app
|
||||
* does not expect is "says nothing", never "not a LLeMbas instance".
|
||||
*/
|
||||
@SerialName("min_clients") val minClients: JsonElement? = null,
|
||||
) {
|
||||
@Serializable
|
||||
data class Login(val device: DeviceLogin = DeviceLogin())
|
||||
|
||||
@@ -9,6 +9,7 @@ import androidx.datastore.preferences.core.stringPreferencesKey
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import eu.llembas.android.api.ApiClient
|
||||
import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.sameBase
|
||||
import eu.llembas.android.wake.WakeAttempt
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.first
|
||||
@@ -23,6 +24,10 @@ import javax.crypto.spec.GCMParameterSpec
|
||||
|
||||
/**
|
||||
* One instance this phone is signed in to. The token is stored encrypted, never in the clear.
|
||||
* [base] is the address the person typed, as the address rule reads it; what the instance says
|
||||
* of its own address is not kept. [id] is a key and nothing more: whatever it was made from, it
|
||||
* is never taken apart or made again -- the choices kept for the instance, its notifications and
|
||||
* its push registration all hang on it.
|
||||
* [signedOut]: the instance refused that token (it was revoked there, or it expired). The entry
|
||||
* stays -- with its place in the list and every choice kept for it -- until the person signs in
|
||||
* again, which replaces the token, or removes it; meanwhile nothing more is asked with it.
|
||||
@@ -93,6 +98,21 @@ object InstanceLogic {
|
||||
fun pickActive(instances: List<Instance>, activeId: String?, defaultId: String?): Instance? =
|
||||
instances.firstOrNull { it.id == activeId } ?: pickDefault(instances, defaultId)
|
||||
|
||||
/**
|
||||
* The entry a finished sign-in is kept as. An instance already here for the same account --
|
||||
* its stored base is the address just typed, or the one the instance says of itself
|
||||
* ([said], which older versions of the app stored) -- is that same instance: it keeps its
|
||||
* id, and so everything kept under it, and gets the new token and the typed address. Only an
|
||||
* instance that is not here yet is a new entry, its id made from the typed address.
|
||||
*/
|
||||
fun signedIn(instances: List<Instance>, typed: String, said: String, name: String, email: String, sealedToken: String): Instance {
|
||||
val twin = instances.firstOrNull {
|
||||
it.email.equals(email, ignoreCase = true) && (sameBase(it.base, typed) || (said.isNotBlank() && sameBase(it.base, said)))
|
||||
}
|
||||
return twin?.copy(base = typed, name = name, sealedToken = sealedToken, signedOut = false)
|
||||
?: Instance(id = "$email@$typed", base = typed, name = name, email = email, sealedToken = sealedToken)
|
||||
}
|
||||
|
||||
/** Adding [added] replaces any existing entry with the same id, makes it active, and sets it
|
||||
* as default only when no surviving default exists yet. */
|
||||
fun afterAdd(instances: List<Instance>, added: Instance, currentDefault: String?): Triple<List<Instance>, String, String> {
|
||||
@@ -239,22 +259,21 @@ class InstanceStore(private val context: Context) {
|
||||
|
||||
fun clientFor(instance: Instance): ApiClient = ApiClient(instance.base, TokenCipher.open(instance.sealedToken))
|
||||
|
||||
suspend fun add(base: String, name: String, email: String, token: String): Instance {
|
||||
val instance = Instance(
|
||||
id = "$email@$base",
|
||||
base = base,
|
||||
name = name,
|
||||
email = email,
|
||||
sealedToken = TokenCipher.seal(token),
|
||||
)
|
||||
/**
|
||||
* A sign-in finished: at [base], the address the person typed as the address rule reads it,
|
||||
* which is the one kept. [said] is the instance's own word for its address -- never stored,
|
||||
* and looked at only to know an instance signed in to before ([InstanceLogic.signedIn]).
|
||||
*/
|
||||
suspend fun add(base: String, said: String, name: String, email: String, token: String) {
|
||||
val locked = TokenCipher.seal(token)
|
||||
context.store.edit { prefs ->
|
||||
val all = prefs[INSTANCES]?.let { runCatching { LembasJson.decodeFromString(listSerializer, it) }.getOrNull() } ?: emptyList()
|
||||
val instance = InstanceLogic.signedIn(all, base, said, name, email, locked)
|
||||
val (next, activeId, defaultId) = InstanceLogic.afterAdd(all, instance, prefs[DEFAULT])
|
||||
prefs[INSTANCES] = LembasJson.encodeToString(listSerializer, next)
|
||||
prefs[ACTIVE] = activeId
|
||||
prefs[DEFAULT] = defaultId
|
||||
}
|
||||
return instance
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -90,11 +90,10 @@ import eu.llembas.android.api.LembasJson
|
||||
import eu.llembas.android.api.ReplyFrame
|
||||
import eu.llembas.android.api.ReportsFrame
|
||||
import eu.llembas.android.api.SearchHit
|
||||
import eu.llembas.android.api.fit
|
||||
import eu.llembas.android.auth.Instance
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.explain
|
||||
import eu.llembas.android.ui.signin.refusal
|
||||
import eu.llembas.android.ui.signin.unfit
|
||||
import eu.llembas.android.ui.theme.LocalPalette
|
||||
import kotlinx.coroutines.CancellationException
|
||||
import kotlinx.coroutines.Job
|
||||
@@ -200,7 +199,7 @@ class ChatsViewModel(app: Application) : AndroidViewModel(app) {
|
||||
val state = _state.asStateFlow()
|
||||
|
||||
/** Why this instance is refused, once it was asked what it is; null while it fits. */
|
||||
private var refused: Int? = null
|
||||
private var refused: String? = null
|
||||
private var checked = false
|
||||
|
||||
/** Read the list; the rows already shown stay while it is read again. */
|
||||
@@ -214,11 +213,11 @@ class ChatsViewModel(app: Application) : AndroidViewModel(app) {
|
||||
if (!checked) {
|
||||
runCatching { Doorstep().discover(api.base) }.onSuccess {
|
||||
checked = true
|
||||
refused = refusal(fit(it))
|
||||
refused = getApplication<Application>().unfit(it)
|
||||
}
|
||||
}
|
||||
refused?.let { said ->
|
||||
_state.value = ChatsState(error = getApplication<Application>().getString(said))
|
||||
_state.value = ChatsState(error = said)
|
||||
return@launch
|
||||
}
|
||||
val told = reportsTold
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
package eu.llembas.android.ui.signin
|
||||
|
||||
import android.app.Application
|
||||
import android.content.Context
|
||||
import android.net.Uri
|
||||
import android.os.Build
|
||||
import androidx.browser.customtabs.CustomTabsIntent
|
||||
@@ -48,13 +49,18 @@ import androidx.lifecycle.AndroidViewModel
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import androidx.lifecycle.viewModelScope
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import eu.llembas.android.BuildConfig
|
||||
import eu.llembas.android.R
|
||||
import eu.llembas.android.api.Address
|
||||
import eu.llembas.android.api.ApiException
|
||||
import eu.llembas.android.api.Discovery
|
||||
import eu.llembas.android.api.Doorstep
|
||||
import eu.llembas.android.api.ServerFit
|
||||
import eu.llembas.android.api.address
|
||||
import eu.llembas.android.api.fit
|
||||
import eu.llembas.android.api.minClient
|
||||
import eu.llembas.android.api.olderThan
|
||||
import eu.llembas.android.api.onOrigin
|
||||
import eu.llembas.android.auth.Instance
|
||||
import eu.llembas.android.lembas
|
||||
import eu.llembas.android.ui.explain
|
||||
@@ -86,6 +92,22 @@ fun refusal(fit: ServerFit): Int? = when (fit) {
|
||||
ServerFit.TOO_NEW -> R.string.signin_too_new
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether the instance [found] describes asks for a newer app than [version]: the version it
|
||||
* needs, or null. Asked only of a LLeMbas instance -- what another service says is not a rule.
|
||||
*/
|
||||
fun newerAppNeeded(found: Discovery, version: String): String? = found.minClient()?.takeIf { found.service == "lembas" && olderThan(version, it) }
|
||||
|
||||
/**
|
||||
* Why this app will not work with the instance [found] describes, in words, or null when it
|
||||
* will. An instance that names an oldest version for this app, above [version], says so with
|
||||
* both versions -- "update the app" -- before anything else is asked of it.
|
||||
*/
|
||||
fun Context.unfit(found: Discovery, version: String = BuildConfig.VERSION_NAME): String? {
|
||||
newerAppNeeded(found, version)?.let { needed -> return getString(R.string.signin_app_too_old, needed, version) }
|
||||
return refusal(fit(found))?.let { getString(it) }
|
||||
}
|
||||
|
||||
/** The sentence for an address that cannot be used as it was typed, or null for one that can (or for none yet). */
|
||||
fun refusal(read: Address): Int? = when (read) {
|
||||
is Address.Base, Address.Empty -> null
|
||||
@@ -126,8 +148,17 @@ class SignInViewModel(app: Application) : AndroidViewModel(app) {
|
||||
val doorstep = Doorstep()
|
||||
try {
|
||||
val found = doorstep.discover(base)
|
||||
refusal(fit(found))?.let { said ->
|
||||
_state.value = SignInState.Failed(getApplication<Application>().getString(said))
|
||||
getApplication<Application>().unfit(found)?.let { said ->
|
||||
_state.value = SignInState.Failed(said)
|
||||
return@launch
|
||||
}
|
||||
// The sign-in goes to the address that was typed and to no other host: where the
|
||||
// document names its doors elsewhere (a proxy that does not pass the address on),
|
||||
// that is said, rather than a code asked of a host the person never named.
|
||||
val doors = found.login.device
|
||||
if (!onOrigin(base, doors.code) || !onOrigin(base, doors.token)) {
|
||||
val named = doors.code.ifBlank { found.baseUrl }
|
||||
_state.value = SignInState.Failed(getApplication<Application>().getString(R.string.signin_other_origin, named))
|
||||
return@launch
|
||||
}
|
||||
val device = getApplication<Application>().getString(R.string.device_name)
|
||||
@@ -161,7 +192,8 @@ class SignInViewModel(app: Application) : AndroidViewModel(app) {
|
||||
answer.accessToken != null -> {
|
||||
val account = answer.account
|
||||
getApplication<Application>().lembas.instances.add(
|
||||
base = found.baseUrl.ifEmpty { base }.trimEnd('/'),
|
||||
base = base,
|
||||
said = found.baseUrl,
|
||||
name = found.name,
|
||||
email = account?.email.orEmpty(),
|
||||
token = answer.accessToken,
|
||||
|
||||
Reference in new issue
Block a user