Files
hapi/ios/Hapi/Features/Pairing/PairingConfirmView.swift
T
weishu 75aa277aac feat(auth): sign in to native apps with a single passkey ceremony
Hub discovery required two authenticator prompts plus a confirm step:
a local locator-only ceremony to read the Hub from the user handle, then a
Hub-issued login challenge. Native apps now sign their own fresh challenge
{v:2, operation:"login", channel:"native", iat, nonce}, read the Hub from
the selected credential's v2 user handle, and post the assertion straight to
POST /api/passkey/login/verify.

- hub: accept native self-issued challenges only within
  [now-5min, now+1min] and only once (replay guard keyed on the challenge,
  claimed synchronously after verification); remove
  /api/passkey/login/options. Browser ceremonies keep Hub-issued challenges.
- shared: createPasskeyNativeLoginChallenge / parsePasskeyNativeLoginChallenge;
  drop discovery helpers.
- ios/android: one system sheet from the pairing screen; manual Hub entry
  for long-address handles or an explicit Hub, which must match the
  credential before anything is sent. Assertions are only posted to the Hub
  named by the credential.
- web: revert address-free discovery; the web app signs in to its own Hub.
- docs: native single-ceremony contract and its security trade-off.
2026-11-17 15:09:53 +08:00

148 lines
6.2 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 .passkeyRejected, .passkeyUnavailable, .passkeyHubAddressRequired, .passkeyWrongHub: "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 .passkeyRejected: String(localized: "Passkey rejected")
case .passkeyUnavailable: String(localized: "Passkey unavailable")
case .passkeyHubAddressRequired: String(localized: "Hub address needed")
case .passkeyWrongHub: String(localized: "Passkey for another hub")
}
}
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 .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.")
case .passkeyHubAddressRequired:
String(localized: "This passkey does not include its hub address. Enter the hub URL, then sign in with the same passkey.")
case .passkeyWrongHub:
String(localized: "This passkey belongs to a different hub. Choose a passkey for this hub, or check the hub address.")
}
}
}