ely / oauth2-client
Ely.by 提供的 league/oauth2-client 扩展包
Requires
- league/oauth2-client: ^1.0 | ^2.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ^4.8 || ^5.0
This package is auto-updated.
Last update: 2024-09-21 22:17:26 UTC
README
本包为 PHP League 的 OAuth 2.0 Client 提供了 Ely.by OAuth 2.0 支持。OAuth 2.0 Client 的详细信息,请访问 League 仓库的 README 文件。
安装
使用 composer 进行安装
composer require ely/oauth2-client
用法
用法与 The League 的 OAuth 客户端相同,使用 \Ely\OAuth2\Client\Provider
作为提供者。更多详细信息请参阅 League 仓库 README。
您可以在 Ely.by 账户 OAuth2 注册页面 获取自己的 clientId
和 clientSecret
。
<?php $provider = new \Ely\OAuth2\Client\Provider([ 'clientId' => '{elyby-client-id}', 'clientSecret' => '{elyby-client-secret}', 'redirectUri' => 'http://example.com/callback-uri', ]);
我们建议将此提供者对象放入服务定位器中,以便随时访问或模拟测试。在下面的代码中,我们认为 $provider
包含我们的提供者对象。
授权码流
首先,您必须生成一个将用户重定向到设置状态会话值并重定向用户到 Ely.by 授权页面的路由。这可以通过将以下代码放置在控制器中完成
<?php $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); exit();
注意,getAuthorizationUrl()
接受一个覆盖参数的数组作为参数。例如,如果您想请求额外的作用域并更改应用程序描述,则必须传递带有所需值的 scope
和 description
键
<?php $authUrl = $provider->getAuthorizationUrl([ 'scope' => ['account_info', 'account_email'], 'description' => 'My super application!', ]);
用户在 Ely.by 账户网站上完成身份验证和授权后,将被重定向回您在提供者配置中指定的 redirectUri
。在 redirectUri 处理程序内部,您必须检查错误和状态匹配。如果所有检查都通过,则尝试将接收到的 auth_code
交换为 access_token
。这可以通过以下代码完成
<?php if (isset($_GET['error'])) { echo 'Oh no! The error ' . $_GET['error'] . ' with message ' . $_GET['message']; } elseif (!isset($_GET['state']) || $_GET['state'] !== $_SESSION['oauth2state']) { unset($_SESSION['oauth2state']); echo 'Invalid state value.'; } else { // Try to get an access token (using the authorization code grant) $token = $provider->getAccessToken(new \League\OAuth2\Client\Grant\AuthorizationCode(), [ 'code' => $_GET['code'], ]); // Optional: Now you have a token you can look up a users account data try { // We got an access token, let's now get the user's details $account = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $account->getUsername()); } catch (\Ely\OAuth2\Client\Exception\IdentityProviderException $e) { // Failed to get user details echo 'Cannot get user account identity. The error is ' . $e->getMessage(); } // Use this to interact with an API on the users behalf echo $token->getToken(); }
刷新令牌
刷新令牌仅提供给请求离线访问的应用程序。您可以通过在生成授权 URL 时设置 scope
选项来指定离线访问
<?php $authUrl = $provider->getAuthorizationUrl([ 'scope' => ['account_info', 'account_email', 'offline_access'], ]);
请注意,刷新令牌仅在第一次请求时返回,之后将为 null。您应该在刷新令牌返回时安全地存储刷新令牌
<?php $token = $provider->getAccessToken('authorization_code', [ 'code' => $code ]); // persist the token in a database $refreshToken = $token->getRefreshToken();
现在您已经拥有了使用刷新令牌刷新访问令牌所需的一切
<?php $token = $provider->getAccessToken(new League\OAuth2\Client\Grant\RefreshToken(), [ 'refresh_token' => $refreshToken, ]);
测试
$ ./vendor/bin/phpunit
贡献
有关详细信息,请参阅 CONTRIBUTING。
致谢
本包由 Ely.by 项目团队设计和开发。我们还感谢所有 贡献者 的帮助。
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件。