2021-01-10 13:40:17 -08:00
|
|
|
//
|
|
|
|
|
// CredentialProvider.swift
|
|
|
|
|
// passExtension
|
|
|
|
|
//
|
|
|
|
|
// Created by Sun, Mingshen on 1/9/21.
|
|
|
|
|
// Copyright © 2021 Bob Sun. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import MobileCoreServices
|
|
|
|
|
import passKit
|
2021-01-31 13:17:37 +01:00
|
|
|
import UIKit
|
2021-01-10 13:40:17 -08:00
|
|
|
|
|
|
|
|
class CredentialProvider {
|
2021-09-20 09:50:05 +02:00
|
|
|
private let viewController: UIViewController
|
|
|
|
|
private let extensionContext: NSExtensionContext
|
|
|
|
|
private let afterDecryption: (Password) -> Void
|
2021-01-10 13:40:17 -08:00
|
|
|
|
2021-09-20 09:50:05 +02:00
|
|
|
init(viewController: UIViewController, extensionContext: NSExtensionContext, afterDecryption: @escaping (Password) -> Void) {
|
2021-01-10 13:40:17 -08:00
|
|
|
self.viewController = viewController
|
|
|
|
|
self.extensionContext = extensionContext
|
2021-09-20 09:50:05 +02:00
|
|
|
self.afterDecryption = afterDecryption
|
2021-01-10 13:40:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func provideCredentialsFindLogin(with passwordPath: String) {
|
|
|
|
|
decryptPassword(in: viewController, with: passwordPath) { password in
|
|
|
|
|
let extensionItem = NSExtensionItem()
|
|
|
|
|
var returnDictionary = [
|
|
|
|
|
PassExtensionKey.usernameKey: password.getUsernameForCompletion(),
|
|
|
|
|
PassExtensionKey.passwordKey: password.password,
|
|
|
|
|
]
|
|
|
|
|
if let totpPassword = password.currentOtp {
|
|
|
|
|
returnDictionary[PassExtensionKey.totpKey] = totpPassword
|
|
|
|
|
}
|
|
|
|
|
extensionItem.attachments = [NSItemProvider(item: returnDictionary as NSSecureCoding, typeIdentifier: String(kUTTypePropertyList))]
|
2021-09-20 09:50:05 +02:00
|
|
|
self.extensionContext.completeRequest(returningItems: [extensionItem])
|
|
|
|
|
self.afterDecryption(password)
|
2021-01-10 13:40:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func provideCredentialsBrowser(with passwordPath: String) {
|
|
|
|
|
decryptPassword(in: viewController, with: passwordPath) { password in
|
|
|
|
|
Utils.copyToPasteboard(textToCopy: password.password)
|
|
|
|
|
// return a dictionary for JavaScript for best-effor fill in
|
|
|
|
|
let extensionItem = NSExtensionItem()
|
|
|
|
|
let returnDictionary = [
|
|
|
|
|
NSExtensionJavaScriptFinalizeArgumentKey: [
|
2021-09-20 09:50:05 +02:00
|
|
|
PassExtensionKey.usernameKey: password.getUsernameForCompletion(),
|
|
|
|
|
PassExtensionKey.passwordKey: password.password,
|
2021-01-10 13:40:17 -08:00
|
|
|
],
|
|
|
|
|
]
|
|
|
|
|
extensionItem.attachments = [NSItemProvider(item: returnDictionary as NSSecureCoding, typeIdentifier: String(kUTTypePropertyList))]
|
2021-09-20 09:50:05 +02:00
|
|
|
self.extensionContext.completeRequest(returningItems: [extensionItem])
|
|
|
|
|
self.afterDecryption(password)
|
2021-01-10 13:40:17 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|