creatorshub/oauth2-creatorshub

为 PHP League OAuth2-Client 提供的 CreatorsHub OAuth 2.0 客户端提供者

v1.1 2018-07-03 20:21 UTC

This package is auto-updated.

Last update: 2024-08-29 04:25:44 UTC


README

本软件包为 PHP League 的 OAuth 2.0 客户端 提供了 CreatorsHub OAuth 2.0 支持。

本软件包遵循 PSR-1PSR-2PSR-4 标准。如果发现不符合标准的地方,请通过 pull request 提交补丁。

要求

支持以下版本的 PHP。

  • PHP 7.0
  • PHP 7.1
  • HHVM

安装

要安装,请使用 composer

composer require creatorshub/oauth2-creatorshub

使用方法

授权码流

$provider = new CreatorsHub\OAuth2\Client\Provider\CreatorsHub([
    'clientId'     => '{creatorshub-client-id}',
    'clientSecret' => '{creatorshub-client-secret}',
    '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());

    }

}

刷新令牌

刷新令牌仅提供给请求离线访问权限的应用程序。您可以通过设置提供者中的 accessType 选项来指定离线访问

$provider = new CreatorsHub\OAuth2\Client\Provider\CreatorsHub([
    'clientId'     => '{creatorshub-client-id}',
    'clientSecret' => '{creatorshub-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']);

现在您已经拥有了使用刷新令牌刷新访问令牌所需的一切

$provider = new CreatorsHub\OAuth2\Client\Provider\CreatorsHub([
    'clientId'     => '{creatorshub-app-id}',
    'clientSecret' => '{creatorshub-app-secret}',
    'redirectUri'  => 'https://example.com/callback-url',
]);

$grant = new League\OAuth2\Client\Grant\RefreshToken();
$token = $provider->getAccessToken($grant, ['refresh_token' => $refreshToken]);

作用域

如果需要,您可以在获取授权 URL 时包含作用域数组。示例

$authorizationUrl = $provider->getAuthorizationUrl([
    'scope' => [
        'some scope',
        'another scope',
    ]
]);
header('Location: ' . $authorizationUrl);
exit;

测试

$ ./vendor/bin/phpunit

贡献

有关详细信息,请参阅 CONTRIBUTING

致谢

许可协议

MIT 许可协议 (MIT)。有关更多信息,请参阅 许可文件