passforios/passAutoFillExtension/Controllers/CredentialProviderViewController.swift

68 lines
2.5 KiB
Swift
Raw Normal View History

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-12-09 16:59:07 -08:00
lazy var credentialProvider = CredentialProvider(viewController: self, extensionContext: self.extensionContext)
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-12-09 16:59:07 -08:00
2020-12-31 21:46:50 -08:00
override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
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)
}
override func provideCredentialWithoutUserInteraction(for credentialIdentity: ASPasswordCredentialIdentity) {
credentialProvider.credentials(for: credentialIdentity)
}
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
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: "")
}
}