enflow/oidconnect-laravel
Composer 安装命令:
composer require enflow/oidconnect-laravel
包简介
Open ID Connect client library for Laravel Framework
关键字:
README 文档
README
The OpenIDConnect Laravel package is meant to provide you an opportunity to easily authenticate users using OpenID Connect protocol.
Installation
To install this package you will need:
- Laravel 5.4+
- PHP 7.1+
Use composer to install
composer require furdarius/oidconnect-laravel:dev-master
Open config/app.php and register the required service providers above your application providers.
'providers' => [ ... Laravel\Socialite\SocialiteServiceProvider::class, Furdarius\OIDConnect\ServiceProvider::class ... ]
If you'd like to make configuration changes in the configuration file you can pubish it with the following Aritsan command:
php artisan vendor:publish --provider="Furdarius\OIDConnect\ServiceProvider"
After that, roll up migrations:
php artisan migrate
Usage
Configuration
At first you will need to add credentials for the OpenID Connect service your application utilizes.
These credentials should be placed in your config/opidconnect.php configuration file.
<?php return [ 'client_id' => 'CLIENT_ID_HERE', 'client_secret' => 'CLIENT_SECRET_HERE', 'redirect' => env('APP_URL') . '/auth/callback', 'auth' => 'https://oidc.service.com/auth', 'token' => 'https://oidc.service.com/token', 'keys' => 'https://oidc.service.com/keys', ];
Endpoints
Now, your app has auth endpoints:
GET /auth/redirect- Used to redirect client to Auth Service login page.GET /auth/callback- Used when Auth Service redirect client to callback url with code.POST /auth/refresh- Used by client for ID Token refreshing.
Middleware
You need to use Auth Middleware on protected routes.
Open App\Http\Kernel and register middleware in $routeMiddleware:
protected $routeMiddleware = [ 'token' => \Furdarius\OIDConnect\TokenMiddleware::class ];
And then use it as usual:
Route::middleware('token')->get('/protected', function (Illuminate\Http\Request $request) { return "You are on protected zone"; });
User Auth
Create your own StatelessGuard and setup it in config/auth.php. Example:
Guard:
<?php namespace App\Auth; use Illuminate\Auth\AuthenticationException; use Illuminate\Auth\GuardHelpers; use Illuminate\Contracts\Auth\Authenticatable; use Illuminate\Contracts\Auth\Guard; use Illuminate\Support\Traits\Macroable; class StatelessGuard implements Guard { use GuardHelpers, Macroable; /** * @return \Illuminate\Contracts\Auth\Authenticatable * @throws AuthenticationException */ public function user() { if (null === $this->user) { throw new AuthenticationException('Unauthenticated user'); } return $this->user; } /** * @param array $credentials * @return bool */ public function validate(array $credentials = []) { return $this->user instanceof Authenticatable; } }
Config (config/auth.php):
'defaults' => [ 'guard' => 'stateless', 'passwords' => 'users', ], ... 'guards' => [ 'stateless' => [ 'driver' => 'stateless' ] ],
Then implement own Authenticator. Example:
<?php namespace App\Auth; use App\User; use Furdarius\OIDConnect\Contract\Authenticator; use Furdarius\OIDConnect\Exception\AuthenticationException; use Lcobucci\JWT\Token\DataSet; class PersonAuthenticatorAdapter implements Authenticator { /** * @param DataSet $claims * * @return void */ public function authUser(DataSet $claims) { $email = $claims->get('email'); if (!$email) { throw new AuthenticationException('User\'s email not present in token'); } $model = new User(['email' => $email]); \Auth::setUser($model); } }
And implement auth guard service provider. Example:
<?php namespace App\Auth; use Furdarius\OIDConnect\Contract\Authenticator; use Illuminate\Support\ServiceProvider; class AuthenticatorServiceProvider extends ServiceProvider { /** * Bootstrap any application services. * * @return void */ public function boot() { \Auth::extend('stateless', function () { return new StatelessGuard(); }); } /** * Register any application services. * * @return void */ public function register() { $this->app->singleton(Authenticator::class, function ($app) { return new PersonAuthenticatorAdapter(); }); } }
Then register it in config/app.php:
'providers' => [
...
App\Auth\AuthenticatorServiceProvider::class,
...
]
Now you can use \Auth::user(); for getting current user information.
enflow/oidconnect-laravel 适用场景与选型建议
enflow/oidconnect-laravel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 551 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 04 月 25 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「oauth」 「laravel」 「openidconnect」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 enflow/oidconnect-laravel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 enflow/oidconnect-laravel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 enflow/oidconnect-laravel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
WeChat OAuth SDK
OIDC connector for yii2-authclient
Ory-Hydra OAuth 2.0 Client Provider for The PHP League OAuth2-Client
Library for ORCID web services
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.
Microsoft Azure Active Directory Authentication Library (AAD) for PHP
统计信息
- 总下载量: 551
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-04-25