Wednesday, November 8, 2023
HomeiOS Developmentswift - Capacitor with iOS Passkey easy methods to get registration information

swift – Capacitor with iOS Passkey easy methods to get registration information


I am implementing Passkey to my Ionic app and I could not discover any plugin on the market so I am constructing my very own plugin. Every little thing works until triggering the passkey on the native iOS, however I could not get the registration information from iOS. How can I get the information from the AppDelegate? My code relies on the Apple official documentation instance: https://developer.apple.com/documentation/authenticationservices/connecting_to_a_service_with_passkeys.

Within the instance, it wanted the AppDelegate to get the registration information delegate, nevertheless I could not handle to make my Capacitor Plugin to make use of the principle AppDelegate. In consequence, authorizationController doesn’t run after passkey registration. Every little thing already works high quality to get problem from webview and cross it to native and set off passkey immediate on the iOS machine, simply could not get the registration end result information.

PasskeyPlugin.swift

@objc(PasskeyPlugin)
public class PasskeyPlugin: CAPPlugin {

    non-public let implementation = Passkey()
    
    @objc func create(_ name: CAPPluginCall) {
        let problem: Knowledge? = name.getString("problem")!.information(utilizing: .utf8)
        
        DispatchQueue.foremost.async {
            let signInViewController = SignInViewController(problem: problem!)
            self.bridge?.viewController?.current(signInViewController, animated: true, completion: nil)
        }
            
        name.resolve([
            "platform": "iOS Native Passkey",
        ])
    }
}

SignInViewController.swift

class SignInViewController: UIViewController {
    var problem: Knowledge!

    init(problem: Knowledge) {
        self.problem = problem
        print(self.problem! as NSData)
        tremendous.init(nibName: nil, bundle: nil)
    }

override func viewDidAppear(_ animated: Bool) {
        tremendous.viewDidAppear(animated)

        signInObserver = NotificationCenter.default.addObserver(forName: .UserSignedIn, object: nil, queue: nil) {_ in
            self.didFinishSignIn()
        }

        signInErrorObserver = NotificationCenter.default.addObserver(forName: .ModalSignInSheetCanceled, object: nil, queue: nil) { _ in
            self.showSignInForm()
        }

        guard let window = self.view.window else { fatalError("The view was not within the app's view hierarchy!") }
        
        let userName = "Identify"
        
        let accountManager = AccountManager()
        accountManager.signUpWith(userName: userName, problem: self.problem, anchor: window)
        
        // I can't use the AppDelegate with errors
        // (UIApplication.shared.delegate as? AppDelegate)?.accountManager.signUpWith(userName: userName, problem: self.problem, anchor: window)
    }
}

AccountManager.swift

extension NSNotification.Identify {
    static let UserSignedIn = Notification.Identify("UserSignedInNotification")
    static let ModalSignInSheetCanceled = Notification.Identify("ModalSignInSheetCanceledNotification")
}


class AccountManager: NSObject, ASAuthorizationControllerPresentationContextProviding, ASAuthorizationControllerDelegate {
    let area = "com.area.my"
    var authenticationAnchor: ASPresentationAnchor?
    var isPerformingModalReqest = false

    func signUpWith(userName: String, problem: Knowledge, anchor: ASPresentationAnchor) {
        self.authenticationAnchor = anchor
        
        if #obtainable(iOS 15.0, *) {
            let publicKeyCredentialProvider = ASAuthorizationPlatformPublicKeyCredentialProvider(relyingPartyIdentifier: area)
            
            // The userID is the identifier for the consumer's account.
            // Laborious coded for instance functions
            let userID = "d0a4bc91-2def-4567-8983-9188a4ca2048".information(utilizing: .utf8)!

            let registrationRequest = publicKeyCredentialProvider.createCredentialRegistrationRequest(problem: problem,
                                                                                                      identify: "Some identify", userID: userID)

            let authController = ASAuthorizationController(authorizationRequests: [ registrationRequest ] )
            authController.delegate = self
            authController.presentationContextProvider = self
            authController.performRequests()
            isPerformingModalReqest = true
        } else {
            // Fallback on earlier variations
        }
    }

    func authorizationController(controller: ASAuthorizationController, didCompleteWithAuthorization authorization: ASAuthorization) {
        if #obtainable(iOS 14.0, *) {
            let logger = Logger()
            
            if #obtainable(iOS 15.0, *) {
                change authorization.credential {
                case let credentialRegistration as ASAuthorizationPlatformPublicKeyCredentialRegistration:
                    logger.log("A brand new passkey was registered: (credentialRegistration)")
                    // Confirm the attestationObject and clientDataJSON along with your service.
                    // The attestationObject accommodates the consumer's new public key to retailer and use for subsequent sign-ins.
                    // let attestationObject = credentialRegistration.rawAttestationObject
                    // let clientDataJSON = credentialRegistration.rawClientDataJSON
                    
                    // After the server verifies the registration and creates the consumer account, signal within the consumer with the brand new account.
                    didFinishSignIn()
                case let credentialAssertion as ASAuthorizationPlatformPublicKeyCredentialAssertion:
                    logger.log("A passkey was used to register: (credentialAssertion)")
                    // Confirm the under signature and clientDataJSON along with your service for the given userID.
                    // let signature = credentialAssertion.signature
                    // let clientDataJSON = credentialAssertion.rawClientDataJSON
                    // let userID = credentialAssertion.userID
                    
                    // After the server verifies the assertion, signal within the consumer.
                    didFinishSignIn()
                case let passwordCredential as ASPasswordCredential:
                    logger.log("A password was supplied: (passwordCredential)")
                    // Confirm the userName and password along with your service.
                    // let userName = passwordCredential.consumer
                    // let password = passwordCredential.password
                    
                    // After the server verifies the userName and password, signal within the consumer.
                    didFinishSignIn()
                default:
                    fatalError("Obtained unknown authorization sort.")
                }
            } else {
                // Fallback on earlier variations
            }
        } else {
            // Fallback on earlier variations
        }

        isPerformingModalReqest = false
    }
}

How can I get authorizationController to run after passkey immediate on the native machine? Thanks upfront.



Supply hyperlink

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments