bahuma / oauth2-nextcloud
Nextcloud OAuth 2.0 客户端提供商,适用于 PHP League 的 OAuth 2.0 客户端
2.0.0
2023-05-11 12:23 UTC
Requires
- php: ^7.4||^8.0
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ^1.4
- phpunit/phpunit: ^9.5
- squizlabs/php_codesniffer: ^3.6
This package is auto-updated.
Last update: 2024-09-11 15:44:17 UTC
README
此软件包为 PHP League 的 OAuth 2.0 客户端 提供Nextcloud OAuth 2.0 支持。
此软件包符合 PSR-1、PSR-2 和 PSR-4 标准。如果您发现任何合规性问题,请通过拉取请求发送补丁。
要求
以下版本的 PHP 受支持。
- 从 PHP 7.4 到 PHP 8.2
使用此软件包,需要 Nextcloud 客户端 ID 和客户端密钥。这些在文档中分别称为 {nextcloud-client-id}
和 {nextcloud-client-secret}
。
请按照 Nextcloud 指示 创建所需的凭据。
安装
要安装,请使用 composer
composer require bahuma/oauth2-nextcloud
使用方法
授权码流
use Bahuma\OAuth2\Client\Provider\Nextcloud; session_start(); $provider = new Nextcloud([ 'clientId' => '{nextcloud-client-id}', 'clientSecret' => '{nextcloud-client-secret}', 'redirectUri' => 'https://example.com/callback-url', 'nextcloudUrl' => 'https://cloud.example.com', // Base URL of your nextcloud instance. ]); if (!empty($_GET['error'])) { // Got an error, probably user denied access exit('Got error: ' . htmlspecialchars($_GET['error'], ENT_QUOTES, 'UTF-8')); } elseif (empty($_GET['code'])) { // If we don't have an authorization code then get one $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); exit; } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) { // State is invalid, possible CSRF attack in progress 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 owner details /** @var \Bahuma\OAuth2\Client\Provider\NextcloudResourceOwner $ownerDetails */ $ownerDetails = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $ownerDetails->getEmail()); } catch (Exception $e) { // Failed to get user details exit('Something went wrong: ' . $e->getMessage()); } // Use this to interact with an API on the users behalf echo $token->getToken(); // Use this to get a new access token if the old one expires echo $token->getRefreshToken(); // Unix timestamp at which the access token expires echo $token->getExpires(); }
刷新令牌
$token = $provider->getAccessToken('authorization_code', [ 'code' => $code ]); // persist the token in a database $refreshToken = $token->getRefreshToken();
现在您已经拥有了使用刷新令牌刷新访问令牌所需的一切
use Bahuma\OAuth2\Client\Provider\Nextcloud; use League\OAuth2\Client\Grant\RefreshToken; $provider = new Nextcloud([ 'clientId' => '{google-client-id}', 'clientSecret' => '{google-client-secret}', 'redirectUri' => 'https://example.com/callback-url', 'nextcloudUrl' => 'https://cloud.example.com', // Base URL of your nextcloud instance. ]); $grant = new RefreshToken(); $token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);
作用域
Nextcloud OAuth2 实现目前不支持作用域访问。这意味着每个令牌都有完全访问整个账户的权限,包括对存储文件的读写权限。务必以安全的方式存储 OAuth2 令牌!
测试
可以使用以下方式运行测试
composer test
可以使用以下方式运行样式检查
composer check
鸣谢
许可证
MIT 许可证 (MIT)。有关更多信息,请参阅 许可证文件。