Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Refactor iOS credential matching to use shared method with UI logic (#1120)
Leendert de Borst229fbd48244a
diff --git a/apps/mobile-app/ios/VaultUI/Utils/CredentialFilter.swift b/apps/mobile-app/ios/VaultUI/Utils/CredentialFilter.swiftnew file mode 100644index 000000000..c4bf6aaab--- /dev/null+++ b/apps/mobile-app/ios/VaultUI/Utils/CredentialFilter.swift@@ -0,0 +1,110 @@+import Foundation+import VaultModels++/// Utility class for filtering credentials based on search text.+/// This class contains the core filtering logic used by both UI and tests for consistency.+public class CredentialFilter {++ /// Extract domain from URL, handling both full URLs and partial domains.+ /// - Parameter urlString: URL or domain string+ /// - Returns: Normalized domain without protocol or www+ private static func extractDomain(from urlString: String) -> String {+ var domain = urlString.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)+ // Remove protocol if present+ domain = domain.replacingOccurrences(of: "https://", with: "")+ domain = domain.replacingOccurrences(of: "http://", with: "")+ // Remove www. prefix+ domain = domain.replacingOccurrences(of: "www.", with: "")+ // Remove path, query, and fragment+ if let firstSlash = domain.firstIndex(of: "/") {+ domain = String(domain[..<firstSlash])+ }+ if let firstQuestion = domain.firstIndex(of: "?") {+ domain = String(domain[..<firstQuestion])+ }+ if let firstHash = domain.firstIndex(of: "#") {+ domain = String(domain[..<firstHash])+ }+ return domain+ }++ /// Extract root domain from a domain string.+ /// - Parameter domain: Domain string+ /// - Returns: Root domain (e.g., "sub.example.com" -> "example.com")+ private static func extractRootDomain(from domain: String) -> String {+ let parts = domain.components(separatedBy: ".")+ return parts.count >= 2 ? parts.suffix(2).joined(separator: ".") : domain+ }++ /// Check if two domains match, supporting partial matches.+ /// - Parameters:+ /// - domain1: First domain+ /// - domain2: Second domain+ /// - Returns: True if domains match (including partial matches)+ private static func domainsMatch(_ domain1: String, _ domain2: String) -> Bool {+ let d1 = extractDomain(from: domain1)+ let d2 = extractDomain(from: domain2)++ // Exact match+ if d1 == d2 { return true }++ // Check if one domain contains the other (for subdomain matching)+ if d1.contains(d2) || d2.contains(d1) { return true }++ // Check root domain match+ let d1Root = extractRootDomain(from: d1)+ let d2Root = extractRootDomain(from: d2)++ return d1Root == d2Root+ }++ /// Filter credentials based on search text. This matches the iOS filterCredentials logic exactly.+ /// - Parameters:+ /// - credentials: List of credentials to filter+ /// - searchText: Search term (app info, URL, etc.)+ /// - Returns: Filtered list of credentials+ public static func filterCredentials(_ credentials: [Credential], searchText: String) -> [Credential] {+ if searchText.isEmpty {+ return credentials+ }++ // Try to parse as URL first+ let searchDomain = extractDomain(from: searchText)++ if !searchDomain.isEmpty {+ var matches: Set<Credential> = []++ // Check for domain matches with priority+ credentials.forEach { credential in+ guard let serviceUrl = credential.service.url, !serviceUrl.isEmpty else { return }++ if domainsMatch(searchText, serviceUrl) {+ matches.insert(credential)+ }+ }++ // If no domain matches found, try text search in service name+ if matches.isEmpty {+ let domainParts = searchDomain.components(separatedBy: ".")+ let domainWithoutExtension = domainParts.first?.lowercased() ?? searchDomain.lowercased()++ let nameMatches = credentials.filter { credential in+ let serviceNameMatch = credential.service.name?.lowercased().contains(domainWithoutExtension) ?? false+ let notesMatch = credential.notes?.lowercased().contains(domainWithoutExtension) ?? false+ return serviceNameMatch || notesMatch+ }+ matches.formUnion(nameMatches)+ }++ return Array(matches)+ } else {+ // Non-URL fallback: simple text search in service name, username, or notes+ let lowercasedSearch = searchText.lowercased()+ return credentials.filter { credential in+ (credential.service.name?.lowercased().contains(lowercasedSearch) ?? false) ||+ (credential.username?.lowercased().contains(lowercasedSearch) ?? false) ||+ (credential.notes?.lowercased().contains(lowercasedSearch) ?? false)+ }+ }+ }+}\ No newline at end of filediff --git a/apps/mobile-app/ios/VaultUI/Views/CredentialProviderView.swift b/apps/mobile-app/ios/VaultUI/Views/CredentialProviderView.swiftindex 0a48738d2..224816b0b 100644--- a/apps/mobile-app/ios/VaultUI/Views/CredentialProviderView.swift+++ b/apps/mobile-app/ios/VaultUI/Views/CredentialProviderView.swift@@ -247,91 +247,7 @@ public class CredentialProviderViewModel: ObservableObject {}func filterCredentials() {- if searchText.isEmpty {- filteredCredentials = credentials- return- }-- func extractDomain(from urlString: String) -> String {- var domain = urlString.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)- // Remove protocol if present- domain = domain.replacingOccurrences(of: "https://", with: "")- domain = domain.replacingOccurrences(of: "http://", with: "")- // Remove www. prefix- domain = domain.replacingOccurrences(of: "www.", with: "")- // Remove path, query, and fragment- if let firstSlash = domain.firstIndex(of: "/") {- domain = String(domain[..<firstSlash])- }- if let firstQuestion = domain.firstIndex(of: "?") {- domain = String(domain[..<firstQuestion])- }- if let firstHash = domain.firstIndex(of: "#") {- domain = String(domain[..<firstHash])- }- return domain- }-- func extractRootDomain(from domain: String) -> String {- let parts = domain.components(separatedBy: ".")- return parts.count >= 2 ? parts.suffix(2).joined(separator: ".") : domain- }-- func domainsMatch(_ domain1: String, _ domain2: String) -> Bool {- let d1 = extractDomain(from: domain1)- let d2 = extractDomain(from: domain2)-- // Exact match- if d1 == d2 { return true }-- // Check if one domain contains the other (for subdomain matching)- if d1.contains(d2) || d2.contains(d1) { return true }-- // Check root domain match- let d1Root = extractRootDomain(from: d1)- let d2Root = extractRootDomain(from: d2)-- return d1Root == d2Root- }-- // Try to parse as URL first- let searchDomain = extractDomain(from: searchText)-- if !searchDomain.isEmpty {- var matches: Set<Credential> = []-- // Check for domain matches with priority- credentials.forEach { credential in- guard let serviceUrl = credential.service.url, !serviceUrl.isEmpty else { return }-- if domainsMatch(searchText, serviceUrl) {- matches.insert(credential)- }- }-- // If no domain matches found, try text search in service name- if matches.isEmpty {- let domainParts = searchDomain.components(separatedBy: ".")- let domainWithoutExtension = domainParts.first?.lowercased() ?? searchDomain.lowercased()-- let nameMatches = credentials.filter { credential in- let serviceNameMatch = credential.service.name?.lowercased().contains(domainWithoutExtension) ?? false- let notesMatch = credential.notes?.lowercased().contains(domainWithoutExtension) ?? false- return serviceNameMatch || notesMatch- }- matches.formUnion(nameMatches)- }-- filteredCredentials = Array(matches)- } else {- // Non-URL fallback: simple text search in service name, username, or notes- let lowercasedSearch = searchText.lowercased()- filteredCredentials = credentials.filter { credential in- (credential.service.name?.lowercased().contains(lowercasedSearch) ?? false) ||- (credential.username?.lowercased().contains(lowercasedSearch) ?? false) ||- (credential.notes?.lowercased().contains(lowercasedSearch) ?? false)- }- }+ filteredCredentials = CredentialFilter.filterCredentials(credentials, searchText: searchText)}diff --git a/apps/mobile-app/ios/VaultUITests/CredentialFilterTests.swift b/apps/mobile-app/ios/VaultUITests/CredentialFilterTests.swiftindex 73fdee79c..1b5c224a9 100644--- a/apps/mobile-app/ios/VaultUITests/CredentialFilterTests.swift+++ b/apps/mobile-app/ios/VaultUITests/CredentialFilterTests.swift@@ -19,7 +19,7 @@ final class CredentialFilterTests: XCTestCase {// [#1] - Exact URL matchfunc testExactUrlMatch() {- let matches = filterCredentials(testCredentials, searchText: "www.coolblue.nl")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "www.coolblue.nl")XCTAssertEqual(matches.count, 1)XCTAssertEqual(matches.first?.service.name, "Coolblue")@@ -27,7 +27,7 @@ final class CredentialFilterTests: XCTestCase {// [#2] - Base URL with path matchfunc testBaseUrlMatch() {- let matches = filterCredentials(testCredentials, searchText: "https://gmail.com/signin")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://gmail.com/signin")XCTAssertEqual(matches.count, 1)XCTAssertEqual(matches.first?.service.name, "Gmail")@@ -35,7 +35,7 @@ final class CredentialFilterTests: XCTestCase {// [#3] - Root domain with subdomain matchfunc testRootDomainMatch() {- let matches = filterCredentials(testCredentials, searchText: "https://mail.google.com")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://mail.google.com")XCTAssertEqual(matches.count, 1)XCTAssertEqual(matches.first?.service.name, "Google")@@ -43,7 +43,7 @@ final class CredentialFilterTests: XCTestCase {// [#4] - No matches for non-existent domainfunc testNoMatches() {- let matches = filterCredentials(testCredentials, searchText: "https://nonexistent.com")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://nonexistent.com")XCTAssertTrue(matches.isEmpty)}@@ -52,7 +52,7 @@ final class CredentialFilterTests: XCTestCase {// [#5] - Partial URL stored matches full URL searchfunc testPartialUrlMatchWithFullUrl() {// Test case: stored URL is "dumpert.nl", search with full URL- let matches = filterCredentials(testCredentials, searchText: "https://www.dumpert.nl")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://www.dumpert.nl")XCTAssertEqual(matches.count, 1)XCTAssertEqual(matches.first?.service.name, "Dumpert")@@ -61,7 +61,7 @@ final class CredentialFilterTests: XCTestCase {// [#6] - Full URL stored matches partial URL searchfunc testFullUrlMatchWithPartialUrl() {// Test case: stored URL is full, search with partial- let matches = filterCredentials(testCredentials, searchText: "coolblue.nl")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "coolblue.nl")XCTAssertEqual(matches.count, 1)XCTAssertTrue(matches.contains { $0.service.name == "Coolblue" })@@ -70,9 +70,9 @@ final class CredentialFilterTests: XCTestCase {// [#7] - Protocol variations (http/https/none) matchfunc testProtocolVariations() {// Test that http and https variations match- let httpsMatches = filterCredentials(testCredentials, searchText: "https://github.com")- let httpMatches = filterCredentials(testCredentials, searchText: "http://github.com")- let noProtocolMatches = filterCredentials(testCredentials, searchText: "github.com")+ let httpsMatches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://github.com")+ let httpMatches = CredentialFilter.filterCredentials(testCredentials, searchText: "http://github.com")+ let noProtocolMatches = CredentialFilter.filterCredentials(testCredentials, searchText: "github.com")XCTAssertEqual(httpsMatches.count, 1)XCTAssertEqual(httpMatches.count, 1)@@ -85,8 +85,8 @@ final class CredentialFilterTests: XCTestCase {// [#8] - WWW prefix variations matchfunc testWwwVariations() {// Test that www variations match- let withWww = filterCredentials(testCredentials, searchText: "www.dumpert.nl")- let withoutWww = filterCredentials(testCredentials, searchText: "dumpert.nl")+ let withWww = CredentialFilter.filterCredentials(testCredentials, searchText: "www.dumpert.nl")+ let withoutWww = CredentialFilter.filterCredentials(testCredentials, searchText: "dumpert.nl")XCTAssertEqual(withWww.count, 1)XCTAssertEqual(withoutWww.count, 1)@@ -97,9 +97,9 @@ final class CredentialFilterTests: XCTestCase {// [#9] - Subdomain matchingfunc testSubdomainMatching() {// Test subdomain matching- let appSubdomain = filterCredentials(testCredentials, searchText: "https://app.example.com")- let wwwSubdomain = filterCredentials(testCredentials, searchText: "https://www.example.com")- let noSubdomain = filterCredentials(testCredentials, searchText: "https://example.com")+ let appSubdomain = CredentialFilter.filterCredentials(testCredentials, searchText: "https://app.example.com")+ let wwwSubdomain = CredentialFilter.filterCredentials(testCredentials, searchText: "https://www.example.com")+ let noSubdomain = CredentialFilter.filterCredentials(testCredentials, searchText: "https://example.com")XCTAssertEqual(appSubdomain.count, 1)XCTAssertEqual(appSubdomain.first?.service.name, "Subdomain Example")@@ -112,9 +112,9 @@ final class CredentialFilterTests: XCTestCase {// [#10] - Paths and query strings ignoredfunc testPathAndQueryIgnored() {// Test that paths and query strings are ignored- let withPath = filterCredentials(testCredentials, searchText: "https://github.com/user/repo")- let withQuery = filterCredentials(testCredentials, searchText: "https://stackoverflow.com/questions?tab=newest")- let withFragment = filterCredentials(testCredentials, searchText: "https://gmail.com#inbox")+ let withPath = CredentialFilter.filterCredentials(testCredentials, searchText: "https://github.com/user/repo")+ let withQuery = CredentialFilter.filterCredentials(testCredentials, searchText: "https://stackoverflow.com/questions?tab=newest")+ let withFragment = CredentialFilter.filterCredentials(testCredentials, searchText: "https://gmail.com#inbox")XCTAssertEqual(withPath.count, 1)XCTAssertEqual(withPath.first?.service.name, "GitHub")@@ -127,7 +127,7 @@ final class CredentialFilterTests: XCTestCase {// [#11] - Complex URL variationsfunc testComplexUrlVariations() {// Test complex URL matching scenario- let matches = filterCredentials(testCredentials, searchText: "https://www.coolblue.nl/product/12345?ref=google")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "https://www.coolblue.nl/product/12345?ref=google")XCTAssertEqual(matches.count, 1)XCTAssertTrue(matches.contains { $0.service.name == "Coolblue" })@@ -136,7 +136,7 @@ final class CredentialFilterTests: XCTestCase {// [#13] - Title-only matchingfunc testTitleOnlyMatching() {// Test service name matching when no URL is available- let matches = filterCredentials(testCredentials, searchText: "newyorktimes")+ let matches = CredentialFilter.filterCredentials(testCredentials, searchText: "newyorktimes")XCTAssertEqual(matches.count, 1)XCTAssertEqual(matches.first?.service.name, "Title Only newyorktimes")@@ -200,97 +200,3 @@ final class CredentialFilterTests: XCTestCase {)}}--// MARK: - Standalone Filtering Logic--/**- * Standalone filtering function extracted from CredentialProviderViewModel.- * This allows testing the filtering logic in isolation without needing to instantiate the view model.- * Based on the filterCredentials() method in CredentialProviderView.swift- */-func filterCredentials(_ credentials: [Credential], searchText: String) -> [Credential] {- if searchText.isEmpty {- return credentials- }-- func extractDomain(from urlString: String) -> String {- var domain = urlString.lowercased().trimmingCharacters(in: .whitespacesAndNewlines)- // Remove protocol if present- domain = domain.replacingOccurrences(of: "https://", with: "")- domain = domain.replacingOccurrences(of: "http://", with: "")- // Remove www. prefix- domain = domain.replacingOccurrences(of: "www.", with: "")- // Remove path, query, and fragment- if let firstSlash = domain.firstIndex(of: "/") {- domain = String(domain[..<firstSlash])- }- if let firstQuestion = domain.firstIndex(of: "?") {- domain = String(domain[..<firstQuestion])- }- if let firstHash = domain.firstIndex(of: "#") {- domain = String(domain[..<firstHash])- }- return domain- }-- func extractRootDomain(from domain: String) -> String {- let parts = domain.components(separatedBy: ".")- return parts.count >= 2 ? parts.suffix(2).joined(separator: ".") : domain- }-- func domainsMatch(_ domain1: String, _ domain2: String) -> Bool {- let d1 = extractDomain(from: domain1)- let d2 = extractDomain(from: domain2)-- // Exact match- if d1 == d2 { return true }-- // Check if one domain contains the other (for subdomain matching)- if d1.contains(d2) || d2.contains(d1) { return true }-- // Check root domain match- let d1Root = extractRootDomain(from: d1)- let d2Root = extractRootDomain(from: d2)-- return d1Root == d2Root- }-- // Try to parse as URL first- let searchDomain = extractDomain(from: searchText)-- if !searchDomain.isEmpty {- var matches: Set<Credential> = []-- // Check for domain matches with priority- credentials.forEach { credential in- guard let serviceUrl = credential.service.url, !serviceUrl.isEmpty else { return }-- if domainsMatch(searchText, serviceUrl) {- matches.insert(credential)- }- }-- // If no domain matches found, try text search in service name- if matches.isEmpty {- let domainParts = searchDomain.components(separatedBy: ".")- let domainWithoutExtension = domainParts.first?.lowercased() ?? searchDomain.lowercased()-- let nameMatches = credentials.filter { credential in- let serviceNameMatch = credential.service.name?.lowercased().contains(domainWithoutExtension) ?? false- let notesMatch = credential.notes?.lowercased().contains(domainWithoutExtension) ?? false- return serviceNameMatch || notesMatch- }- matches.formUnion(nameMatches)- }-- return Array(matches)- } else {- // Non-URL fallback: simple text search in service name, username, or notes- let lowercasedSearch = searchText.lowercased()- return credentials.filter { credential in- (credential.service.name?.lowercased().contains(lowercasedSearch) ?? false) ||- (credential.username?.lowercased().contains(lowercasedSearch) ?? false) ||- (credential.notes?.lowercased().contains(lowercasedSearch) ?? false)- }- }-}