定制 hyrioo/hyrnatic-authenticator 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hyrioo/hyrnatic-authenticator

Composer 安装命令:

composer require hyrioo/hyrnatic-authenticator

包简介

This is my package hyrnatic-authenticator

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

JWT based authentication for Laravel
With refresh tokens and automatic reuse detection.

Installation

You can install the package via composer:

composer require hyrioo/hyrnatic-authenticator
php artisan vendor:publish --provider="Hyrioo\HyrnaticAuthenticator\HyrnaticAuthenticatorServiceProvider"

You can publish and run the migrations with:

php artisan vendor:publish --tag="hyrnatic-authenticator-migrations"
php artisan migrate

You can publish the config file with:

php artisan vendor:publish --tag="hyrnatic-authenticator-config"

Usage

Update user model

Add the Hyrioo\HyrnaticAuthenticator\HasApiTokens trait to your user model.

use Hyrioo\HyrnaticAuthenticator\Traits\HasApiTokens;
 
class User extends Authenticatable
{
    use HasApiTokens;
}

Configure auth guard

Add hyrnatic-authenticator as the driver to your api guard.
Example:

'guards' => [
        'api' => [
            'driver' => 'hyrnatic-authenticator',
            'provider' => 'users',
        ],
    ],

Issuing tokens

When issuing a new token, you will get a new token family, an access token and a refresh token. The token family can have a name, if you eg. want to show the active logins for the user. You can set individual expire for all three. It's also possible to set custom claims for both the access and refresh token.

$builder = auth('api')->create($user) // NewTokenBuilder
$builder->setName('Phone'); // Optional
$builder->setScopes(['photo.manage']); // Optional
$builder->setFamilyExpiresAt(now()->addYear()); // Optional
$builder->setAccessExpiresAt(now()->addMinutes(5)); // Optional
$builder->setRefreshExpiresAt(now()->addMonth()); // Optional

$token = $builder->getToken();
$token->accessToken;
$token->refreshToken;

Refresh token

When refreshing the token, you can set a new expiry for both the access and refresh tokens. But scopes and custom claims will be the same.

$builder = auth('api')->refresh($request->refresh_token); // RefreshTokenBuilder
$builder->setAccessExpiresAt(now()->addMinutes(5)); // Optional
$builder->setRefreshExpiresAt(now()->addMonth()); // Optional

$token = $builder->refreshToken();
$token->accessToken;
$token->refreshToken;

Revoke token

When revoking a token the entire token family will be revoked, and both refresh and access tokens will stop working.

auth('api')->logout();

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.

统计信息

  • 总下载量: 18
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-17