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 {
|
|
|
|
|
var passcodelock: PasscodeExtensionDisplay {
|
|
|
|
|
PasscodeExtensionDisplay(extensionContext: self.extensionContext)
|
2018-09-24 15:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
var embeddedNavigationController: UINavigationController {
|
|
|
|
|
children.first as! UINavigationController
|
2018-09-24 15:06:43 +08:00
|
|
|
}
|
|
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
var passwordsViewController: PasswordsViewController {
|
|
|
|
|
embeddedNavigationController.viewControllers.first as! PasswordsViewController
|
2018-09-24 22:03:14 +08:00
|
|
|
}
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2021-01-03 15:08:15 -08:00
|
|
|
lazy var credentialProvider = CredentialProvider(viewController: self, extensionContext: self.extensionContext)
|
|
|
|
|
|
2018-09-24 22:03:14 +08:00
|
|
|
override func viewDidLoad() {
|
|
|
|
|
super.viewDidLoad()
|
2020-12-31 21:46:50 -08:00
|
|
|
passcodelock.presentPasscodeLockIfNeeded(self)
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2020-12-31 21:46:50 -08:00
|
|
|
let passwordsTableEntries = PasswordStore.shared.fetchPasswordEntityCoreData(withDir: false).compactMap { PasswordTableEntry($0) }
|
|
|
|
|
let dataSource = PasswordsTableDataSource(entries: passwordsTableEntries)
|
|
|
|
|
passwordsViewController.dataSource = dataSource
|
|
|
|
|
passwordsViewController.selectionDelegate = self
|
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-01-03 15:08:15 -08:00
|
|
|
credentialProvider.identifier = serviceIdentifiers.first
|
2020-12-31 21:46:50 -08:00
|
|
|
let url = serviceIdentifiers.first.flatMap { URL(string: $0.identifier) }
|
|
|
|
|
passwordsViewController.navigationItem.prompt = url?.host
|
2021-01-02 22:13:48 -08:00
|
|
|
let keywords = url?.host?.sanitizedDomain?.components(separatedBy: ".") ?? []
|
|
|
|
|
passwordsViewController.showPasswordsWithSuggstion(keywords)
|
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) {
|
|
|
|
|
guard let identifier = credentialIdentity.recordIdentifier else {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
credentialProvider.identifier = credentialIdentity.serviceIdentifier
|
|
|
|
|
passwordsViewController.navigationItem.prompt = identifier
|
|
|
|
|
passwordsViewController.showPasswordsWithSuggstion([identifier])
|
2021-01-03 15:08:15 -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) {
|
|
|
|
|
let passwordEntity = password.passwordEntity
|
2018-12-09 16:59:07 -08:00
|
|
|
|
2021-01-03 15:08:15 -08:00
|
|
|
credentialProvider.persistAndProvideCredentials(with: passwordEntity.getPath())
|
2018-09-24 15:06:43 +08:00
|
|
|
}
|
|
|
|
|
}
|
2021-01-02 22:13:48 -08:00
|
|
|
|
|
|
|
|
private extension String {
|
|
|
|
|
var sanitizedDomain: String? {
|
|
|
|
|
replacingOccurrences(of: ".com", with: "")
|
|
|
|
|
.replacingOccurrences(of: ".org", with: "")
|
|
|
|
|
.replacingOccurrences(of: ".edu", with: "")
|
|
|
|
|
.replacingOccurrences(of: ".net", with: "")
|
|
|
|
|
.replacingOccurrences(of: ".gov", with: "")
|
|
|
|
|
.replacingOccurrences(of: "www.", with: "")
|
|
|
|
|
}
|
|
|
|
|
}
|