benbjurstrom/plink
Composer 安装命令:
composer require benbjurstrom/plink
包简介
Secure One-Time Passwords For Laravel
关键字:
README 文档
README
Passwordless Log-In Links for Laravel
This package provides full-featured passwordless log-in links for Laravel applications.
- ✅ Rate limited
- ✅ Invalidated after first use
- ✅ Locked to the user's session
- ✅ Configurable expiration
- ✅ Detailed error messages
- ✅ Customizable mail template
- ✅ Auditable logs
Installation
1. Install the package via composer
composer require benbjurstrom/plink
2. Add the package's interface and trait to your Authenticatable model
// app/Models/User.php namespace App\Models; //... use BenBjurstrom\Plink\Models\Concerns\HasPlinks; use BenBjurstrom\Plink\Models\Concerns\Plinkable; class User extends Authenticatable implements Plinkable { use HasFactory, Notifiable, HasPlinks; // ... }
3. Publish and run the migrations
php artisan vendor:publish --tag="plink-migrations"
php artisan migrate
4. Add the package provided routes
// routes/web.php Route::plinkRoutes();
5. (Optional) Publish the views for custom styling
php artisan vendor:publish --tag="plink-views"
This package publishes the following views:
resources/
└── views/
└── vendor/
└── plink/
├── error.blade.php
├── components/
└── template.blade.php
└── mail/
├── notification.blade.php
└── plink.blade.php
6. (Optional) Publish the config file
php artisan vendor:publish --tag="plink-config"
This is the contents of the published config file:
<?php return [ /* |-------------------------------------------------------------------------- | Link Expiration and Throttling |-------------------------------------------------------------------------- | | These settings control the security aspects of the generated links, | including their expiration time and the throttling mechanism to prevent | abuse. | */ 'expiration' => 5, // Minutes 'limits' => [ ['limit' => 1, 'minutes' => 1], ['limit' => 3, 'minutes' => 5], ['limit' => 5, 'minutes' => 30], ], /* |-------------------------------------------------------------------------- | Model Configuration |-------------------------------------------------------------------------- | | This setting determines the model used by Plink to store and retrieve | one-time passwords. By default, it uses the 'App\Models\User' model. | */ 'models' => [ 'authenticatable' => env('AUTH_MODEL', App\Models\User::class), ], /* |-------------------------------------------------------------------------- | Mailable Configuration |-------------------------------------------------------------------------- | | This setting determines the Mailable class used by Plink to send emails. | Change this to your own Mailable class if you want to customize the email | sending behavior. | */ 'mailable' => BenBjurstrom\Plink\Mail\PlinkMail::class, /* |-------------------------------------------------------------------------- | Template Configuration |-------------------------------------------------------------------------- | | This setting determines the email template used by Plink to send emails. | Switch to 'plink::mail.notification' if you prefer to use the default | Laravel notification template. | */ 'template' => 'plink::mail.plink', // 'template' => 'plink::mail.notification', ];
Usage
Laravel Breeze Livewire Example
- Replace the Breeze provided App\Livewire\Forms\LoginForm::authenticate method with a sendEmail method that runs the SendPlink action. Also be sure to remove password from the LoginForm's properties.
// app/Livewire/Forms/LoginForm.php use BenBjurstrom\Plink\Actions\SendPlink; use BenBjurstrom\Plink\Exceptions\PlinkThrottleException; use BenBjurstrom\Plink\Models\Plink; //... #[Validate('required|string|email')] public string $email = ''; #[Validate('boolean')] public bool $remember = false; //... public function sendEmail(): void { $this->validate(); $this->ensureIsNotRateLimited(); RateLimiter::hit($this->throttleKey(), 300); try { (new SendPlink)->handle($this->email, $this->remember); } catch (PlinkThrottleException $e) { throw ValidationException::withMessages([ 'form.email' => $e->getMessage(), ]); } RateLimiter::clear($this->throttleKey()); }
- Update resources/views/livewire/pages/auth/login.blade.php such that the login function calls our new sendEmail method and redirects back with a status confirmation. You can also remove the password input field in this same file.
public function login(): void { $this->validate(); $this->form->sendEmail(); redirect()->back()->with(['status' => 'Login link sent!']); }
Laravel Breeze Inertia Example
- Replace the Breeze provided App\Http\Requests\Auth\LoginRequest::authenticate method with a sendEmail method that runs the SendPlink action. Also be sure to remove password from the rules array.
// app/Http/Requests/Auth/LoginRequest.php use BenBjurstrom\Plink\Actions\SendPlink; use BenBjurstrom\Plink\Exceptions\PlinkThrottleException; use BenBjurstrom\Plink\Models\Plink; //... public function rules(): array { return [ 'email' => ['required', 'string', 'email'] ]; } //... public function sendEmail(): Void { $this->ensureIsNotRateLimited(); RateLimiter::hit($this->throttleKey(), 300); try { (new SendPlink)->handle($this->email, $this->remember); } catch (PlinkThrottleException $e) { throw ValidationException::withMessages([ 'email' => $e->getMessage(), ]); } RateLimiter::clear($this->throttleKey()); }
- Update the App\Http\Controllers\Auth\AuthenticatedSessionController::store method to call our new sendEmail method and redirect back with a status confirmation.
// app/Http/Controllers/Auth/AuthenticatedSessionController.php public function store(LoginRequest $request): RedirectResponse { $request->sendEmail(); return back()->with(['status' => 'Login link sent!']); }
- Remove the password input field from the resources/js/Pages/Auth/Login.vue file.
Everything else is handled by the package components.
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
benbjurstrom/plink 适用场景与选型建议
benbjurstrom/plink 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 22 次下载、GitHub Stars 达 25, 最近一次更新时间为 2024 年 10 月 04 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「Ben Bjurstrom」 「plink」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 benbjurstrom/plink 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 benbjurstrom/plink 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 benbjurstrom/plink 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
First Factor One-Time Passwords for Laravel (Passwordless OTP Login)
OpemAmAuth is a client for authenticating against OpenAM in PHP
Pgvector driver for Laravel Scout
LdapLookup is a tool to lookup entries in LDAP For Laravel 5.1+
First Factor One-Time Passwords for Laravel (Passwordless OTP Login)
Alfabank REST API integration
统计信息
- 总下载量: 22
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 25
- 点击次数: 16
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2024-10-04