diff --git a/README.md b/README.md index 0b41245..a967843 100644 --- a/README.md +++ b/README.md @@ -61,7 +61,7 @@ We need to make sure your users can register their devices and authenticate with 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-controllers) +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 @@ -120,26 +120,29 @@ class User extends Authenticatable implements WebAuthnAuthenticatable From here you're ready to work with WebAuthn Authentication. The following steps will help you close the gap to a full implementation. -### 4. Register the controllers +### 4. Register the routes and controllers -WebAuthn uses exclusive controller actions to registering and authenticating users. If you want a quick start, just publish the controllers and routes included in Laragear WebAuthn. +WebAuthn uses exclusive routes to register and authenticate users. Creating these routes and controller may be cumbersome, specially if it's your first time in the WebAuthn real. + +Instead, go for a quick start and publish the controllers included in Laragear WebAuthn. These controllers will be located at `app\Http\Controllers\WebAuthn`. ```shell -php artisan vendor:publish --provider="Laragear\WebAuthn\WebAuthnServiceProvider" --tag="routes" php artisan vendor:publish --provider="Laragear\WebAuthn\WebAuthnServiceProvider" --tag="controllers" ``` -The `webauthn.php` route file should be added to your `routes` directory. You can pick them up easily in your `RouteServiceProvider`, or go the quick way and require the file from your `web.php` routes file. +Next, to pick these controllers easily, go into your `web.php` routes file and register a default set of routes with the `WebAuthn::routes()` method. ```php +// web.php use Illuminate\Support\Facades\Route; +use Laragear\WebAuthn\WebAuthn; + +Route::view('welcome'); // WebAuthn Routes -Route::group([], base_path('routes/webauthn.php')); +WebAuthn::routes(); ``` -Along with the routes, the authentication controllers will be located at `App\Http\Controllers\WebAuthn`, which these routes point them toward automatically. - ### 5. Use the Javascript helper This package includes a simple but convenient script to handle WebAuthn Attestation and Assertion. To use it, just publish the `webauthn.js` asset into your application public resources. diff --git a/src/WebAuthn.php b/src/WebAuthn.php index 3a42672..08cba0a 100644 --- a/src/WebAuthn.php +++ b/src/WebAuthn.php @@ -2,6 +2,8 @@ namespace Laragear\WebAuthn; +use Illuminate\Support\Facades\Route; + /** * @internal */ @@ -23,16 +25,26 @@ class WebAuthn public const RESIDENT_KEY_DISCOURAGED = 'discouraged'; /** - * Returns all user verifications flags possible. + * Registers a set of default WebAuthn routes. * - * @return string[] + * @return void */ - public static function userVerifications(): array + public static function routes(): void { - return [ - static::USER_VERIFICATION_REQUIRED, - static::USER_VERIFICATION_PREFERRED, - static::USER_VERIFICATION_DISCOURAGED, - ]; + Route::middleware('web')->group(static function (): void { + Route::post('webauthn/register/options') + ->uses([\App\Http\Controllers\WebAuthn\WebAuthnRegisterController::class, 'options']) + ->name('webauthn.register.options'); + Route::post('webauthn/register') + ->uses([\App\Http\Controllers\WebAuthn\WebAuthnRegisterController::class, 'register']) + ->name('webauthn.register'); + + Route::post('webauthn/login/options') + ->uses([\App\Http\Controllers\WebAuthn\WebAuthnLoginController::class, 'options']) + ->name('webauthn.login.options'); + Route::post('webauthn/login') + ->uses([\App\Http\Controllers\WebAuthn\WebAuthnLoginController::class, 'login']) + ->name('webauthn.login'); + }); } } diff --git a/tests/Http/Controllers/StubControllersTest.php b/tests/Http/Controllers/StubControllersTest.php index c18c033..855cfc3 100644 --- a/tests/Http/Controllers/StubControllersTest.php +++ b/tests/Http/Controllers/StubControllersTest.php @@ -7,6 +7,7 @@ use Laragear\WebAuthn\Http\Requests\AssertionRequest; use Laragear\WebAuthn\Http\Requests\AttestationRequest; use Laragear\WebAuthn\Http\Requests\AttestedRequest; use Laragear\WebAuthn\JsonTransport; +use Laragear\WebAuthn\WebAuthn; use Tests\Stubs\WebAuthnAuthenticatableUser; use Tests\TestCase; @@ -14,7 +15,7 @@ class StubControllersTest extends TestCase { protected function defineWebRoutes($router): void { - $router->group([], __DIR__ . '/../../../routes/webauthn.php'); + WebAuthn::routes(); } public function test_uses_attestation_request(): void diff --git a/tests/WebAuthnTest.php b/tests/WebAuthnTest.php new file mode 100644 index 0000000..fdcba6c --- /dev/null +++ b/tests/WebAuthnTest.php @@ -0,0 +1,23 @@ +