mydaniel/laravel-paseto
Composer 安装命令:
composer require mydaniel/laravel-paseto
包简介
Paseto (Platform-Agnostic Security Tokens) authentication guard for Laravel. A secure alternative to JWT.
README 文档
README
This package provides a PASETO (Platform-Agnostic Security Tokens) authentication guard for Laravel. It offers a modern, secure, and easy-to-use alternative to JWT (JSON Web Tokens).
Paseto tokens are encrypted and authenticated, providing better security guarantees than JWT out of the box.
Features
- ✅ Secure, stateless authentication for your Laravel applications.
- ✅ PASETO v4 Local support (symmetric key authenticated encryption).
- ✅ Easy to configure and use.
- ✅ Token blacklist functionality to invalidate tokens upon logout.
- ✅ An artisan command to generate a secure secret key.
- ✅ Fully customizable through configuration and contracts.
Installation
You can install the package via composer:
composer require mydaniel/laravel-paseto
Configuration
-
Publish the configuration file:
This will create a
config/paseto.phpfile in your project. You can customize token expiration, issuer, audience, and other claims here.php artisan vendor:publish --provider="MyDaniel\Paseto\PasetoServiceProvider" --tag="config"
-
Generate a Secret Key:
Run the following artisan command to generate a secure, 32-byte hex-encoded key. The command will automatically add it to your
.envfile.php artisan paseto:generate-key
This will add a line like this to your
.envfile:PASETO_SECRET_KEY="your-generated-secret-key"
-
Configure the Auth Guard:
Open your
config/auth.phpfile and make the following changes:'defaults' => [ 'guard' => 'api', // Or your default guard 'passwords' => 'users', ], 'guards' => [ 'api' => [ 'driver' => 'paseto', 'provider' => 'users', ], ],
Usage
1. Preparing Your User Model
Add the MyDaniel\Paseto\Contracts\PasetoSubject contract and the MyDaniel\Paseto\Traits\HasPaseto trait to your User model.
<?php namespace App\Models; use Illuminate\Foundation\Auth\User as Authenticatable; use MyDaniel\Paseto\Contracts\PasetoSubject; use MyDaniel\Paseto\Traits\HasPaseto; class User extends Authenticatable implements PasetoSubject { use HasPaseto; // ... your model properties and methods }
The PasetoSubject contract ensures your model has the necessary methods for generating token claims. The HasPaseto trait provides a ready-to-use implementation for these methods.
2. Generating a Token
After authenticating a user (e.g., in a login controller), you can generate a token for them.
use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; class AuthController extends Controller { public function login(Request $request) { $credentials = $request->only('email', 'password'); if (!Auth::attempt($credentials)) { return response()->json(['error' => 'Unauthorized'], 401); } /** @var \App\Models\User $user */ $user = Auth::user(); $token = $user->generateToken(); return response()->json(['token' => $token]); } }
3. Protecting Routes
Use the auth:api middleware in your routes/api.php file to protect routes that require authentication.
use Illuminate\Support\Facades\Route; Route::middleware('auth:api')->get('/user', function (Request $request) { return $request->user(); });
Clients should send the token in the Authorization header as a Bearer token:
Authorization: Bearer <your-paseto-token>
4. Logging Out (Invalidating a Token)
To invalidate a token, you can use the logout method from the Auth facade. This will add the token's unique identifier (jti) to the blacklist for its remaining lifetime.
public function logout() { Auth::logout(); return response()->json(['message' => 'Successfully logged out']); }
Note: The blacklist feature requires a cache driver. By default, it uses your application's default cache driver. You can specify a different cache store in config/paseto.php.
Customizing Token Claims
You can add custom claims to your tokens by implementing the getJwtCustomClaims method in your User model.
// In App\Models\User public function getJwtCustomClaims(): array { return [ 'foo' => 'bar', 'role' => $this->role, ]; }
Contributing
Contributions are welcome! Please feel free to submit a pull request on GitHub.
License
The MIT License (MIT). Please see License File for more information.
mydaniel/laravel-paseto 适用场景与选型建议
mydaniel/laravel-paseto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 897 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 08 月 20 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「security」 「Authentication」 「auth」 「laravel」 「token」 「jwt」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 mydaniel/laravel-paseto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 mydaniel/laravel-paseto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 mydaniel/laravel-paseto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
A lightweight and powerful OAuth 2.0 authorization and resource server library with support for all the core specification grants. This library will allow you to secure your API with OAuth and allow your applications users to approve apps that want to access their data from your API.
Provide a way to secure accesses to all routes of an symfony application.
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
统计信息
- 总下载量: 897
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-08-20