2026-09-19 15:33:23 +02:00
|
|
|
//
|
|
|
|
|
// CredentialProviderViewController.swift
|
2026-09-19 15:37:31 +02:00
|
|
|
// AutoFill
|
2026-09-19 15:33:23 +02:00
|
|
|
//
|
|
|
|
|
// Created by Christophe Vila on 19/09/2026.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import AuthenticationServices
|
2026-09-19 19:08:54 +02:00
|
|
|
import SwiftUI
|
|
|
|
|
import MyPassCore
|
2026-09-19 15:33:23 +02:00
|
|
|
|
2026-09-19 19:08:54 +02:00
|
|
|
final class CredentialProviderViewController: ASCredentialProviderViewController {
|
2026-09-19 15:33:23 +02:00
|
|
|
|
2026-09-19 19:08:54 +02:00
|
|
|
private let session = VaultSession()
|
|
|
|
|
private let bookmarkService = FileBookmarkService()
|
|
|
|
|
private let keychainStore = KeychainStore(accessGroup: "org.antiloop222.mypass")
|
|
|
|
|
private let biometricService = BiometricAuthService()
|
|
|
|
|
|
|
|
|
|
// Called when the user selects MyPass from the QuickType bar.
|
2026-09-19 15:33:23 +02:00
|
|
|
override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
|
2026-09-19 19:08:54 +02:00
|
|
|
let ids = serviceIdentifiers.map(\.identifier)
|
|
|
|
|
showUI(serviceIdentifiers: ids)
|
2026-09-19 15:33:23 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 19:08:54 +02:00
|
|
|
// Called for inline QuickType suggestion (no UI shown).
|
2026-09-19 15:33:23 +02:00
|
|
|
override func provideCredentialWithoutUserInteraction(for credentialIdentity: ASPasswordCredentialIdentity) {
|
2026-09-19 19:08:54 +02:00
|
|
|
Task {
|
|
|
|
|
do {
|
|
|
|
|
try await unlockSilently()
|
|
|
|
|
let all = session.allEntries()
|
|
|
|
|
if let entry = all.first(where: { $0.id.uuidString == credentialIdentity.recordIdentifier }) {
|
|
|
|
|
let credential = ASPasswordCredential(user: entry.username, password: entry.password.reveal())
|
|
|
|
|
self.extensionContext.completeRequest(withSelectedCredential: credential, completionHandler: nil)
|
|
|
|
|
} else {
|
|
|
|
|
self.extensionContext.cancelRequest(withError: ASExtensionError(.credentialIdentityNotFound))
|
|
|
|
|
}
|
|
|
|
|
} catch {
|
|
|
|
|
self.extensionContext.cancelRequest(withError: ASExtensionError(.userInteractionRequired))
|
|
|
|
|
}
|
2026-09-19 15:33:23 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-19 19:08:54 +02:00
|
|
|
private func unlockSilently() async throws {
|
|
|
|
|
guard session.isLocked else { return }
|
|
|
|
|
let password = try keychainStore.load(for: "masterPassword")
|
|
|
|
|
let url = try bookmarkService.resolveURL()
|
|
|
|
|
defer { bookmarkService.stopAccess(url: url) }
|
|
|
|
|
try session.unlock(url: url, password: password)
|
2026-09-19 15:33:23 +02:00
|
|
|
}
|
|
|
|
|
|
2026-09-19 19:08:54 +02:00
|
|
|
private func showUI(serviceIdentifiers: [String]) {
|
|
|
|
|
let rootView = ExtensionRootView(
|
|
|
|
|
session: session,
|
|
|
|
|
serviceIdentifiers: serviceIdentifiers,
|
|
|
|
|
bookmarkService: bookmarkService,
|
|
|
|
|
keychainStore: keychainStore,
|
|
|
|
|
biometricService: biometricService,
|
|
|
|
|
onSelect: { [weak self] (entry: Entry) in
|
|
|
|
|
let credential = ASPasswordCredential(
|
|
|
|
|
user: entry.username,
|
|
|
|
|
password: entry.password.reveal()
|
|
|
|
|
)
|
|
|
|
|
self?.extensionContext.completeRequest(withSelectedCredential: credential, completionHandler: nil)
|
|
|
|
|
},
|
|
|
|
|
onCancel: { [weak self] in
|
|
|
|
|
self?.extensionContext.cancelRequest(withError: ASExtensionError(.userCanceled))
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
let host = UIHostingController(rootView: rootView)
|
|
|
|
|
addChild(host)
|
|
|
|
|
view.addSubview(host.view)
|
|
|
|
|
host.view.frame = view.bounds
|
|
|
|
|
host.view.autoresizingMask = [UIView.AutoresizingMask.flexibleWidth, UIView.AutoresizingMask.flexibleHeight]
|
|
|
|
|
host.didMove(toParent: self)
|
2026-09-19 15:33:23 +02:00
|
|
|
}
|
|
|
|
|
}
|