concrete5/oauth2-concrete5

此包已被弃用,不再维护。作者建议使用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: 2023-10-10 18:41:30 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获取更多信息。