concretecms/oauth2-concretecms

PHP League OAuth2-Client的concrete5 OAuth2客户端提供者

1.0.1 2019-02-19 16:48 UTC

This package is auto-updated.

Last update: 2024-09-10 20:45:20 UTC


README

此包为PHP League的OAuth 2.0客户端提供concrete5 OAuth 2.0支持。

安装

要安装,请使用composer

composer require concrete5/oauth2-concrete5

要求

您必须在concrete5网站上运行8.5.0或更高版本的concrete5。此外,您必须在控制台>系统>API>集成页面上创建一个API集成,并从标准用户同意创建。此集成将创建一个客户端ID和一个客户端密钥,您将在下面使用。

用法

用法与The League的OAuth客户端相同,使用\Concrete\OAuth2\Client\Provider\Concrete5作为提供者。

授权码流

$provider = new Concrete\OAuth2\Client\Provider\Concrete5([
    'clientId'          => '{integrationClientId}',
    'clientSecret'      => '{integrationClientSecret}',
    'redirectUri'       => 'https://example.com/callback-url'
]);

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

    $url = $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->getUserName());

    } 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();
}

测试

$ ./vendor/bin/phpunit

致谢

许可证

MIT许可证(MIT)。有关更多信息,请参阅LICENSE.TXT。