Skip to content

Commit

Permalink
Refactored passkey authenticator to not be stored on add delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Jul 1, 2024
1 parent 3c53a1c commit 21c7469
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Simplenote/Classes/SPAuthHandler.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ class SPAuthHandler {

/// Simperium Authenticator
///
private let simperiumService: SPAuthenticator
let simperiumService: SPAuthenticator

/// Designated Initializer.
///
Expand Down
6 changes: 4 additions & 2 deletions Simplenote/Classes/SPAuthViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ class SPAuthViewController: UIViewController {
///
private lazy var validator = AuthenticationValidator()

private var passkeyAuthenticator: PasskeyAuthenticator?

/// # Indicates if we've got valid Credentials. Doesn't display any validation warnings onscreen
///
private var isInputValid: Bool {
Expand Down Expand Up @@ -323,8 +325,8 @@ private extension SPAuthViewController {
Task {
//TODO: Handle errors
//TODO: Handle email not valid
let passkeyAuthenticator = SPAppDelegate.shared().passkeyAuthenticator
try? await passkeyAuthenticator.attemptPasskeyAuth(for: email, in: self)
passkeyAuthenticator = PasskeyAuthenticator(authenticator: controller.simperiumService)
try? await passkeyAuthenticator?.attemptPasskeyAuth(for: email, in: self)
}
}

Expand Down
12 changes: 8 additions & 4 deletions Simplenote/Classes/SPSettingsViewController+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,27 @@ extension SPSettingsViewController {
extension SPSettingsViewController {
@objc
func presentPasskeyAuthenticationSetupAlert() {
let appDelegate = SPAppDelegate.shared()
guard let email = appDelegate.simperium.user?.email else {
return
}
let authenticator = PasskeyAuthenticator(authenticator: appDelegate.simperium.authenticator)
self.passkeyAuthenticator = authenticator

let alert = UIAlertController(title: PasskeyAuthentication.alertTitle, message: PasskeyAuthentication.message, preferredStyle: .alert)
alert.addTextField { textField in
textField.textContentType = .password
textField.isSecureTextEntry = true
}

let action = UIAlertAction(title: PasskeyAuthentication.submit, style: .default) { [unowned alert] _ in
let appDelegate = SPAppDelegate.shared()
guard let textfield = alert.textFields?.first,
let password = textfield.text,
let email = appDelegate.simperium.user?.email else {
let password = textfield.text else {
return
}

Task {
do {
let authenticator = appDelegate.passkeyAuthenticator
try await authenticator.registerPasskey(for: email, password: password, in: self)
} catch {
// TODO: Display some action for failure
Expand Down
4 changes: 4 additions & 0 deletions Simplenote/Classes/SPSettingsViewController.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
#import <UIKit/UIKit.h>
#import "SPTableViewController.h"

@class PasskeyAuthenticator;

@interface SPSettingsViewController : SPTableViewController <UIPickerViewDelegate, UIPickerViewDataSource> {
//Preferences
NSNumber *sortOrderPref;
NSNumber *numPreviewLinesPref;
}

@property (nonatomic, strong) PasskeyAuthenticator *passkeyAuthenticator;

@end

extern NSString *const SPAlphabeticalTagSortPref;
Expand Down
4 changes: 0 additions & 4 deletions Simplenote/SPAppDelegate+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,6 @@ extension SPAppDelegate {
func setupStoreManager() {
StoreManager.shared.initialize()
}

@objc func setupPasskeyAuthenticator() {
passkeyAuthenticator = PasskeyAuthenticator(authenticator: simperium.authenticator)
}
}

// MARK: - Internal Methods
Expand Down
3 changes: 0 additions & 3 deletions Simplenote/SPAppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
@class PublishStateObserver;
@class AccountDeletionController;
@class CoreDataManager;
@class PasskeyAuthenticator;

NS_ASSUME_NONNULL_BEGIN

Expand Down Expand Up @@ -42,8 +41,6 @@ NS_ASSUME_NONNULL_BEGIN

@property (nullable, strong, nonatomic) AccountDeletionController *accountDeletionController;

@property (strong, nonatomic) PasskeyAuthenticator *passkeyAuthenticator;

- (void)presentSettingsViewController;

- (void)save;
Expand Down
1 change: 0 additions & 1 deletion Simplenote/SPAppDelegate.m
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ - (BOOL)application:(UIApplication *)application willFinishLaunchingWithOptions:
[self setupThemeNotifications];
[self setupSimperium];
[self setupAuthenticator];
[self setupPasskeyAuthenticator];
[self setupAppCenter];
[self setupCrashLogging];
[self configureVersionsController];
Expand Down

0 comments on commit 21c7469

Please sign in to comment.