swift-server / swift-webauthn

A Swift library for implementing the WebAuthn spec
https://swiftpackageindex.com/swift-server/webauthn-swift/main/documentation/webauthn
Apache License 2.0
127 stars 25 forks source link

How to finish registration #66

Open jaydipdeveloper opened 5 months ago

jaydipdeveloper commented 5 months ago

let webAuthnManager = WebAuthnManager( configuration: WebAuthnManager.Configuration( relyingPartyID: "oloid.ai", relyingPartyName: "My Fancy Web App", relyingPartyOrigin: "https://oloid.ai" ) )

    Task {
        let result = webAuthnManager.beginRegistration(user: PublicKeyCredentialUserEntity(id: self.generateRandomBytes(count: 10), name: "Jaydip", displayName: "Jaydip Finava"))
        print(result)
        let id = result.user.id.base64URLEncodedString()

        let request = RegistrationCredential

how to create request object of type RegistrationCredential?

        let confirmCredentialIDNotRegisteredYet: (String) async throws -> Bool = { credentialID in
            return true
        }
        do {
            let credential = try await webAuthnManager.finishRegistration(
                challenge: result.challenge,
                credentialCreationData: RegistrationCredential,
                confirmCredentialIDNotRegisteredYet: confirmCredentialIDNotRegisteredYet
            )
            print(credential)
        } catch {
            print(error.localizedDescription)
        }
    }
dimitribouniol commented 5 months ago

how to create request object of type RegistrationCredential

You decode this from the web browser or your app — you'll need to pass the result from beginRegistration() to the client, have it sign that via a passkey, and supply it back to the server to be finalized and stored with finishRegistration(). Have you already gone through the documentation here? https://swiftpackageindex.com/swift-server/webauthn-swift/main/documentation/webauthn/example-implementation

jaydipdeveloper commented 5 months ago

I'm using on native swift

let webAuthnManager = WebAuthnManager( configuration: WebAuthnManager.Configuration( relyingPartyID: "domain", relyingPartyName: "My Fancy Web App", relyingPartyOrigin: "https://domain.ai" ) )

    Task {
        let result = webAuthnManager.beginRegistration(user: PublicKeyCredentialUserEntity(id: self.generateRandomBytes(count: 10), name: "Jaydip", displayName: "Jaydip Finava"))
        print(result)
        let id = result.user.id.base64URLEncodedString()

        let request = RegistrationCredential(id: id, type: .publicKey, rawID: result.challenge, attestationResponse: AuthenticatorAttestationResponse(clientDataJSON: result.challenge, attestationObject: result.challenge))

        let confirmCredentialIDNotRegisteredYet: (String) async throws -> Bool = { credentialID in
            return true
        }
        do {
            let credential = try await webAuthnManager.finishRegistration(
                challenge: result.challenge,
                credentialCreationData: request,
                confirmCredentialIDNotRegisteredYet: confirmCredentialIDNotRegisteredYet
            )
            print(credential)
        } catch {
            print(error)
        }
    }
jaydipdeveloper commented 5 months ago

Can you give me sample code in iOS Swift please?

dimitribouniol commented 5 months ago

This library is for a server backend, not an iOS app. Apple has documentation here for the iOS half of the equation, assuming you are interacting with a supported server: https://developer.apple.com/documentation/authenticationservices/connecting_to_a_service_with_passkeys

jaydipdeveloper commented 5 months ago

I need to do passkey like Dashlane iOS app

On Tue, 7 May 2024 at 6:36 PM, Dimitri Bouniol @.***> wrote:

This library is for a server backend, not an iOS app. Apple has documentation here for the iOS half of the equation, assuming you are interacting with a supported server: https://developer.apple.com/documentation/authenticationservices/connecting_to_a_service_with_passkeys

— Reply to this email directly, view it on GitHub https://github.com/swift-server/webauthn-swift/issues/66#issuecomment-2098364164, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOLYOFJ2OEB5DZTTITB4KPLZBDGW7AVCNFSM6AAAAABHKL5N3CVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDAOJYGM3DIMJWGQ . You are receiving this because you authored the thread.Message ID: @.***>

jaydipdeveloper commented 5 months ago

WebAuthnError(reason: PassDemo.WebAuthnError.Reason.invalidAttestationObject)