# WebAuthn [![Latest Version on Packagist](https://img.shields.io/packagist/v/laragear/webauthn.svg)](https://packagist.org/packages/laragear/webauthn) [![Latest stable test run](https://github.com/Laragear/WebAuthn/workflows/Tests/badge.svg)](https://github.com/Laragear/WebAuthn/actions) [![Codecov coverage](https://codecov.io/gh/Laragear/WebAuthn/branch/1.x/graph/badge.svg?token=HIngrvQeOj)](https://codecov.io/gh/Laragear/WebAuthn) [![CodeClimate Maintainability](https://api.codeclimate.com/v1/badges/39841b40ab4b05b8f9d3/maintainability)](https://codeclimate.com/github/Laragear/WebAuthn/maintainability) [![Sonarcloud Status](https://sonarcloud.io/api/project_badges/measure?project=Laragear_WebAuthn&metric=alert_status)](https://sonarcloud.io/dashboard?id=Laragear_WebAuthn) [![Laravel Octane Compatibility](https://img.shields.io/badge/Laravel%20Octane-Compatible-success?style=flat&logo=laravel)](https://laravel.com/docs/9.x/octane#introduction) Authenticate users with fingerprints, patterns and biometric data. ```php // App\Http\Controllers\LoginController.php use Laragear\WebAuthn\Http\Requests\AssertedRequest; public function login(AssertedRequest $request) { $user = $request->login(); return response()->json(['message' => "Welcome back, $user->name!"]); } ``` > You want to add two-factor authentication to your app? Check out [Laragear TwoFactor](https://github.com/Laragear/TwoFactor). ## Keep this package free [![Patreon](.github/assets/patreon.png)](https://patreon.com/packagesforlaravel)[![Ko-fi](.github/assets/ko-fi.png)](https://ko-fi.com/DarkGhostHunter)[![Buymeacoffee](.github/assets/buymeacoffee.png)](https://www.buymeacoffee.com/darkghosthunter)[![PayPal](.github/assets/paypal.png)](https://www.paypal.com/paypalme/darkghosthunter) Your support allows me to keep this package free, up-to-date and maintainable. Alternatively, you can **[spread the word!](http://twitter.com/share?text=I%20am%20using%20this%20cool%20PHP%20package&url=https://github.com%2FLaragear%2FWebAuthn&hashtags=PHP,Laravel)** ## Requirements * PHP 8.0 or later, with `ext-openssl`. * Laravel 9.x or later. ## Installation Require this package into your project using Composer: ```bash composer require laragear/webauthn ``` ## How does it work? WebAuthn authentication process consists in two _ceremonies_: attestation, and assertion. Attestation is the process of asking the authenticator (a phone, laptop, USB key...) to create a private-public key pair, and **register** the public key inside the app. For that to work, the user must exist, and the browser must support WebAuthn, which is what intermediates between the authenticator (OS & device hardware) and the app. Assertion is the process of pushing a cryptographic challenge to the device, which will return back _signed_ by the private key. Upon arrival, the app checks the signature is correct with the stored public key, ready to **log in**. The private key doesn't leave the authenticator, and there are no shared passwords to save, let alone remember. ## Set up We need to make sure your users can register their devices and authenticate with them. 1. [Add the `eloquent-webauthn` driver](#1-add-the-eloquent-webauthn-driver) 2. [Create the `webauthn_credentials` table](#2-create-the-webauthn_credentials-table) 3. [Implement the contract and trait](#3-implement-the-contract-and-trait) After that, you can quickly start WebAuthn with the included controllers and helpers to make your life easier. 4. [Register the controllers](#4-register-the-routes-and-controllers) 5. [Use the Javascript helper](#5-use-the-javascript-helper) ### 1. Add the `eloquent-webauthn` driver Laragear WebAuthn works by extending the Eloquent User Provider with an additional check to find a user for the given WebAuthn Credentials (Assertion). This makes this WebAuthn package compatible with any guard you may have. Simply go into your `auth.php` configuration file, change the driver from `eloquent` to `eloquent-webauthn`, and add the `password_fallback` to `true`. ```php return [ // ... 'providers' => [ 'users' => [ 'driver' => 'eloquent-webauthn', 'model' => App\User::class, 'password_fallback' => true, ], ] ]; ``` The `password_fallback` indicates the User Provider should fall back to validate the password when the request is not a WebAuthn Assertion. It's enabled to seamlessly use both classic and WebAuthn authentication procedures. ### 2. Create the `webauthn_credentials` table Create the `webauthn_credentials` table by publishing the migration file and migrating the table: ```shell php artisan vendor:publish --provider="Laragear\WebAuthn\WebAuthnServiceProvider" --tag="migrations" php artisan migrate ``` > You may edit the migration to your liking, like adding new columns, but **not** to remove them or change their name. ### 3. Implement the contract and trait Add the `WebAuthnAuthenticatable` contract and the `WebAuthnAuthentication` trait to the User class, or any other that uses authentication. ```php {{-- ... --}} @vite(['resources/js/app.js', 'resources/js/vendor/webauthn/webauthn.js']) ``` Once done, you can easily start registering and login in users. For example, for a logged-in user, you may show a registration view in HTML with the following code: ```html