Fichiers
Aleop c3608b3938 Loksi 2026.10.0 (Android 3) : source correspondante
Export du dépôt interne d'Aleop, révision 5fbb921.
2026-10-09 22:14:30 +02:00

87 lignes
3.1 KiB
Kotlin

// GPL-3.0-only. Loksi: vault name or address entered at login (decision of 04/10/2026).
// Copied by mobile/android/loksi.py next to EnvironmentUrlDataJsonExtensions.kt. Same rule
// as the Loksi browser extension (extensions/loksi-coffre.ts).
package com.bitwarden.data.repository.util
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import kotlinx.serialization.json.Json
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import java.net.HttpURLConnection
import java.net.URI
import java.net.URL
/**
* Loksi: domain of the Loksi vaults. The vault name "abc" gives https://abc.loksi.fr.
*/
const val LOKSI_DOMAIN: String = "loksi.fr"
// The name chosen in the order funnel: same pattern as the order backend (_NOM), 2 to 20
// lowercase letters or digits, a hyphen allowed but neither first nor last. Never
// interpolated as typed: it matches this pattern or it is refused.
private val LOKSI_VAULT_NAME = Regex("^[a-z0-9](?:[a-z0-9-]{0,18}[a-z0-9])$")
private const val LOKSI_TIMEOUT_MS: Int = 10_000
/**
* Loksi: base address of the vault for what was typed, or null if it is refused.
* A vault name gives https://<name>.loksi.fr. Otherwise a full https address without
* credentials, query or fragment, for a customer with their own domain.
*/
fun loksiVaultAddress(input: String?): String? {
val value = input.orEmpty().trim()
if (LOKSI_VAULT_NAME.matches(value)) {
return "https://$value.$LOKSI_DOMAIN"
}
if (!value.startsWith("https://")) {
return null
}
val uri = try {
URI(value)
} catch (_: Exception) {
return null
}
if (uri.scheme != "https" || uri.host.isNullOrEmpty() || uri.rawUserInfo != null ||
uri.rawQuery != null || uri.rawFragment != null
) {
return null
}
val port = if (uri.port == -1) "" else ":${uri.port}"
return "https://${uri.host}$port${uri.rawPath.orEmpty()}".trimEnd('/')
}
/**
* Loksi: true only if a Vaultwarden server answers on /api/config at [base].
* [fetch] returns the body of a successful GET, or null.
*/
suspend fun isLoksiVault(
base: String,
fetch: suspend (String) -> String? = ::loksiGet,
): Boolean =
try {
val body = fetch("$base/api/config")
body != null &&
Json.parseToJsonElement(body).jsonObject["server"]
?.jsonObject?.get("name")?.jsonPrimitive?.content == "Vaultwarden"
} catch (_: Exception) {
false
}
private suspend fun loksiGet(url: String): String? = withContext(Dispatchers.IO) {
val connection = URL(url).openConnection() as HttpURLConnection
try {
connection.connectTimeout = LOKSI_TIMEOUT_MS
connection.readTimeout = LOKSI_TIMEOUT_MS
connection.instanceFollowRedirects = false
connection.setRequestProperty("Accept", "application/json")
if (connection.responseCode == HttpURLConnection.HTTP_OK) {
connection.inputStream.bufferedReader().use { it.readText() }
} else {
null
}
} finally {
connection.disconnect()
}
}