dts-eve/oauth2-eve

EVE Online Oauth 2.0 客户端提供商,用于 PHP League OAuth2-Client

1.0.2 2021-10-31 13:52 UTC

This package is not auto-updated.

Last update: 2024-10-01 01:19:26 UTC


README

源自 https://github.com/killmails/oauth2-eve

Source Code Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

此包为 PHP League 的 OAuth 2.0 客户端 提供了 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)。请参阅 许可文件 以获取更多信息。