2018-09-24 15:06:43 +08:00
|
|
|
//
|
|
|
|
|
// CredentialProviderViewController.swift
|
|
|
|
|
// passAutoFillExtension
|
|
|
|
|
//
|
|
|
|
|
// Created by Yishi Lin on 2018/9/24.
|
|
|
|
|
// Copyright © 2018 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import AuthenticationServices
|
|
|
|
|
import passKit
|
|
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
class CredentialProviderViewController: ASCredentialProviderViewController {
|
2021-08-27 23:34:30 +02:00
|
|
|
private lazy var passcodelock: PasscodeExtensionDisplay = { [unowned self] in
|
2021-01-31 13:17:37 +01:00
|
|
|
PasscodeExtensionDisplay(extensionContext: extensionContext)
|
2021-08-27 23:34:30 +02:00
|
|
|
}()
|
2018-09-24 15:06:43 +08:00
|
|
|
|
2021-12-28 02:57:11 +01:00
|
|
|
private lazy var passwordsViewController: PasswordsViewController = (children.first as! UINavigationController).viewControllers.first as! PasswordsViewController
|
2018-09-24 15:06:43 +08:00
|
|
|
|
2021-08-27 23:34:30 +02:00
|
|
|
private lazy var credentialProvider: CredentialProvider = { [unowned self] in
|
2021-10-01 19:32:14 +02:00
|
|
|
CredentialProvider(viewController: self, extensionContext: extensionContext, afterDecryption: NotificationCenterDispatcher.showOTPNotification)
|
2021-08-27 23:34:30 +02:00
|
|
|
}()
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2021-08-27 23:34:30 +02:00
|
|
|
private lazy var passwordsTableEntries = PasswordStore.shared.fetchPasswordEntityCoreData(withDir: false)
|
2021-10-03 05:46:07 +02:00
|
|
|
.map(PasswordTableEntry.init)
|
2021-01-03 15:08:15 -08:00
|
|
|
|
2018-09-24 22:03:14 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2021-08-27 23:34:30 +02:00
|
|
|
passwordsViewController.dataSource = PasswordsTableDataSource(entries: passwordsTableEntries)
|
2020-12-31 21:46:50 -08:00
|
|
|
passwordsViewController.selectionDelegate = self
|
2021-08-27 23:34:30 +02:00
|
|
|
passwordsViewController.navigationItem.leftBarButtonItem = UIBarButtonItem(
|
|
|
|
|
barButtonSystemItem: .cancel,
|
|
|
|
|
target: self,
|
|
|
|
|
action: #selector(cancel)
|
|
|
|
|
)
|
2018-09-24 22:03:14 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
|
2021-08-27 23:34:30 +02:00
|
|
|
passcodelock.presentPasscodeLockIfNeeded(self) {
|
|
|
|
|
self.view.isHidden = true
|
|
|
|
|
} after: { [unowned self] in
|
2023-03-10 06:33:19 +01:00
|
|
|
view.isHidden = false
|
|
|
|
|
credentialProvider.identifier = serviceIdentifiers.first
|
2021-08-27 23:34:30 +02:00
|
|
|
let url = serviceIdentifiers.first
|
|
|
|
|
.map(\.identifier)
|
2021-10-03 05:46:07 +02:00
|
|
|
.flatMap(URL.init)
|
2023-03-10 06:33:19 +01:00
|
|
|
passwordsViewController.navigationItem.prompt = url?.host
|
|
|
|
|
passwordsViewController.showPasswordsWithSuggestion(matching: url?.host ?? "")
|
2021-08-27 23:34:30 +02:00
|
|
|
}
|
2018-09-24 22:03:14 +08:00
|
|
|
}
|
2021-01-03 15:08:15 -08:00
|
|
|
|
|
|
|
|
override func provideCredentialWithoutUserInteraction(for credentialIdentity: ASPasswordCredentialIdentity) {
|
2021-01-03 17:38:02 -08:00
|
|
|
credentialProvider.identifier = credentialIdentity.serviceIdentifier
|
|
|
|
|
if !PasscodeLock.shared.hasPasscode, Defaults.isRememberPGPPassphraseOn {
|
|
|
|
|
credentialProvider.credentials(for: credentialIdentity)
|
|
|
|
|
} else {
|
|
|
|
|
extensionContext.cancelRequest(withError: NSError(domain: ASExtensionErrorDomain, code: ASExtensionError.userInteractionRequired.rawValue))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func prepareInterfaceToProvideCredential(for credentialIdentity: ASPasswordCredentialIdentity) {
|
2021-08-27 23:36:50 +02:00
|
|
|
passcodelock.presentPasscodeLockIfNeeded(self) {
|
|
|
|
|
self.view.isHidden = true
|
|
|
|
|
} after: { [unowned self] in
|
2023-03-10 06:33:19 +01:00
|
|
|
credentialProvider.credentials(for: credentialIdentity)
|
2021-01-03 17:38:02 -08:00
|
|
|
}
|
2021-01-03 15:08:15 -08:00
|
|
|
}
|
2021-01-10 13:40:17 -08:00
|
|
|
|
|
|
|
|
@objc
|
|
|
|
|
private func cancel(_: AnyObject?) {
|
2021-01-31 13:17:37 +01:00
|
|
|
extensionContext.cancelRequest(withError: NSError(domain: "PassExtension", code: 0))
|
2021-01-10 13:40:17 -08:00
|
|
|
}
|
2020-12-31 21:46:50 -08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
extension CredentialProviderViewController: PasswordSelectionDelegate {
|
|
|
|
|
func selected(password: PasswordTableEntry) {
|
2021-08-27 23:34:30 +02:00
|
|
|
credentialProvider.persistAndProvideCredentials(with: password.passwordEntity.getPath())
|
2018-09-24 15:06:43 +08:00
|
|
|
}
|
|
|
|
|
}
|