razesoldier / oauth2-eve
EVE Online OAuth 2.0 客户端提供者,用于 The PHP League OAuth2-Client
1.0.3
2023-03-03 12:37 UTC
Requires
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ^1.0
- phpunit/phpunit: ^5.7|^6.0
- squizlabs/php_codesniffer: ^3.0
README
FORK自 https://github.com/killmails/oauth2-eve
本包为 PHP League 的 OAuth 2.0 Client 提供了 EVE Online OAuth 2.0 支持。
安装
安装时,请使用 composer
composer require dts-eve/oauth2-eve
用法
用法与 The League 的 OAuth 客户端相同,使用 \DtsEve\OAuth2\Client\Provider\EveOnline
作为提供者。
授权码流
$provider = new DtsEve\OAuth2\Client\Provider\EveOnline([ 'clientId' => '{eve-client-id}', 'clientSecret' => '{eve-client-key}', 'redirectUri' => 'https://example.com/callback-url', ]); // If we don't have an authorization code then get one if (!isset($_GET['code'])) { // Fetch the authorization URL from the provider; this returns the // urlAuthorize option and generates and applies any necessary parameters // (e.g. state). $authorizationUrl = $provider->getAuthorizationUrl(); // Get the state generated for you and store it to the session. $_SESSION['oauth2state'] = $provider->getState(); // Redirect the user to the authorization URL. header('Location: ' . $authorizationUrl); exit; // Check given state against previously stored one to mitigate CSRF attack } elseif (empty($_GET['state']) || (isset($_SESSION['oauth2state']) && $_GET['state'] !== $_SESSION['oauth2state'])) { if (isset($_SESSION['oauth2state'])) { unset($_SESSION['oauth2state']); } exit('Invalid state'); } else { try { // Try to get an access token using the authorization code grant. $accessToken = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // We have an access token, which we may use in authenticated // requests against the service provider's API. echo 'Access Token: ' . $accessToken->getToken() . "<br>"; echo 'Refresh Token: ' . $accessToken->getRefreshToken() . "<br>"; echo 'Expired in: ' . $accessToken->getExpires() . "<br>"; echo 'Already expired? ' . ($accessToken->hasExpired() ? 'expired' : 'not expired') . "<br>"; // Using the access token, we may look up details about the // resource owner. $resourceOwner = $provider->getResourceOwner($accessToken); var_export($resourceOwner->toArray()); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token or user details. exit($e->getMessage()); } }
刷新令牌
一旦您的应用程序被授权,您可以使用刷新令牌来刷新已过期的令牌,而不是从头开始获取新的令牌。要这样做,只需从您的数据存储中重新使用此刷新令牌来请求刷新。
$provider = new DtsEve\OAuth2\Client\Provider\EveOnline([ 'clientId' => '{eve-client-id}', 'clientSecret' => '{eve-client-key}', 'redirectUri' => 'https://example.com/callback-url', ]); $existingAccessToken = getAccessTokenFromYourDataStore(); if ($existingAccessToken->hasExpired()) { $newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]); // Purge old access token and store new access token to your data store. }
管理作用域
在创建您的 EVE Online 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => ['esi-killmails.read_killmails.v1', 'esi-killmails.read_corporation_killmails.v1'] ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果两者都没有定义,则提供者将使用内部默认值。
请使用 ESI 文档 来查找可用的作用域完整列表。
测试
$ ./vendor/bin/phpunit
贡献
请参阅 CONTRIBUTING 以获取详细信息。
致谢
许可协议
MIT 许可协议 (MIT)。请参阅 许可文件 以获取更多信息。