ahoicloud/oauth2-client

Ahoi OAuth 2.0 客户端提供商,用于 PHP League OAuth2-Client

1.1.1 2017-12-28 10:03 UTC

README

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

本包为 PHP League OAuth 2.0 Client 提供了 Ahoi OAuth 2.0 支持。OAuth 2.0 Client

安装

使用 composer 安装

composer require ahoicloud/oauth2-client

使用方法

使用方法和 The League 的 OAuth 客户端相同,使用 \FVJM\OAuth2\Client\Provider\Ahoi 作为提供者。

授权码流程

$provider = new FVJM\OAuth2\Client\Provider\Ahoi([
    'clientId'          => '{ahoi-client-id}',
    'clientSecret'      => '{ahoi-client-secret}',
    'ahoiInstanceUrl'      => '{ahoi-instance-url}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

if (!isset($_GET['code'])) {

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl();
    $_SESSION['oauth2state'] = $provider->getState();
    header('Location: '.$authUrl);
    exit;

// Check given state against previously stored one to mitigate CSRF attack
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

    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 user's details
        $user = $provider->getResourceOwner($token);

        // Use these details to create a new profile
        printf('Hello %s!', $user->getFirstname());

    } catch (Exception $e) {

        // Failed to get user details
        exit('Oh dear...');
    }

    // Use this to interact with an API on the users behalf
    echo $token->getToken();
}

管理作用域

在创建 Ahoi 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。

$options = [
    'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE',
    'scope' => ['profile','offline_acccess'] // array or string
];

$authorizationUrl = $provider->getAuthorizationUrl($options);

如果两者都没有定义,则提供者将使用内部默认值。

撰写本文档时,以下作用域可用。

  • profile
  • offline_acccess

刷新令牌

$provider = new FVJM\OAuth2\Client\Provider\Ahoi([
    'clientId'          => '{ahoi-client-id}',
    'clientSecret'      => '{ahoi-client-secret}',
    'ahoiInstanceUrl'      => '{ahoi-instance-url}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

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

客户端凭据授权

当您的应用程序代表自己访问它控制/拥有的服务提供商中的资源时,它可以使用客户端凭据授权类型。这最好用于您的应用程序凭据是私有的并且从未向最终用户公开(例如,通过网页浏览器等)的情况。此授权类型的功能类似于资源所有者密码凭据授权类型,但它不请求用户的用户名或密码。它仅使用服务提供商分配给您的客户端的客户端 ID 和密钥。

$provider = new FVJM\OAuth2\Client\Provider\Ahoi([
    'clientId'          => '{ahoi-client-id}',
    'clientSecret'      => '{ahoi-client-secret}',
    'ahoiInstanceUrl'      => '{ahoi-instance-url}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

try {

    // Try to get an access token using the client credentials grant.
    $accessToken = $provider->getAccessToken('client_credentials');

} catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {

    // Failed to get the access token
    exit($e->getMessage());

}

测试

$ ./vendor/bin/phpunit

致谢

许可证

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