passforios/passAutoFillExtension/Controllers/CredentialProviderViewController.swift

66 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
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]) {
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)
}
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
2020-12-31 21:46:50 -08:00
decryptPassword(in: self, with: passwordEntity) { password in
let username = password.getUsernameForCompletion()
let password = password.password
let passwordCredential = ASPasswordCredential(user: username, password: password)
self.extensionContext.completeRequest(withSelectedCredential: passwordCredential)
}
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: "")
}
}