承接 emiljimenez21/laravel-jwt-auth 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

emiljimenez21/laravel-jwt-auth

最新稳定版本:1.0.0

Composer 安装命令:

composer require emiljimenez21/laravel-jwt-auth

包简介

A JWT client for applications behind an OIDC or OAuth2.0 identity provider

README 文档

README

Latest Version on Packagist Total Downloads GitHub Actions

This package introduces a JWT based authentication mechanism into your laravel application. It is designed for SPA's that use an OpenID Connect (OIDC) or OAuth 2.0 identity provider with public PKCE-enabled clients.

Installation

You can install the package via composer:

composer require emiljimenez21/laravel-jwt-auth

Basic usage

Step 1: Place the public key you use to sign your JWTs in the /storage directory with the filename oauth_public.key

Step 2: Add the HasJWT trait to your user model

use EmilJimenez21\LaravelJWTAuth\Traits\HasJWT;

class User extends Authenticatable {
    use HasJWT;
}

Step 3: Update your api guard in /config/auth.php to use the jwt driver

'guards' => [
    'api' => [
        'driver' => 'jwt',
        'provider' => 'users'
    ]
]

Step 4: Specify the user resolver in AppServiceProvider boot method. Do this when you don't have the user stored in the application.

The user resolver enables your application to use the bearer token to make requests to the IDP so you can create and populate the user if they don't exist.

NOTE: This feature is intended for applications that use a public PKCE flow to generate access tokens on the client from the Idp.

public function boot(): void
{
    /**
     * This code will only be ran when the jwt subject does not exist in this system.
     * 
     * The user resolver expects a callable that accepts a ?string $bearerToken and a
     * ?User response. It provides a great way for your application to quickly create
     * users that aren't in the system yet.    
     * */
    JWT::setUserResolver(function ($bearerToken) {
        // Call the IDP and get the user profile data
        $response = Http::withHeader('Authorization', "Bearer $bearerToken")
            ->get('http://localhost:8000/api/user');

        // Retrieve the response body
        $contents = $response->getBody()->getContents();

        // Convert the json response to an array
        $userData = json_decode($contents, true);

        // Return a new or existing user
        return User::query()->firstOrCreate([
            'id' => $userData['data']['id']
        ]);
    });
}

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Credits

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-10-01