awuniversity / oauth2-aw
OAuth 2.0 客户端 AW
1.0.8
2020-07-22 11:16 UTC
Requires
- awuniversity/oauth2-client: >=1.0
README
本软件包为 PHP AwUniversity 的 OAuth 2.0 客户端 提供Google OAuth 2.0 支持。
本软件包符合 PSR-1、PSR-2 和 PSR-4 标准。如果发现不符合标准,请通过 pull request 发送补丁。
要求
支持以下版本的 PHP。
- PHP 7.0
- PHP 7.1
- PHP 7.2
- PHP 7.3
本软件包使用 OpenID Connect 对 Google 账户进行用户认证。
使用本软件包需要 Google 客户端 ID 和客户端密钥。在文档中,它们分别称为 {aw-client-id}
和 {aw-client-secret}
。
请按照 Google 说明 创建所需的凭证。
安装
使用 composer 安装
composer require awuniversity/oauth2-aw
使用方法
授权码流程
use AwU\OAuth2\Client\Provider\Google; $provider = new Google([ 'clientId' => '{aw-client-id}', 'clientSecret' => '{aw-client-secret}', 'redirectUri' => 'https://example.com/callback-url', 'hostedDomain' => 'example.com', // optional; used to restrict access to users on your G Suite/Google Apps for Business accounts ]); 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 $ownerDetails = $provider->getResourceOwner($token); // Use these details to create a new profile printf('Hello %s!', $ownerDetails->getFirstName()); } 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(); }
可用选项
Google
提供以下 选项
accessType
用于在线或离线访问hostedDomain
用于验证 G Suite 用户prompt
用于修改用户将看到的提示scopes
用于请求访问更多用户信息
访问令牌 JWT
Google 为所有访问令牌提供 JSON Web Token (JWT)。此令牌 包含有关已认证用户的基本信息。可以从访问令牌的 id_token
值访问 JWT。
/** @var AwU\OAuth2\Client\Token\AccessToken $token */ $values = $token->getValues(); /** @var string */ $jwt = $values['id_token'];
解析 JWT 需要一个 JWT 解析器。请参阅解析器文档以获取说明。
刷新令牌
刷新令牌仅提供给请求离线访问的应用程序。您可以通过在提供者中设置 accessType
选项来指定离线访问。
use AwU\OAuth2\Client\Provider\Google; $provider = new Google([ 'clientId' => '{aw-client-id}', 'clientSecret' => '{aw-client-secret}', 'redirectUri' => 'https://example.com/callback-url', 'accessType' => 'offline', ]);
需要注意的是,刷新令牌仅在第一次请求时返回,之后将返回 null
。您应该安全地存储返回的刷新令牌。
$token = $provider->getAccessToken('authorization_code', [ 'code' => $code ]); // persist the token in a database $refreshToken = $token->getRefreshToken();
如果您需要获取新的刷新令牌,可以通过强制审批提示来请求一个。
$authUrl = $provider->getAuthorizationUrl(['approval_prompt' => 'force']);
现在,您已经有了使用刷新令牌刷新访问令牌所需的一切。
use AwU\OAuth2\Client\Provider\Google; use AwU\OAuth2\Client\Grant\RefreshToken; $provider = new Google([ 'clientId' => '{aw-client-id}', 'clientSecret' => '{aw-client-secret}', 'redirectUri' => 'https://example.com/callback-url', ]); $grant = new RefreshToken(); $token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);
作用域
可以通过在生成授权 URL 时使用 scope
参数设置额外的 作用域。
$authorizationUrl = $provider->getAuthorizationUrl([ 'scope' => [ 'scope-url-here' ], ]);
测试
可以使用以下方式运行测试
composer test
可以使用以下方式运行样式检查
composer check
贡献
请参阅 CONTRIBUTING 以获取详细信息。
致谢
许可证
MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。