Skip to content

Commit

Permalink
Wired up authentication with passkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
charliescheer committed Jul 16, 2024
1 parent ce0f245 commit 8127485
Showing 1 changed file with 40 additions and 2 deletions.
42 changes: 40 additions & 2 deletions Simplenote/AuthViewController+Swift.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Foundation
import AuthenticationServices

// MARK: - AuthViewController: Interface Initialization
//
Expand Down Expand Up @@ -201,8 +202,20 @@ extension AuthViewController {

@objc
func performPasskeyAuthentication() {
//TODO: Login with Passkeys
print("# Logging in with passkeys!!!!")
startActionAnimation()
setInterfaceEnabled(false)

let passkeyAuthenticator = PasskeyAuthenticator()
Task { @MainActor in
do {
try await attemptPasskeyAuthentication(for: usernameText)
} catch {
passkeyAuthFailed(error)
}

stopActionAnimation()
setInterfaceEnabled(true)
}
}

@objc
Expand Down Expand Up @@ -332,6 +345,30 @@ extension AuthViewController {
}
}

// MARK: - Passkeys
//
extension AuthViewController: ASAuthorizationControllerPresentationContextProviding {
public func presentationAnchor(for controller: ASAuthorizationController) -> ASPresentationAnchor {
view.window!
}

private func attemptPasskeyAuthentication(for email: String) async throws {
let passkeyAuthenticator = PasskeyAuthenticator()
let verify = try await passkeyAuthenticator.attemptPasskeyAuth(for: email, in: self)
authenticator.authenticate(withUsername: verify.username, token: verify.accessToken)
}

private func passkeyAuthFailed(_ error: any Error) {
presentPasskeyAuthError(error)
}

private func presentPasskeyAuthError(_ error: any Error) {
let alert = NSAlert(messageText: Localization.passkeyAuthFailureTitle, informativeText: error.localizedDescription)
alert.addButton(withTitle: Localization.cancelText)
alert.runModal()
}
}


// MARK: - Localization
//
Expand All @@ -351,4 +388,5 @@ private enum Localization {
static let unverifiedErrorMessage = NSLocalizedString("There was an preparing your verification email, please try again later", comment: "Request error alert message")
static let verificationSentTitle = NSLocalizedString("Check your Email", comment: "Vefification sent alert title")
static let verificationSentTemplate = NSLocalizedString("We’ve sent a verification email to %1$@. Please check your inbox and follow the instructions.", comment: "Confirmation that an email has been sent")
static let passkeyAuthFailureTitle = NSLocalizedString("Passkey Authentication Failed", comment: "Title for passkey authentication failure")
}

0 comments on commit 8127485

Please sign in to comment.