马里奥/castle

简单易用的两步验证集成

安装: 68

依赖项: 2

建议者: 0

安全: 0

星星: 1

关注者: 1

分支: 0

开放问题: 0

语言:CSS

类型:laravel

2.0.8 2024-02-26 14:59 UTC

This package is auto-updated.

Last update: 2024-09-03 15:11:36 UTC


README

Logo

Castle

这个Laravel包可以帮助您快速将双因素认证添加到现有的应用中,类似于谷歌认证。

功能

  • 使用示例应用流程进行演示。
  • 两步认证。
  • 中间件保护。

徽章

MIT License version

安装

使用Composer安装my-project

  composer require mariojgt/castle
  php artisan install::castle

用法/示例

1:您需要将特质分配给用户模型表,以便使用两步验证并访问备用码。

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',
    ];

这将确保您有权访问备用码

2:为了同步用户,您需要首先使用辅助工具生成认证器密钥,通常在注册时或使用控制器方法同步认证器

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:此时,认证器已对该用户启用,现在您需要在此处保护中间件,以下是一个示例

// 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:显示用户码,通常您只显示一次备用码,您可以使用以下示例

Auth()->user()->getCodes; // this will return the backup codes for that user

5:使用备用码查看示例

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
	}

}

技术栈

客户端: TailwindCSS,vuejs,blade

服务器: 2fa,Laravel