vormkracht10 / oauth2-genesys
此软件包为 PHP League 的 OAuth 2.0 客户端提供了 Genesys OAuth 2.0 支持
v0.2.4
2024-02-26 14:46 UTC
Requires
- php: ^8.0
- league/oauth2-client: ^2.6
- league/oauth2-server: ^8.3
- mockery/mockery: ^1.5
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.0
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^9
- spatie/ray: ^1.28
README
此软件包为 PHP League 的 OAuth 2.0 客户端提供了 Genesys OAuth 2.0 支持。
安装
您可以通过 composer 安装此软件包
composer require vormkracht10/oauth2-genesys
使用
使用方法与 The League 的 OAuth 客户端相同,使用 \Vormkracht10\OAuth2Genesys\Provider\Genesys
作为提供者。
定义地区
由于 Genesys 对不同地区使用不同的端点,您需要定义要使用的地区。这可以通过将地区作为参数传递给构造函数来完成。默认地区设置为 us-east-1
。所有地区及其端点可以在 Genesys API 文档 此处 找到。
授权码流
require_once('./vendor/autoload.php'); session_start(); $provider = new \Vormkracht10\OAuth2Genesys\Provider\Genesys([ 'region' => 'us-west-2', 'clientId' => '{genesys-client-id}', 'clientSecret' => '{genesys-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); if (!isset($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: '.$authUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); exit('Invalid state'); } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Optional: Now you have a token you can look up a users profile data try { // We got an access token, let's now get the user's details $user = $provider->getResourceOwner($token); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
测试
composer test
变更日志
请参阅 CHANGELOG 了解最近更改的详细信息。
贡献
请参阅 CONTRIBUTING 了解详细信息。
安全漏洞
请查看 我们的安全策略 了解如何报告安全漏洞。
鸣谢
许可
MIT 许可证 (MIT)。请参阅 许可文件 了解更多信息。