Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Fix iOS passkey data type parsing (#520)

Leendert de Borstc2b6e8af1ef6
diff --git a/apps/mobile-app/ios/Autofill/CredentialIdentityStore.swift b/apps/mobile-app/ios/Autofill/CredentialIdentityStore.swift
index d9e2eb54c..12c959974 100644
--- a/apps/mobile-app/ios/Autofill/CredentialIdentityStore.swift
+++ b/apps/mobile-app/ios/Autofill/CredentialIdentityStore.swift
@@ -15,13 +15,16 @@ public class CredentialIdentityStore {
private init() {}
/// Save credentials into the native iOS credential store.
- public func saveCredentialIdentities(_ credentials: [Credential]) async throws {
+ /// - Parameters:
+ /// - credentials: The credentials to register
+ /// - passkeyOnly: If true, only register passkey identities (skip passwords). Default is false.
+ public func saveCredentialIdentities(_ credentials: [Credential], passkeyOnly: Bool = false) async throws {
// TODO: improve implementation to better separate password and passkey identities.
// As if a record has both a password and passkey, it will not only show up as a password identity.
var allIdentities: [ASCredentialIdentity] = []
- // Create password identities
- let passwordIdentities: [ASPasswordCredentialIdentity] = credentials.compactMap { credential in
+ // Create password identities (skip if passkeyOnly mode)
+ let passwordIdentities: [ASPasswordCredentialIdentity] = passkeyOnly ? [] : credentials.compactMap { credential in
guard let urlString = credential.service.url,
let url = URL(string: urlString),
let host = url.host else {
@@ -51,45 +54,56 @@ public class CredentialIdentityStore {
// Create passkey identities
let passkeyIdentities: [ASPasskeyCredentialIdentity] = credentials.flatMap { credential -> [ASPasskeyCredentialIdentity] in
- guard let passkeys = credential.passkeys,
- let urlString = credential.service.url,
- let url = URL(string: urlString),
- let host = url.host else {
+ guard let passkeys = credential.passkeys else {
return []
}
- let effectiveDomain = Self.effectiveDomain(from: host)
-
- return passkeys.compactMap { passkey in
- guard !passkey.isDeleted else { return nil }
-
- return ASPasskeyCredentialIdentity(
- relyingPartyIdentifier: passkey.rpId,
- userName: passkey.userName ?? usernameOrEmail(credential: credential),
- credentialID: passkey.credentialId,
- userHandle: passkey.userId ?? Data(),
- recordIdentifier: passkey.id.uuidString
- )
- }
+ return passkeys.filter { !($0.isDeleted) }
+ .compactMap { passkey in
+ // Get the userName for display in iOS AutoFill UI
+ // Passkeys don't store userName in the database, so we use the credential's username or email
+ let userName = passkey.userName ?? usernameOrEmail(credential: credential)
+
+ // iOS requires a non-empty userName to display the passkey in AutoFill
+ if userName.isEmpty {
+ print("CredentialIdentityStore: Skipping passkey \(passkey.id) - userName is empty")
+ return nil
+ }
+
+ // For passkeys, we use the rpId from the passkey itself, not the service URL
+ // This is because passkeys are tied to the RP ID, which may differ from the service URL
+ return ASPasskeyCredentialIdentity(
+ relyingPartyIdentifier: passkey.rpId,
+ userName: userName,
+ credentialID: passkey.credentialId,
+ userHandle: passkey.userHandle ?? Data(),
+ recordIdentifier: passkey.id.uuidString
+ )
+ }
}
allIdentities.append(contentsOf: passkeyIdentities)
+ print("CredentialIdentityStore: Registering \(passwordIdentities.count) password identities and \(passkeyIdentities.count) passkey identities")
+
guard !allIdentities.isEmpty else {
- print("No valid identities to save.")
+ print("CredentialIdentityStore: No valid identities to save.")
return
}
let state = await storeState()
guard state.isEnabled else {
- print("Credential identity store is not enabled.")
+ print("CredentialIdentityStore: Credential identity store is not enabled. Please enable AutoFill in iOS Settings.")
return
}
+ print("CredentialIdentityStore: Store is enabled, saving \(allIdentities.count) total identities")
+
do {
try await store.saveCredentialIdentities(allIdentities)
+ print("CredentialIdentityStore: Successfully saved all identities")
} catch {
- print("Failed to save credential identities to native iOS storage: \(error)")
+ print("CredentialIdentityStore: Failed to save credential identities: \(error)")
}
}
diff --git a/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Passkey.swift b/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Passkey.swift
index 4476cefb4..e1e1ca416 100644
--- a/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Passkey.swift
+++ b/apps/mobile-app/ios/Autofill/CredentialProviderViewController+Passkey.swift
@@ -317,7 +317,7 @@ extension CredentialProviderViewController {
clientDataHash: clientDataHash,
rpId: rpId,
privateKeyJWK: passkey.privateKey,
- userId: passkey.userId,
+ userId: passkey.userHandle,
uvPerformed: true,
prfInputs: nil,
prfSecret: passkey.prfKey
diff --git a/apps/mobile-app/ios/Autofill/CredentialProviderViewController.swift b/apps/mobile-app/ios/Autofill/CredentialProviderViewController.swift
index 13a6e630b..e3743c04d 100644
--- a/apps/mobile-app/ios/Autofill/CredentialProviderViewController.swift
+++ b/apps/mobile-app/ios/Autofill/CredentialProviderViewController.swift
@@ -272,7 +272,7 @@ public class CredentialProviderViewController: ASCredentialProviderViewControlle
clientDataHash: clientDataHash,
rpId: rpId,
privateKeyJWK: passkey.privateKey,
- userId: passkey.userId,
+ userId: passkey.userHandle,
uvPerformed: true,
prfInputs: nil,
prfSecret: passkey.prfKey
@@ -304,26 +304,27 @@ public class CredentialProviderViewController: ASCredentialProviderViewControlle
/// which iOS can then use to suggest autofill credentials when a user focuses an input field on a login form.
/// These suggestions will then be shown above the iOS keyboard, which saves the user one step.
///
- /// Note: QuickType bar suggestions are only enabled on iOS 26+ due to biometric authentication limitations
- /// in iOS 17 and 18 where background authentication doesn't work reliably.
+ /// Note: Password QuickType bar suggestions are only enabled on iOS 26+ due to biometric authentication limitations
+ /// in iOS 17 and 18 where background authentication doesn't work reliably. However, PASSKEY identities must be
+ /// registered on all iOS versions (17+) so iOS knows to call this extension for passkey authentication requests.
private func registerCredentialIdentities(credentials: [Credential]) async {
- // Only register credentials for QuickType on iOS 26+
- // iOS 17 and 18 have issues with background biometric authentication, so we disable QuickType there
- if #available(iOS 26.0, *) {
- do {
+ do {
+ if #available(iOS 26.0, *) {
+ // iOS 26+: Register both passwords and passkeys for QuickType and manual selection
try await CredentialIdentityStore.shared.saveCredentialIdentities(credentials)
print("Registered credential identities (passwords + passkeys) for QuickType on iOS 26+")
- } catch {
- print("Failed to save credential identities: \(error)")
- }
- } else {
- // On iOS 17-18, clear any existing identities to ensure nothing shows in QuickType
- do {
- try await CredentialIdentityStore.shared.removeAllCredentialIdentities()
- print("Cleared credential identities on iOS <26 to disable QuickType suggestions")
- } catch {
- print("Failed to clear credential identities: \(error)")
+ } else {
+ // iOS 17-25: Only register passkeys (skip passwords for QuickType to avoid biometric issues)
+ // But passkeys MUST be registered so iOS knows to offer this extension for passkey authentication
+ let passkeyOnlyCredentials = credentials.filter { credential in
+ guard let passkeys = credential.passkeys else { return false }
+ return !passkeys.isEmpty
+ }
+ try await CredentialIdentityStore.shared.saveCredentialIdentities(passkeyOnlyCredentials, passkeyOnly: true)
+ print("Registered \(passkeyOnlyCredentials.count) passkey identities on iOS <26 (password QuickType disabled)")
}
+ } catch {
+ print("Failed to save credential identities: \(error)")
}
}
diff --git a/apps/mobile-app/ios/VaultModels/Passkey.swift b/apps/mobile-app/ios/VaultModels/Passkey.swift
index 615ae7fc0..54e438b4d 100644
--- a/apps/mobile-app/ios/VaultModels/Passkey.swift
+++ b/apps/mobile-app/ios/VaultModels/Passkey.swift
@@ -6,10 +6,9 @@ import Foundation
*/
public struct Passkey: Codable, Hashable, Equatable {
public let id: UUID
- public let credentialId: Data // 16-byte GUID, converted to base64url for WebAuthn
- public let credentialIdString: String // Parent credential UUID
+ public let credentialId: Data // Credential ID as supplied originally by the RP
public let rpId: String // Relying party identifier (domain)
- public let userId: Data? // User identifier from RP (optional)
+ public let userHandle: Data? // User ID/handle from RP (optional)
public let userName: String? // User-visible identifier (typically email)
public let publicKey: Data // JWK format (JSON)
public let privateKey: Data // JWK format (JSON), encrypted in storage
@@ -22,9 +21,8 @@ public struct Passkey: Codable, Hashable, Equatable {
public init(
id: UUID,
credentialId: Data,
- credentialIdString: String,
rpId: String,
- userId: Data?,
+ userHandle: Data?,
userName: String?,
publicKey: Data,
privateKey: Data,
@@ -36,9 +34,8 @@ public struct Passkey: Codable, Hashable, Equatable {
) {
self.id = id
self.credentialId = credentialId
- self.credentialIdString = credentialIdString
self.rpId = rpId
- self.userId = userId
+ self.userHandle = userHandle
self.userName = userName
self.publicKey = publicKey
self.privateKey = privateKey
diff --git a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift
index 2e63d4a69..43a63afe9 100644
--- a/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift
+++ b/apps/mobile-app/ios/VaultStoreKit/VaultStore+Passkey.swift
@@ -5,10 +5,26 @@ import VaultModels
/**
* VaultStore+Passkey
* Extension to VaultStore for passkey operations
- * TODO: review file
*/
extension VaultStore {
+ // MARK: - Column Expressions
+
+ private static let passkeysTable = Table("Passkeys")
+
+ private static let colId = Expression<String>("Id")
+ private static let colCredentialId = Expression<String>("CredentialId")
+ private static let colRpId = Expression<String>("RpId")
+ private static let colUserId = Expression<String?>("UserId")
+ private static let colPublicKey = Expression<String>("PublicKey")
+ private static let colPrivateKey = Expression<String>("PrivateKey")
+ private static let colPrfKey = Expression<Blob?>("PrfKey")
+ private static let colDisplayName = Expression<String>("DisplayName")
+ private static let colAdditionalData = Expression<Blob?>("AdditionalData")
+ private static let colCreatedAt = Expression<String>("CreatedAt")
+ private static let colUpdatedAt = Expression<String>("UpdatedAt")
+ private static let colIsDeleted = Expression<Int64>("IsDeleted")
+
// MARK: - Passkey Queries
/**
@@ -19,79 +35,22 @@ extension VaultStore {
throw VaultStoreError.vaultNotUnlocked
}
- let query = """
- SELECT
- Id,
- CredentialId,
- RpId,
- UserId,
- PublicKey,
- PrivateKey,
- PrfKey,
- DisplayName,
- AdditionalData,
- CreatedAt,
- UpdatedAt,
- IsDeleted
- FROM Passkeys
- WHERE AdditionalData = ? AND IsDeleted = 0
- LIMIT 1
- """
+ print("VaultStore+Passkey: Looking up passkey by credentialId: \(credentialId.base64EncodedString().prefix(20))...")
let credentialIdBlob = Blob(bytes: [UInt8](credentialId))
+ let query = Self.passkeysTable
+ .filter(Self.colAdditionalData == credentialIdBlob)
+ .filter(Self.colIsDeleted == 0)
+ .limit(1)
- for row in try dbConn.prepare(query, [credentialIdBlob]) {
- guard let idString = row[0] as? String,
- let credentialIdString = row[1] as? String,
- let rpId = row[2] as? String,
- let publicKeyString = row[4] as? String,
- let privateKeyString = row[5] as? String,
- let displayName = row[7] as? String,
- let createdAtString = row[9] as? String,
- let updatedAtString = row[10] as? String,
- let isDeletedInt64 = row[11] as? Int64,
- let id = UUID(uuidString: idString),
- let parentCredentialId = UUID(uuidString: credentialIdString),
- let publicKeyData = publicKeyString.data(using: .utf8),
- let privateKeyData = privateKeyString.data(using: .utf8) else {
- continue
- }
-
- guard let createdAt = parseDateString(createdAtString),
- let updatedAt = parseDateString(updatedAtString) else {
- continue
- }
-
- let userId = (row[3] as? String)?.data(using: .utf8)
- let prfKey = (row[6] as? SQLite.Blob).map { Data($0.bytes) }
- let isDeleted = isDeletedInt64 == 1
-
- // Get the actual WebAuthn credential ID from AdditionalData column
- let webauthnCredentialId: Data
- if let additionalDataBlob = row[8] as? SQLite.Blob {
- webauthnCredentialId = Data(additionalDataBlob.bytes)
- } else {
- // Fallback: use the passkey ID as a GUID and convert to bytes
- webauthnCredentialId = (try? PasskeyHelper.guidToBytes(idString)) ?? Data()
+ for row in try dbConn.prepare(query) {
+ if let passkey = try parsePasskeyRow(row) {
+ print("VaultStore+Passkey: Found passkey - rpId=\(passkey.rpId), displayName=\(passkey.displayName), userId=\(passkey.userHandle?.base64EncodedString().prefix(20) ?? "(nil)")...")
+ return passkey
}
-
- return Passkey(
- id: id,
- credentialId: webauthnCredentialId,
- credentialIdString: parentCredentialId.uuidString,
- rpId: rpId,
- userId: userId,
- userName: nil,
- publicKey: publicKeyData,
- privateKey: privateKeyData,
- prfKey: prfKey,
- displayName: displayName,
- createdAt: createdAt,
- updatedAt: updatedAt,
- isDeleted: isDeleted
- )
}
+ print("VaultStore+Passkey: No passkey found with matching credentialId")
return nil
}
@@ -103,78 +62,16 @@ extension VaultStore {
throw VaultStoreError.vaultNotUnlocked
}
- let query = """
- SELECT
- Id,
- CredentialId,
- RpId,
- UserId,
- PublicKey,
- PrivateKey,
- PrfKey,
- DisplayName,
- AdditionalData,
- CreatedAt,
- UpdatedAt,
- IsDeleted
- FROM Passkeys
- WHERE CredentialId = ? AND IsDeleted = 0
- ORDER BY CreatedAt DESC
- """
+ let query = Self.passkeysTable
+ .filter(Self.colCredentialId == credentialId.uuidString)
+ .filter(Self.colIsDeleted == 0)
+ .order(Self.colCreatedAt.desc)
var passkeys: [Passkey] = []
- for row in try dbConn.prepare(query, [credentialId.uuidString]) {
- guard let idString = row[0] as? String,
- let credentialIdString = row[1] as? String,
- let rpId = row[2] as? String,
- let publicKeyString = row[4] as? String,
- let privateKeyString = row[5] as? String,
- let displayName = row[7] as? String,
- let createdAtString = row[9] as? String,
- let updatedAtString = row[10] as? String,
- let isDeletedInt64 = row[11] as? Int64,
- let id = UUID(uuidString: idString),
- let parentCredentialId = UUID(uuidString: credentialIdString),
- let publicKeyData = publicKeyString.data(using: .utf8),
- let privateKeyData = privateKeyString.data(using: .utf8) else {
- continue
- }
-
- guard let createdAt = parseDateString(createdAtString),
- let updatedAt = parseDateString(updatedAtString) else {
- continue
- }
-
- let userId = (row[3] as? String)?.data(using: .utf8)
- let prfKey = (row[6] as? SQLite.Blob).map { Data($0.bytes) }
- let isDeleted = isDeletedInt64 == 1
-
- // Get the actual WebAuthn credential ID from AdditionalData column
- let webauthnCredentialId: Data
- if let additionalDataBlob = row[8] as? SQLite.Blob {
- webauthnCredentialId = Data(additionalDataBlob.bytes)
- } else {
- // Fallback: use the passkey ID as a GUID and convert to bytes
- webauthnCredentialId = (try? PasskeyHelper.guidToBytes(idString)) ?? Data()
+ for row in try dbConn.prepare(query) {
+ if let passkey = try parsePasskeyRow(row) {
+ passkeys.append(passkey)
}
-
- let passkey = Passkey(
- id: id,
- credentialId: webauthnCredentialId,
- credentialIdString: parentCredentialId.uuidString,
- rpId: rpId,
- userId: userId,
- userName: nil,
- publicKey: publicKeyData,
- privateKey: privateKeyData,
- prfKey: prfKey,
- displayName: displayName,
- createdAt: createdAt,
- updatedAt: updatedAt,
- isDeleted: isDeleted
- )
-
- passkeys.append(passkey)
}
return passkeys
@@ -188,80 +85,21 @@ extension VaultStore {
throw VaultStoreError.vaultNotUnlocked
}
- let query = """
- SELECT
- Id,
- CredentialId,
- RpId,
- UserId,
- PublicKey,
- PrivateKey,
- PrfKey,
- DisplayName,
- AdditionalData,
- CreatedAt,
- UpdatedAt,
- IsDeleted
- FROM Passkeys
- WHERE RpId = ? AND IsDeleted = 0
- ORDER BY CreatedAt DESC
- """
-
- var passkeys: [Passkey] = []
- for row in try dbConn.prepare(query, [rpId]) {
- guard let idString = row[0] as? String,
- let credentialIdString = row[1] as? String,
- let rpId = row[2] as? String,
- let publicKeyString = row[4] as? String,
- let privateKeyString = row[5] as? String,
- let displayName = row[7] as? String,
- let createdAtString = row[9] as? String,
- let updatedAtString = row[10] as? String,
- let isDeletedInt64 = row[11] as? Int64,
- let id = UUID(uuidString: idString),
- let parentCredentialId = UUID(uuidString: credentialIdString),
- let publicKeyData = publicKeyString.data(using: .utf8),
- let privateKeyData = privateKeyString.data(using: .utf8) else {
- continue
- }
-
- guard let createdAt = parseDateString(createdAtString),
- let updatedAt = parseDateString(updatedAtString) else {
- continue
- }
+ print("VaultStore+Passkey: Looking up passkeys for rpId: \(rpId)")
- let userId = (row[3] as? String)?.data(using: .utf8)
- let prfKey = (row[6] as? SQLite.Blob).map { Data($0.bytes) }
- let isDeleted = isDeletedInt64 == 1
+ let query = Self.passkeysTable
+ .filter(Self.colRpId == rpId)
+ .filter(Self.colIsDeleted == 0)
+ .order(Self.colCreatedAt.desc)
- // Get the actual WebAuthn credential ID from AdditionalData column
- let webauthnCredentialId: Data
- if let additionalDataBlob = row[8] as? SQLite.Blob {
- webauthnCredentialId = Data(additionalDataBlob.bytes)
- } else {
- // Fallback: use the passkey ID as a GUID and convert to bytes
- webauthnCredentialId = (try? PasskeyHelper.guidToBytes(idString)) ?? Data()
+ var passkeys: [Passkey] = []
+ for row in try dbConn.prepare(query) {
+ if let passkey = try parsePasskeyRow(row) {
+ passkeys.append(passkey)
}
-
- let passkey = Passkey(
- id: id,
- credentialId: webauthnCredentialId,
- credentialIdString: parentCredentialId.uuidString,
- rpId: rpId,
- userId: userId,
- userName: nil,
- publicKey: publicKeyData,
- privateKey: privateKeyData,
- prfKey: prfKey,
- displayName: displayName,
- createdAt: createdAt,
- updatedAt: updatedAt,
- isDeleted: isDeleted
- )
-
- passkeys.append(passkey)
}
+ print("VaultStore+Passkey: Found \(passkeys.count) passkeys for rpId: \(rpId)")
return passkeys
}
@@ -451,6 +289,97 @@ extension VaultStore {
// MARK: - Helper Methods
+ /**
+ * Parse a passkey row from database query using type-safe column expressions
+ */
+ private func parsePasskeyRow(_ row: Row) throws -> Passkey? {
+ // Extract required fields using column expressions
+ let idString = try row.get(Self.colId)
+ let parentCredentialIdString = try row.get(Self.colCredentialId)
+ let rpId = try row.get(Self.colRpId)
+ let userIdBase64url = try? row.get(Self.colUserId)
+ let publicKeyString = try row.get(Self.colPublicKey)
+ let privateKeyString = try row.get(Self.colPrivateKey)
+ let displayName = try row.get(Self.colDisplayName)
+ let createdAtString = try row.get(Self.colCreatedAt)
+ let updatedAtString = try row.get(Self.colUpdatedAt)
+ let isDeletedInt64 = try row.get(Self.colIsDeleted)
+
+ // Parse UUIDs
+ guard let id = UUID(uuidString: idString),
+ let parentCredentialId = UUID(uuidString: parentCredentialIdString) else {
+ print("VaultStore+Passkey: Invalid UUID in passkey row - id=\(idString), credentialId=\(parentCredentialIdString)")
+ return nil
+ }
+
+ // Parse dates
+ guard let createdAt = parseDateString(createdAtString),
+ let updatedAt = parseDateString(updatedAtString) else {
+ print("VaultStore+Passkey: Invalid date in passkey row - created=\(createdAtString), updated=\(updatedAtString)")
+ return nil
+ }
+
+ // Parse public/private keys
+ guard let publicKeyData = publicKeyString.data(using: .utf8),
+ let privateKeyData = privateKeyString.data(using: .utf8) else {
+ print("VaultStore+Passkey: Invalid key data in passkey row")
+ return nil
+ }
+
+ // Parse userId (stored as base64url-encoded string in DB, decode to raw bytes for iOS)
+ let userId: Data?
+ if let userIdBase64url = userIdBase64url, !userIdBase64url.isEmpty {
+ do {
+ userId = try PasskeyHelper.base64urlToBytes(userIdBase64url)
+ } catch {
+ print("VaultStore+Passkey: Failed to decode userId from base64url: \(error)")
+ userId = nil
+ }
+ } else {
+ userId = nil
+ }
+
+ // Parse PrfKey (stored as blob)
+ let prfKey: Data?
+ if let prfKeyBlob = try? row.get(Self.colPrfKey) {
+ prfKey = Data(prfKeyBlob.bytes)
+ } else {
+ prfKey = nil
+ }
+
+ let isDeleted = isDeletedInt64 == 1
+
+ // Parse WebAuthn credential ID from AdditionalData (stored as blob containing 16-byte GUID)
+ // This is the actual passkey credential ID that iOS expects for matching
+ let webauthnCredentialId: Data
+ if let additionalDataBlob = try? row.get(Self.colAdditionalData) {
+ webauthnCredentialId = Data(additionalDataBlob.bytes)
+ } else {
+ // Fallback: use the passkey record ID as a GUID and convert to bytes
+ do {
+ webauthnCredentialId = try PasskeyHelper.guidToBytes(idString)
+ } catch {
+ print("VaultStore+Passkey: Failed to convert passkey ID to bytes: \(error)")
+ webauthnCredentialId = Data()
+ }
+ }
+
+ return Passkey(
+ id: id,
+ credentialId: webauthnCredentialId,
+ rpId: rpId,
+ userHandle: userId,
+ userName: nil, // userName not stored in DB, derived from parent credential
+ publicKey: publicKeyData,
+ privateKey: privateKeyData,
+ prfKey: prfKey,
+ displayName: displayName,
+ createdAt: createdAt,
+ updatedAt: updatedAt,
+ isDeleted: isDeleted
+ )
+ }
+
/**
* Parse a date string to a Date object for use in queries.
*/