lantosbro / laravel-oauth2-client
Laravel OAuth2 客户端
Requires
- php: ^8.0|^8.1
- illuminate/support: ^7.0|^8.0|^9.0|^10.0
- league/oauth2-client: ^2.0
- nesbot/carbon: ^1.26.3 || ^2.0
Requires (Dev)
- orchestra/testbench: ^5.0|^6.0|^7.0
- phpunit/phpunit: ^8.0|^9.0|^10.0
This package is auto-updated.
Last update: 2024-09-30 01:47:00 UTC
README
Laravel OAuth 2 客户端认证包
一个小型的 OAuth2 客户端认证库
支持我们
我们在创建开源包上投入了大量精力,如果您从使用这些包的产品中获得收入,我们将不胜感激资助。
安装
您可以通过 composer 安装此包
composer require lantosbro/laravel-oauth2-client
包助手
包助手可用于返回包版本
用法
此库的主要目的是处理 OAuth2 的认证需求。然后您应该有一个可以在 API 客户端使用的令牌。
提供文件和数据库的令牌驱动程序。
文件
文件驱动程序将在 storage/app/oauth2 中保存一个文件,其中包含与 OAuth2 服务器通信所需的令牌详细信息。
数据库
配置
如果您想使用 DB 驱动程序并希望自定义表名,则可以发布配置文件并修改 table_name 列
php artisan vendor:publish --provider="LantosBro\OAuth2\Providers\OAuth2ServiceProvider" --tag="integration-config"
迁移
如果您使用 DB 驱动程序,则需要发布迁移。
php artisan vendor:publish --provider="LantosBro\OAuth2\Providers\OAuth2ServiceProvider" --tag="integration-migrations"
然后您需要运行迁移
php artisan migrate
集成配置
大部分设置都可以在配置文件中找到,需要将其复制并放置在 Laravel 配置目录中
return [ 'oauth2' => [ 'clientId' => '', 'clientSecret' => '', ], 'options' => [ 'scope' => ['openid email profile offline_access accounting.settings accounting.transactions accounting.contacts accounting.journals.read accounting.reports.read accounting.attachments'] ], 'tokenProcessor' => '\LantosBro\OAuth2\Support\AuthorisationProcessor', 'tokenModel' => '\LantosBro\OAuth2\Support\Token\File', 'authorisedRedirect' => '', 'failedRedirect' => '', ];
(待办:创建一个命令来自动发布配置文件)
由于库的主要关注点是包,因此需要通过服务提供器以集成名称将其加载到 Laravel 中。所以对于 xero:-
$this->mergeConfigFrom(__DIR__.'/../../config/config.php', 'xero');
您还需要检查 OAuth2 服务器的凭证要求,并根据需要添加到配置中。
授权与授权处理器
已预定义路由以连接到 OAuth2 服务器,命名路由为 'oauth2.authorise' 和 'oauth2.callback',两者都需要传递集成。所以对于 xero:-
route('oauth2.authorise', ['integration' => 'xero']); // will return /oauth2/xero/authorise
如果您使用的是简单的服务器并且所有设置都正确完成,我们应该能够迅速链接账户。
然而,某些 API 将具有自定义处理需求,例如 Xero 需要 tenant id。
在这些情况下,我们需要创建一个自定义授权处理器,该处理器接受 League/Oauth2-client AccessToken 和集成名称,以便可以拉取配置。
所以对于 Xero,它看起来是这样的:
<?php namespace LantosBro\Xero\Support; use LantosBro\Xero\Facades\Identity; use LantosBro\Xero\Identity\Connection; use LantosBro\Xero\Exceptions\CantRetreiveTenantException; class AuthorisationProcessor { public function __construct($accessToken, $integration) { $config = config($integration); $token = $config['tokenModel']; $token = (new $token($integration))->set([ 'accessToken' => $accessToken->getToken(), 'refreshToken' => $accessToken->getRefreshToken(), 'expires' => $accessToken->getExpires(), 'idToken' => $accessToken->getValues()['id_token'] ])->save(); $connection = Identity::connection()->raw()->get(); if($connection != []){ $tenantId = $connection->json()[0]['tenantId']; $token->set(['tenantId' => $tenantId])->save(); return $token; } else{ throw new CantRetreiveTenantException; } } }
现在我们的访问令牌等已保存,我们应该能够使用 macsidigital/laravel-api-client 与 OAuth2 API 进行通信,当然每个 API 都会有所不同,因此您需要检查文档。以下是我们如何使用存储的详细信息与 Xero API 通信的示例。
<?php namespace LantosBro\Xero\Support; use LantosBro\Xero\Facades\Client; use LantosBro\API\Support\Entry as ApiEntry; class Entry extends ApiEntry { public function newRequest() { $config = config('xero'); $class = $config['tokenModel']; $token = new $class('xero'); if($token->hasExpired()){ $token = $token->renewToken(); } return Client::baseUrl($config['baseUrl'])->withToken($token->accessToken())->withHeaders(['xero-tenant-id' => $token->tenantId()]); } }
测试
composer test
待办事项
- 测试
- 一些正式的文档
基本上,我们只是在定义如何授权和与 API 通信。有关更多详细信息,请参阅 laravel-api-client 的文档。
变更日志
有关最近更改的更多信息,请参阅变更日志。
贡献
有关详细信息,请参阅贡献。
安全性
如果您发现任何安全相关的问题,请通过电子邮件info@macsi.co.uk而不是使用问题跟踪器。
鸣谢
许可
MIT 许可协议 (MIT)。请参阅许可文件获取更多信息。