--- title: "WebAuthn Integration" description: "Comprehensive guide to Sonr's WebAuthn/FIDO2 implementation for passwordless authentication" icon: "key" sidebarTitle: "WebAuthn Integration" --- This documentation covers the WebAuthn implementation in the Sonr blockchain, providing a secure, passwordless authentication mechanism through W3C-compliant WebAuthn protocols. ## Overview Sonr's WebAuthn implementation enables gasless onboarding and secure transaction authorization without requiring users to hold tokens initially. This document provides a comprehensive guide to understanding and using our WebAuthn client. ## Architecture WebAuthn Architecture Diagram The WebAuthn implementation is structured across three primary layers: 1. **Client Layer** (`client/auth/webauthn.go`) - WebAuthnClient interface - Registration and Authentication flows - DID integration 2. **Internal WebAuthn Package** (`internal/webauthn/`) - COSE key parsing - CBOR encoding/decoding - Attestation verification - Signature verification (ES256/RS256) 3. **DID Module Layer** (`x/did/keeper/`) - WebAuthn controller verifier - Credential storage in DID documents - Challenge generation and validation ## API Reference ### WebAuthnClient Interface ```go type WebAuthnClient interface { // Registration Operations BeginRegistration(ctx context.Context, opts *RegistrationOptions) (*RegistrationChallenge, error) CompleteRegistration(ctx context.Context, challenge *RegistrationChallenge, response *AuthenticatorAttestationResponse) (*WebAuthnCredential, error) // Authentication Operations BeginAuthentication(ctx context.Context, opts *AuthenticationOptions) (*AuthenticationChallenge, error) CompleteAuthentication(ctx context.Context, challenge *AuthenticationChallenge, response *AuthenticatorAssertionResponse, credentialID string) (*AuthenticationResult, error) // Credential Management Methods... } ``` ## Usage Examples ### Registration Flow ```go Registration Example func registerWebAuthn() error { client := auth.NewWebAuthnClient() ctx := context.Background() // Begin registration regOpts := &auth.RegistrationOptions{ UserID: "user123", Username: "alice@example.com", DisplayName: "Alice Smith", UserVerification: "preferred", } challenge, err := client.BeginRegistration(ctx, regOpts) if err != nil { return err } // Complete registration response := &auth.AuthenticatorAttestationResponse{ ClientDataJSON: clientDataJSON, AttestationObject: attestationObject, } credential, err := client.CompleteRegistration(ctx, challenge, response) return err } ``` ### Authentication Flow ```go Authentication Example func authenticateWebAuthn(credentialID string) error { client := auth.NewWebAuthnClient() ctx := context.Background() // Begin authentication authOpts := &auth.AuthenticationOptions{ UserVerification: "required", AllowedCredentials: []*auth.CredentialDescriptor{ { Type: "public-key", ID: []byte(credentialID), }, }, } challenge, err := client.BeginAuthentication(ctx, authOpts) if err != nil { return err } // Complete authentication response := &auth.AuthenticatorAssertionResponse{ ClientDataJSON: clientDataJSON, AuthenticatorData: authenticatorData, Signature: signature, UserHandle: userHandle, } result, err := client.CompleteAuthentication(ctx, challenge, response, credentialID) return err } ``` ## Security Considerations ### Supported Algorithms ECDSA with P-256 curve and SHA-256 RSASSA-PKCS1-v1_5 with SHA-256 ### Attestation Formats 1. **none**: No attestation (development/testing) 2. **packed**: Self-attestation or certificate chain 3. **fido-u2f**: Legacy U2F authenticators 4. **android-safetynet**: Android device attestation ### Security Features - **Challenge uniqueness**: Each challenge is unique and time-bound - **Origin validation**: Ensures requests come from trusted origins - **User verification**: Requires biometric or PIN when configured - **Counter tracking**: Detects cloned credentials - **Credential isolation**: Each DID has separate credential namespace ## Configuration ### Chain Parameters ```json Configuration Example { "webauthn": { "rp_id": "sonr.io", "rp_name": "Sonr Network", "timeout": 60000, "user_verification": "preferred", "attestation": "none", "allowed_origins": [ "https://sonr.io", "https://app.sonr.io" ] } } ``` ## Troubleshooting - **"Invalid attestation format"**: Ensure authenticator supports the configured format - **"Challenge mismatch"**: Verify challenge hasn't expired - **"Origin validation failed"**: Check allowed origins list - **"User verification required"**: Ensure authenticator supports verification ## Contributing ### Development Setup ```bash Setup Commands # Clone repository git clone https://github.com/sonr-io/sonr.git # Install dependencies make install # Run WebAuthn tests make test-webauthn ``` ## References Official W3C WebAuthn Specification FIDO2 Client to Authenticator Protocol ## License Copyright 2024 Sonr Inc. Licensed under the Apache License, Version 2.0. ```