mirror of
https://github.com/bitwarden/ios.git
synced 2025-12-13 02:58:59 -06:00
102 lines
3.9 KiB
Swift
102 lines
3.9 KiB
Swift
import AuthenticationServices
|
|
import BitwardenShared
|
|
|
|
/// An `ASCredentialProviderViewController` that implements credential autofill.
|
|
///
|
|
class CredentialProviderViewController: ASCredentialProviderViewController {
|
|
// MARK: Properties
|
|
|
|
/// The processor that manages application level logic.
|
|
private var appProcessor: AppProcessor?
|
|
|
|
// MARK: ASCredentialProviderViewController
|
|
|
|
override func prepareCredentialList(for serviceIdentifiers: [ASCredentialServiceIdentifier]) {
|
|
initializeApp()
|
|
}
|
|
|
|
// Implement this method if your extension supports showing credentials in the QuickType bar.
|
|
// When the user selects a credential from your app, this method will be called with the
|
|
// ASPasswordCredentialIdentity your app has previously saved to the ASCredentialIdentityStore.
|
|
// Provide the password by completing the extension request with the associated ASPasswordCredential.
|
|
// If using the credential would require showing custom UI for authenticating the user, cancel
|
|
// the request with error code ASExtensionError.userInteractionRequired.
|
|
// override func provideCredentialWithoutUserInteraction(for credentialIdentity: ASPasswordCredentialIdentity) {
|
|
// let databaseIsUnlocked = true
|
|
// if (databaseIsUnlocked) {
|
|
// let passwordCredential = ASPasswordCredential(user: "j_appleseed", password: "apple1234")
|
|
// self.extensionContext.completeRequest(withSelectedCredential: passwordCredential, completionHandler: nil)
|
|
// } else {
|
|
// self.extensionContext.cancelRequest(
|
|
// withError: NSError(
|
|
// domain: ASExtensionErrorDomain,
|
|
// code: ASExtensionError.userInteractionRequired.rawValue
|
|
// )
|
|
// )
|
|
// }
|
|
// }
|
|
|
|
// Implement this method if provideCredentialWithoutUserInteraction(for:) can fail with
|
|
// ASExtensionError.userInteractionRequired. In this case, the system may present your extension's
|
|
// UI and call this method. Show appropriate UI for authenticating the user then provide the password
|
|
// by completing the extension request with the associated ASPasswordCredential.
|
|
//
|
|
// override func prepareInterfaceToProvideCredential(for credentialIdentity: ASPasswordCredentialIdentity) {
|
|
// }
|
|
|
|
// MARK: Private
|
|
|
|
/// Cancels the extension request and dismisses the extension's view controller.
|
|
///
|
|
private func cancel() {
|
|
extensionContext.cancelRequest(
|
|
withError: NSError(
|
|
domain: ASExtensionErrorDomain,
|
|
code: ASExtensionError.userCanceled.rawValue
|
|
)
|
|
)
|
|
}
|
|
|
|
/// Sets up and initializes the app and UI.
|
|
///
|
|
private func initializeApp() {
|
|
let errorReporter = OSLogErrorReporter()
|
|
let services = ServiceContainer(errorReporter: errorReporter)
|
|
let appModule = DefaultAppModule(appExtensionDelegate: self, services: services)
|
|
let appProcessor = AppProcessor(appModule: appModule, services: services)
|
|
self.appProcessor = appProcessor
|
|
|
|
appProcessor.start(appContext: .appExtension, navigator: self)
|
|
}
|
|
}
|
|
|
|
// MARK: - AppExtensionDelegate
|
|
|
|
extension CredentialProviderViewController: AppExtensionDelegate {
|
|
var isInAppExtension: Bool { true }
|
|
|
|
func didCancel() {
|
|
cancel()
|
|
}
|
|
}
|
|
|
|
// MARK: - RootNavigator
|
|
|
|
extension CredentialProviderViewController: RootNavigator {
|
|
var rootViewController: UIViewController? { self }
|
|
|
|
func show(child: Navigator) {
|
|
if let fromViewController = children.first {
|
|
fromViewController.willMove(toParent: nil)
|
|
fromViewController.view.removeFromSuperview()
|
|
fromViewController.removeFromParent()
|
|
}
|
|
|
|
if let toViewController = child.rootViewController {
|
|
addChild(toViewController)
|
|
view.addConstrained(subview: toViewController.view)
|
|
toViewController.didMove(toParent: self)
|
|
}
|
|
}
|
|
}
|