pschocke / laravel-webauthn-core
不带观点的Laravel Webauthn支持
Requires
- guzzlehttp/psr7: ^1.5 || ^2.0
- laravel/framework: ^5.8 || ^6.0 || ^7.0 || ^8.0
- php-http/discovery: ^1.6
- php-http/httplug: ^1.0 || ^2.0
- php-http/message: ^1.7
- psr/http-client: ^1.0
- thecodingmachine/safe: ^1.0
- web-auth/cose-lib: ^3.0
- web-auth/webauthn-lib: ^3.0
- web-token/jwt-signature: ^1.3 || ^2.0
Requires (Dev)
- ext-sqlite3: *
- laravel/legacy-factories: ^1.0
- nunomaduro/larastan: ^0.4 || ^0.5 || ^0.6 || ^0.7
- ocramius/package-versions: ^1.5 || ^2.0
- orchestra/testbench: ^3.5 || ^5.0 || ^6.0
- phpstan/phpstan-deprecation-rules: ^0.12
- phpstan/phpstan-phpunit: ^0.12
- phpstan/phpstan-strict-rules: ^0.12
- phpunit/phpunit: ^6.0 || ^7.0 || ^8.0 || ^9.0
- psalm/plugin-laravel: ^1.4
- thecodingmachine/phpstan-safe-rule: ^1.0
- vimeo/psalm: ^3.9 || ^4.0
This package is auto-updated.
Last update: 2024-09-29 06:03:12 UTC
README
LaravelWebauthn是一个用于Laravel的Webauthn适配器。它为您提供构建双因素认证登录或授权所需的所有工具。
它高度受asbiin的laravel-webauthn包的启发,并从中复制了大量代码,但不含路由、控制器和中间件,以提供最大限度的灵活性。它基本上是提取的核心功能。
如果您需要一个可以直接应用到您应用程序中的现成实现,请务必查看他的仓库。
安装
您可以使用Composer将此包安装到您的Laravel项目中
composer require pschocke/laravel-webauthn
您不需要将此包添加到您的服务提供者中。
支持
此包支持Laravel 5.8及更高版本,并已在php 7.2及更高版本上进行测试。
它基于asbiin/laravel-webauthn,而web-auth/webauthn-framework又基于此。
重要
如果没有以下内容,您的浏览器将拒绝与您的安全设备协商中继:
- 域名(localhost和127.0.0.1将被webauthn.js拒绝)
- 您的浏览器信任的SSL/TLS证书(自签名证书是可以的)
- 连接HTTPS端口443(除443以外的端口将被拒绝)
配置
您可以通过运行以下Artisan命令将LaravelWebauthn配置发布到名为config/webauthn.php的文件中,并发布资源:
php artisan vendor:publish --provider="Pschocke\LaravelWebauthn\LaravelWebauthnServiceProvider"
这将配置文件发布到/config/webauthn.php,并创建一个新的迁移。
之后,运行您的迁移
php artisan migrate
初始配置
Webauthn通常与User模型一起使用,但要让您能够使用它与其他模型一起使用,您需要在用于认证的模型上实现WebauthnCredentiable接口
class User extends Authenticatable implements Pschocke\LaravelWebauthn\Contracts\WebauthnCredentiable {}
用法
以下示例仅展示了一种注册和认证的方法。还有很多其他方法可以使Webauthn授权工作。
注册新的Webauthn设备
要使用Webauthn设备(例如touchID、Yubikey、Windows Hello等)来验证用户,您首先需要注册该设备并将其连接到用户。
注册由javascript启动并在服务器端进行验证。首先,生成一个公钥并将其提供给javascript
$publicKey = Pschocke\LaravelWebauthn\Facades\Webauthn::getRegisterData($user);
请确保保留公钥以供验证部分使用,例如在会话中或在您的livewire组件中。
<!-- load javascript part --> <script src="{!! secure_asset('vendor/pschocke/webauthn-laravel/webauthn.js') !!}"></script> ... <!-- button click to run registration --> <button onclick="register()"> Register new Device </button> <!-- form to send datas to --> <form method="POST" id="form"> @csrf <input type="hidden" name="register" id="register" /> <input type="hidden" name="name" id="name" /> </form> ... <!-- script part to run the sign part --> <script> var publicKey = {!! json_encode($publicKey) !!}; var webauthn = new WebAuthn(); register = function() { webauthn.register( publicKey, function (datas) { document.getElementById('register').value = JSON.stringify(datas); document.getElementById('form').submit(); } ); } </script>
提交时,验证响应并将其附加到用户
Pschocke\LaravelWebauthn\Facades\Webauthn::doRegister( $user, $publicKey, $submittedData, $nameOfTheKey );
如果遇到损坏的数据,此方法将抛出异常。如果没有错误运行,则表示密钥已注册,您可以通知用户其成功。
认证
用户注册Webauthn设备后,您可以检查给定的设备是否已注册给指定的用户
首先,您需要生成一个公钥并将其发送到您的javascript
$publicKey = Pschocke\LaravelWebauthn\Facades\Webauthn::getAuthenticateData($user);
请确保保留公钥以供验证部分使用,例如在会话中或在您的livewire组件中。
<!-- load javascript part --> <script src="{!! secure_asset('vendor/pschocke/webauthn-laravel/webauthn.js') !!}"></script> ... <!-- button click to run registration --> <button onclick="register()"> Register new Device </button> <!-- form to send datas to --> <form method="POST" action="" id="form"> @csrf <input type="hidden" name="data" id="data" /> </form> ... <!-- script part to run the sign part --> <script> var publicKey = {!! json_encode($publicKey) !!}; var webauthn = new WebAuthn(); webauthn.sign( publicKey, function (datas) { document.getElementById("data").value = JSON.stringify(datas), document.getElementById("form").submit(); } ); </script>
最后,您需要验证公钥响应
$result = Webauthn::doAuthenticate( $request->user(), $publicKey, $request->input( 'data') );
如果遇到损坏的数据,此方法将抛出异常。
如果结果为true,则表示您的用户已成功验证,您可以为应用程序的部分进行登录/授权等操作。
许可证
在MIT许可证下许可。 查看许可证。
大量的代码是由Alexis Saettler编写的