// GPL-3.0-only. Loksi: vault name or address entered at login (decision of 04/10/2026). // Copied by mobile/ios/loksi.py into BitwardenShared. Same rule as the Loksi browser // extension (extensions/loksi-coffre.ts) and the Android app (LoksiVault.kt). import Foundation /// Loksi: turns what the user typed into a vault address, and checks that a vault answers. /// enum LoksiVault { /// Domain of the Loksi vaults. The vault name "abc" gives https://abc.loksi.fr. static let domain = "loksi.fr" /// The name chosen in the order funnel: 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 static let namePattern = "^[a-z0-9](?:[a-z0-9-]{0,18}[a-z0-9])$" /// Time allowed for the /api/config check. private static let timeout: TimeInterval = 10 /// Base address of the vault for what was typed, or `nil` if it is refused. /// /// A vault name gives https://.loksi.fr. Otherwise a full https address without /// credentials, query or fragment, for a customer with their own domain. /// static func address(for input: String) -> URL? { let value = input.trimmingCharacters(in: .whitespacesAndNewlines) if value.range(of: namePattern, options: .regularExpression) != nil { return URL(string: "https://\(value).\(domain)") } guard value.hasPrefix("https://"), let components = URLComponents(string: value), components.scheme == "https", let host = components.host, !host.isEmpty, components.user == nil, components.password == nil, components.query == nil, components.fragment == nil else { return nil } var path = components.percentEncodedPath while path.hasSuffix("/") { path.removeLast() } let port = components.port.map { ":\($0)" } ?? "" return URL(string: "https://\(host)\(port)\(path)") } /// Whether a Vaultwarden server answers on /api/config at `base`. /// /// - Parameters: /// - base: The vault address returned by `address(for:)`. /// - fetch: Returns the body of a successful GET, or `nil`. /// static func isVault( _ base: URL, fetch: (URL) async -> Data? = LoksiVault.get, ) async -> Bool { guard let body = await fetch(base.appendingPathComponent("api/config")), let json = try? JSONSerialization.jsonObject(with: body) as? [String: Any], let server = json["server"] as? [String: Any] else { return false } return server["name"] as? String == "Vaultwarden" } /// A GET without cookies or redirects, the body if the answer is 200. /// static func get(_ url: URL) async -> Data? { let configuration = URLSessionConfiguration.ephemeral configuration.timeoutIntervalForRequest = timeout let session = URLSession(configuration: configuration, delegate: NoRedirect(), delegateQueue: nil) defer { session.finishTasksAndInvalidate() } var request = URLRequest(url: url) request.setValue("application/json", forHTTPHeaderField: "Accept") guard let (data, response) = try? await session.data(for: request), (response as? HTTPURLResponse)?.statusCode == 200 else { return nil } return data } /// Refuses redirects: the vault must answer at the address itself. /// private final class NoRedirect: NSObject, URLSessionTaskDelegate { func urlSession( _ session: URLSession, task: URLSessionTask, willPerformHTTPRedirection response: HTTPURLResponse, newRequest request: URLRequest, ) async -> URLRequest? { nil } } }