Files
hapi/ios/Hapi/Features/Pairing/PairingConfirmView.swift
T
weishu 59a486ba88 feat: passkey sign-in for the iOS and Android apps
Native apps cannot follow a self-hosted hub's domain (entitlements and
asset links are fixed at build time), so they use one shared RP ID,
hapi.run, and every hub accepts it only from exact app origins.

- hub: `{"client":"native"}` on the passkey option routes selects the
  native RP; origins are https://hapi.run (iOS/macOS) and the official
  android:apk-key-hash values, extendable via HAPI_PASSKEY_NATIVE_ORIGINS
  (HAPI_PASSKEY_NATIVE_RP_ID for forks). Native login options restrict
  allowCredentials to this hub's passkeys. Accept a null
  authenticatorAttachment.
- website: publish apple-app-site-association (webcredentials for
  XJB862F92W.run.hapi.app) and assetlinks.json (get_login_creds for
  run.hapi.companion; signing fingerprint still a TODO placeholder).
- iOS: webcredentials:hapi.run, AuthenticationServices ceremonies,
  passkey-only sign-in on the pairing screen, Settings → Passkeys, and a
  passkey-session credential kind used by silent re-auth.
- Android: Credential Manager 1.6.0, the same flows and credential kind,
  and README notes on computing the apk-key-hash origin.
- docs: native passkey contract and user guide.
2026-11-16 17:24:04 +08:00

145 lines
6.0 KiB
Swift

import HapiClient
import SwiftUI
/// Confirmation step for the deep-link path (`hapicompanion://bind`) — the
/// one entry where an external link initiates pairing, so an explicit user
/// confirmation stays in between. Manual entry and QR scan pair inline.
///
/// On success `AppModel` clears all pairing presentation state and flips to
/// `.paired`, which dismisses this view's container — nothing to do here.
struct PairingConfirmView: View {
let pending: PendingPairing
@Environment(AppModel.self) private var model
@State private var attempt = PairingAttempt()
private var isAddingAnotherHub: Bool {
if case .paired = model.state { return true }
return false
}
var body: some View {
Form {
Section {
LabeledContent("Hub") {
Text(pending.hubUrl)
.multilineTextAlignment(.trailing)
.textSelection(.enabled)
}
LabeledContent("Access token") {
Text(HubDisplay.maskedToken(pending.accessToken))
.monospaced()
}
} footer: {
Text(isAddingAnotherHub
? String(localized: "This hub will be added to your paired hubs and become active.")
: String(localized: "The app checks the hub is reachable, then exchanges the token for a session."))
}
if let failure = attempt.failure {
Section {
PairingErrorView(failure: failure)
}
}
Section {
Button {
attempt.pair(model, hubUrl: pending.hubUrl, accessToken: pending.accessToken)
} label: {
if attempt.isPairing {
HStack(spacing: 8) {
ProgressView()
Text("Pairing…")
}
.frame(maxWidth: .infinity)
} else {
Text(attempt.failure == nil ? String(localized: "Pair") : String(localized: "Try Again"))
.frame(maxWidth: .infinity)
}
}
.disabled(attempt.isPairing)
}
}
.navigationTitle(isAddingAnotherHub ? String(localized: "Add Hub") : String(localized: "Pair"))
.navigationBarTitleDisplayMode(.inline)
.interactiveDismissDisabled(attempt.isPairing)
}
}
/// The pairing screen's error states, one presentation per
/// ``PairingFailure`` case.
struct PairingErrorView: View {
let failure: PairingFailure
var body: some View {
Label {
VStack(alignment: .leading, spacing: 4) {
Text(title)
.font(.headline)
Text(message)
.font(.subheadline)
.foregroundStyle(.secondary)
}
} icon: {
Image(systemName: icon)
.foregroundStyle(.red)
}
}
private var icon: String {
switch failure {
case .invalidHubURL: "questionmark.circle"
case .unreachable: "wifi.exclamationmark"
case .protocolMismatch: "arrow.triangle.2.circlepath"
case .invalidAccessToken: "key"
case .hubError: "exclamationmark.triangle"
case .storageFailure: "externaldrive.badge.xmark"
case .noPasskeys, .passkeyRejected, .passkeyUnavailable: "person.badge.key"
}
}
private var title: String {
switch failure {
case .invalidHubURL: String(localized: "Invalid hub URL")
case .unreachable: String(localized: "Hub unreachable")
case .protocolMismatch: String(localized: "Version mismatch")
case .invalidAccessToken: String(localized: "Token rejected")
case .hubError: String(localized: "Hub error")
case .storageFailure: String(localized: "Could not save")
case .noPasskeys: String(localized: "No passkeys for this hub")
case .passkeyRejected: String(localized: "Passkey rejected")
case .passkeyUnavailable: String(localized: "Passkey unavailable")
}
}
private var message: String {
switch failure {
case .invalidHubURL:
String(localized: "Check the hub address. HTTPS is recommended, for example https://hub.example.com.")
case .unreachable:
String(localized: "No HAPI hub answered at this address. Check that the hub is running and that this device can reach it (same network, or the relay tunnel is up).")
case .protocolMismatch(let hubVersion, let supportedVersion):
String(
format: String(localized: "The hub speaks protocol v%lld, this app supports v%lld. Update the older side, then try again."),
Int64(hubVersion),
Int64(supportedVersion)
)
case .invalidAccessToken:
String(localized: "The hub rejected this access token — it may have been rotated. Get a fresh pairing code from the hub and try again.")
case .hubError(let status):
String(
format: String(localized: "The hub answered unexpectedly (HTTP %lld). Try again in a moment."),
Int64(status)
)
case .storageFailure:
String(localized: "Pairing succeeded but the credentials could not be stored on this device. Try again.")
case .noPasskeys:
String(localized: "This hub has no passkeys for the app yet. Pair with the access token, then add a passkey in Settings → Passkeys.")
case .passkeyRejected:
String(localized: "The hub did not accept this passkey. It may have been deleted, or the hub's access token was rotated. Pair with the access token instead.")
case .passkeyUnavailable:
String(localized: "This device could not use a passkey for this hub. Try again, or pair with the access token.")
}
}
}