web_authn_web 0.0.1
web_authn_web: ^0.0.1 copied to clipboard
Flutter Web plugin for WebAuthn (passkeys) registration and authentication.
web_authn_web #
A Flutter Web implementation for WebAuthn (Web Authentication API), allowing you to register and authenticate users using passkeys/biometrics.
This package wraps the web_authen.js logic and exposes it via a clean, strictly typed Dart API.
Platform support: Web only (Flutter Web).
WebAuthn references #
- WebAuthn specification (W3C): https://www.w3.org/TR/webauthn-3/
- Web Authentication API overview (W3C): https://www.w3.org/TR/webauthn-2/
- WebAuthn.org (FIDO Alliance): https://webauthn.org/
Features #
- Register: Create a new public key credential.
- Sign (Login): Authenticate using an existing credential.
- Delete: Signal deletion of a credential.
- Typed API: Use Dart classes like
PublicKeyCredentialCreationOptionsinstead of raw Maps.
Installation #
Add this package to your pubspec.yaml:
dependencies:
web_authn_web: ^0.0.1
Or run:
flutter pub add web_authn_web
Usage #
This package automatically injects the required JavaScript code (web_authen.js) into your application.
Requirements #
- Secure context: WebAuthn requires HTTPS (or
localhost). - Relying Party ID:
rp.id(andrpIdin login) must match your effective domain (e.g.,example.com). - Server side: You must generate challenges and verify the returned attestation/assertion on your backend.
Data encoding expectations #
challenge: base64url encoded string.user.id: base64 encoded string (standard base64, not url-safe).allowCredentials[].idandexcludeCredentials[].id: base64url encoded string.
Register a Passkey #
import 'package:web_authn_web/web_authn_web.dart';
final webAuthn = WebAuthnWeb();
final options = PublicKeyCredentialCreationOptions(
rp: RpEntity(name: 'ACME Corp', id: 'example.com'),
user: UserEntity(
name: 'user@example.com',
id: 'CAMW', // base64 encoded id
displayName: 'User Name',
),
challenge: 'Y2hhbGxlbmdl', // base64url encoded challenge
pubKeyCredParams: [
PubKeyCredParam(type: 'public-key', alg: -7), // ES256
],
authenticatorSelection: AuthenticatorSelectionCriteria(
authenticatorAttachment: 'platform',
),
);
try {
final result = await webAuthn.register(options);
print('Registration successful: $result');
} catch (e) {
print('Registration failed: $e');
}
Authenticate (Sign) #
final options = PublicKeyCredentialRequestOptions(
challenge: 'Y2hhbGxlbmdl', // base64url encoded challenge
rpId: 'example.com',
userVerification: 'required',
);
try {
final result = await webAuthn.sign(options);
print('Sign successful: $result');
} catch (e) {
print('Sign failed: $e');
}
Delete Credential #
try {
await webAuthn.deleteAuth('credentialId', 'rpId');
} catch (e) {
print('Delete failed: $e');
}
Notes #
- This package only performs the browser-side WebAuthn calls. Always validate the returned data on your server.
- Passkey APIs are not available in all browsers or in insecure contexts.