buddy-works / oauth2-client
Buddy OAuth 2.0 客户端提供者
1.0.0
2021-01-19 12:20 UTC
Requires
- php: >=7.2
- ext-json: *
- league/oauth2-client: ^2.4
Requires (Dev)
- friendsofphp/php-cs-fixer: ^2.16
- phpstan/phpstan: ^0.12.18
- phpunit/phpunit: ^9.0
This package is auto-updated.
Last update: 2024-09-19 20:21:21 UTC
README
此软件包为 PHP League 的 OAuth 2.0 客户端 提供Buddy OAuth 2.0 支持。
安装
要安装,请使用 composer
composer require buddy-works/oauth2-client
使用
使用与 The League 的 OAuth 客户端相同,使用 Buddy\OAuth2\Client\Provider\Buddy
作为提供者。
授权代码流
$provider = new Buddy\OAuth2\Client\Provider\Buddy([ 'clientId' => '{buddy-client-id}', 'clientSecret' => '{buddy-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); // Use these details to create a new profile printf('Hello %s!', $user->getNickname()); } 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(); }
管理作用域
创建 Buddy 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。
use Buddy\OAuth2\Client\Provider\Buddy; $options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => [Buddy::SCOPE_WORKSPACE, Buddy::REPOSITORY_READ] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果没有定义,提供者将使用内部默认值。
在编写本文档时,以下作用域可用。
WORKSPACE
访问基本工作区信息以及管理成员、组和成员权限的权利PROJECT_DELETE
删除项目的权限。REPOSITORY_READ
访问提交和仓库内容。也允许仓库检出。REPOSITORY_WRITE
在仓库中写入的权限。也允许文件删除(包含 REPOSITORY_READ 权限)。EXECUTION_INFO
访问执行历史。EXECUTION_RUN
运行和停止执行的权限(包含 EXECUTION_INFO 权限)。EXECUTION_MANAGE
添加/编辑管道的权限(包含 EXECUTION_RUN 权限)。USER_INFO
访问授权用户的基信息。USER_KEY
访问授权用户的公钥。USER_EMAIL
访问授权用户的电子邮件列表。INTEGRATION_INFO
访问授权用户的集成列表。MEMBER_EMAIL
访问工作区成员的联系信息。MANAGE_EMAILS
查看和管理用户电子邮件地址的权限(包含 USER_EMAIL 权限)。WEBHOOK_INFO
访问钩子信息。WEBHOOK_ADD
获取和添加钩子的权限。WEBHOOK_MANAGE
添加/编辑和删除钩子的权限。VARIABLE_ADD
获取和添加环境变量的权限。VARIABLE_INFO
访问环境变量的信息。VARIABLE_MANAGE
添加/编辑和删除环境变量的权限。
测试
composer tests
致谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。