mariojgt/castle
Composer 安装命令:
composer require mariojgt/castle
包简介
Simple and easy 2 steps verification integration
README 文档
README
Castle
This Laravel package help you quickly add 2fa authentication in you existing application, simular to google authentication.
Features
- Demo with the example application flow you need.
- 2 steps autentication.
- middleware protection.
Badges
Installation
Install my-project with composer
composer require mariojgt/castle php artisan install::castle
Usage/Examples
1: You need to assign the trait to you user model table in order to use the 2steps verification and have access to the backup codes.
use Mariojgt\Castle\Trait\Castle; class User extends Authenticatable { use HasApiTokens, HasFactory, Notifiable, Castle; /** * The attributes that are mass assignable. * * @var string[] */ protected $fillable = [ 'name', 'email', 'password', ];
this will insure you have access to the backup codes
2: In order to sync the user you need to first generate the authenticator secret using the helper normally when you register or with a controller method to sync the authenticator
use Mariojgt\Castle\Helpers\AuthenticatorHandle; class myController { public register () { // Start the class that handle most of the logic $handle = new AuthenticatorHandle(); // Generate the code $codeInfo = $handle->generateCode($userEmail); // Sync that code with the user using the trait Auth()->user()->syncAuthenticator($codeInfo['secret']); }
3: At this point the authenticator is enabled against that user, now you need to protect the middleware here is a example
// Auth Route Example Route::group([ 'middleware' => ['web', '2fa'], // note you can use (2fa:admin) for admin guard or leave empty for web as default ], function () { // Example page required to be login Route::get('/castle-try', [HomeContoller::class, 'protected'])->name('castle.try'); });
4: Display the user codes, normaly you only display the backup codes once you can use the following example
Auth()->user()->getCodes; // this will return the backup codes for that user
5: using backup codes see the example
use Mariojgt\Castle\Helpers\AuthenticatorHandle; myclass { public myFunction () { // Start the class that handle most of the logic $handle = new AuthenticatorHandle(); // the encryption is using the normal laravel encrypt fuction // example encrypt('user_secret') $handle->useBackupCode($codeYouType, $encryptauthenticatorSecret); // The second parameter is not required } }
Tech Stack
Client: TailwindCSS, vuejs, blade
Server: 2fa, Laravel
统计信息
- 总下载量: 174
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 0
- 依赖项目数: 1
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-25
