Repository Public
1// What SP Vault tells the operating system so that it can offer credentials in the keyboard / QuickType bar and in2// Safari: for each login a site name and a username, for each passkey the relying party, account name and3// credential id, for each one-time code a site name and a label. Never a password, a private key or a TOTP secret.4// The list lives in the system's `ASCredentialIdentityStore`; it is replaced after each unlock / sync and erased on5// sign-out, on "remove this device" and when the user turns suggestions off.6import AuthenticationServices7import Foundation8import SharedVaultKit910/// A one-time code the system can offer (`ASOneTimeCodeCredentialIdentity`, iOS 18 / macOS 15).11struct OneTimeCodeIdentity: Sendable, Equatable, Hashable {12 let recordIdentifier: String13 let serviceIdentifier: String14 let label: String15}1617enum CredentialIdentitySync {18 /// One identity per https host of each login that has a valid TOTP secret (same URL rules as passwords).19 static func oneTimeCodeIdentities(for items: [ItemView]) -> [OneTimeCodeIdentity] {20 var out: [OneTimeCodeIdentity] = []21 for it in items where it.item.type == .login && (it.item.trashedAt ?? "").isEmpty {22 guard let secret = it.item.fields["totp"], !secret.isEmpty, (try? SPV1.parseTotp(secret)) != nil else { continue }23 let user = it.item.fields["username"] ?? ""24 var seen = Set<String>()25 for u in it.item.urls where u.match != .never {26 guard let o = SPV1.parseOrigin(u.url), o.scheme == "https" || o.host == "localhost" || o.host == "127.0.0.1" else { continue }27 if seen.insert(o.host).inserted { out.append(OneTimeCodeIdentity(recordIdentifier: it.id, serviceIdentifier: o.host, label: user.isEmpty ? it.item.title : user)) }28 }29 }30 return out31 }3233 static func systemIdentities(for items: [ItemView]) -> [any ASCredentialIdentity] {34 let ids = VaultReader.credentialIdentities(for: items)35 var out: [any ASCredentialIdentity] = ids.passwords.map {36 ASPasswordCredentialIdentity(serviceIdentifier: ASCredentialServiceIdentifier(identifier: $0.serviceIdentifier, type: .domain), user: $0.user, recordIdentifier: $0.recordIdentifier)37 }38 out += ids.passkeys.map {39 ASPasskeyCredentialIdentity(relyingPartyIdentifier: $0.relyingPartyIdentifier, userName: $0.userName, credentialID: $0.credentialID, userHandle: $0.userHandle, recordIdentifier: $0.recordIdentifier)40 }41 out += oneTimeCodeIdentities(for: items).map {42 ASOneTimeCodeCredentialIdentity(serviceIdentifier: ASCredentialServiceIdentifier(identifier: $0.serviceIdentifier, type: .domain), label: $0.label, recordIdentifier: $0.recordIdentifier)43 }44 return out45 }4647 /// Whether the user enabled SP Vault as an AutoFill provider in the system settings.48 static func isProviderEnabled() async -> Bool { await ASCredentialIdentityStore.shared.state().isEnabled }4950 /// Replaces the system's list. A no-op (and not an error) while the provider is disabled in the system settings.51 static func replace(with items: [ItemView]) async {52 guard await isProviderEnabled() else { return }53 try? await ASCredentialIdentityStore.shared.replaceCredentialIdentities(systemIdentities(for: items))54 }5556 /// Adds identities without touching the others (the extension, right after a passkey was created).57 static func add(for items: [ItemView]) async {58 guard await isProviderEnabled() else { return }59 try? await ASCredentialIdentityStore.shared.saveCredentialIdentities(systemIdentities(for: items))60 }6162 static func clear() async { try? await ASCredentialIdentityStore.shared.removeAllCredentialIdentities() }63}64