lahaxearnaud / laravel-token
Laravel 4 token 管理
Requires
- php: >=5.4.0
- illuminate/support: 4.2.*
Requires (Dev)
- codeclimate/php-test-reporter: dev-master
- laravel/framework: 4.2.*
- orchestra/testbench: ~2.2.0
- phpunit/phpunit: 3.7.*
This package is auto-updated.
Last update: 2022-02-01 12:43:04 UTC
README
目录
安装
{ "require": { "lahaxearnaud/laravel-token": "~0.5" } }
数据库
$ php artisan migrate --package="lahaxearnaud/laravel-token"
提供者
'providers' => array( // ... 'Lahaxearnaud\LaravelToken\LaravelTokenServiceProvider', ),
外观
'aliases' => array( // ... 'Token' => 'Lahaxearnaud\LaravelToken\LaravelTokenFacade', ),
用法
创建 token
$token = Token::create($userID, $allowLogin);
如果 $allowLogin
设置为 true,则 token 可用于通过路由过滤器进行身份验证。
加密 token
$token = Token::create($userID, $allowLogin); $cryptToken = Token::cryptToken($token->token);
如果 $allowLogin
设置为 true,则 token 可用于通过路由过滤器进行身份验证。
验证 token
如果你加密了你的 token
$tokenStr = Token::getTokenValueFromRequest(); $cryptToken = Token::isValidCryptToken($token->token, $userId);
如果你没有加密你的 token
$tokenStr = Token::getTokenValueFromRequest(); $cryptToken = Token::isValidToken($token->token, $userId);
如果你使用这些函数,token 不会被销毁。它可以多次使用。
对于一次性使用的 token
$tokenStr = Token::getTokenValueFromRequest(); /** * if the token is crypt do : * $tokenStr = Token::uncryptToken($tokenStr); **/ $tokenValid = true; try { // find the token $token = $token->findByToken($tokenStr, $userId); // test the token validity if (Token::isValidToken($token)) { // do what you need to do // delete the token Token::burn($token); } else { $tokenValid = false; } } catch (TokenNotFoundException $e) { $tokenValid = false; } if($tokenValid) { // manage errors }
路由过滤器
简单的 token 保护
Route::get('/token-protected', array('before' => 'token', function () { echo "I am token protected"; }));
通过 token 身份验证
用于身份验证的 token 必须是登录 token,请参阅 token 创建部分
Route::get('/login-by-token', array('before' => 'token.auth', function () { echo Auth::user()->username; }));
为了使用通过 token 进行身份验证,你的 User 类需要实现 Lahaxearnaud\LaravelToken\Models\UserTokenInterface
use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; use Lahaxearnaud\LaravelToken\Models\UserTokenInterface; class User extends Eloquent implements UserInterface, RemindableInterface, UserTokenInterface { use UserTrait, RemindableTrait; /** * The database table used by the model. * * @var string */ protected $table = 'users'; /** * The attributes excluded from the model's JSON form. * * @var array */ protected $hidden = array('password', 'remember_token'); public function loggableByToken() { return true; } }
当用户尝试使用 token 进行身份验证时,会调用 loggableByToken
方法
如果在 token 验证时发生错误,则会抛出 TokenExeption 异常,请参阅 异常 部分
默认情况下,您可以通过参数或头信息发送您的 token。字段的默认名称为 token
,但您可以通过发布和更改配置来更改它
$ php artisan config:publish lahaxearnaud/laravel-token
然后更改 tokenFieldName config/packages/lahaxearnaud/laravel-token/config.php
您可以通过以下方式获取 token 实例
Token::getCurrentToken();
异常
如果你使用路由过滤器,你需要处理一些异常。在 filter.php
中添加以下错误处理器来捕获它们。这是一个基本示例,请根据您的需求更改行为(重定向、记录...)
App::error(function(\Lahaxearnaud\LaravelToken\exeptions\TokenException $exception) { if($exception instanceof \Lahaxearnaud\LaravelToken\exeptions\TokenNotFoundException) { return \Response::make('Unauthorized (Not found)', 401); } if($exception instanceof \Lahaxearnaud\LaravelToken\exeptions\TokenNotValidException) { return \Response::make('Unauthorized (Not valid token)', 401); } if($exception instanceof \Lahaxearnaud\LaravelToken\exeptions\UserNotLoggableByTokenException) { return \Response::make('Unauthorized (Not loggable by token)', 401); } if($exception instanceof \Lahaxearnaud\LaravelToken\exeptions\NotLoginTokenException) { return \Response::make('Unauthorized (Not login token)', 401); } });
事件
你可以监听事件
- 找不到 token
- 名称:
token.notFound
- 参数
- token 字符串
- 名称:
- token 无效
- 名称:
token.notValid
- 参数
- token 对象
- 名称:
- token 不允许用于登录
- 名称:
token.notLoginToken
- 参数
- token 对象
- 名称:
- 用户无法使用 token 登录
- 名称:
token.notLoggableUser
- 参数
- token 对象
- 用户对象
- 名称:
- 代币销毁
- 名称:
token.burned
- 参数
- token 对象
- 名称:
- 代币创建
- 名称:
token.created
- 参数
- token 对象
- 名称:
- 代币保存
- 名称:
token.saved
- 参数
- token 对象
- 名称:
命令
A new artisan command is added to your project in order to help you to clean your token table
### Delete expired tokens
Without any option the command delete all expired tokens.
```bash
$ php artisan token:clean
```
### Truncate the table
If you specified ``--all`` all token will be deleted
```bash
$ php artisan token:clean -all
```
API
安全
对字符串代币进行加密以获取公共代币
Token::cryptToken ($uncrypt)
解密公共代币以获取私有代币
Token::uncryptToken ($crypt)
创建
创建代币实例(直接保存在数据库中)
Token::create ($userId, $allowLogin = false, $lifetime = 3600, $length = 100)
如果 $allowLogin
设置为 true,则 token 可用于通过路由过滤器进行身份验证。
删除
删除代币
Token::burn (Token $token)
验证
获取代币,检查代币是否有正确的用户ID以及是否未过期
Token::isValidToken ($token, $userId)
与isValidToken相同,但在尝试查找之前先解密代币
Token::isValidCryptToken ($token, $userId)
仅验证代币是否过期
Token::isValid (Token $token)
查找
按ID查找代币
Token::find ($id)
按代币字符串查找代币
Token::findByToken ($token, $userId)
查找某个用户的全部代币
Token::findByUser ($idUser)
待办
- 配置以允许用户和类型仅有一个代币