joséayram/oauth2-lichess

Lichess 对 PHP League 的 OAuth 2.0 客户端的 OAuth 2.0 支持

0.1.1 2022-04-10 18:51 UTC

This package is auto-updated.

Last update: 2024-09-11 00:27:11 UTC


README

Codacy Badge Scrutinizer Code Quality Code Coverage Build Status Software License

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

安装

要安装,请使用 composer

$ composer require joseayram/oauth2-lichess

用法

用法与 The League 的 OAuth 客户端相同,使用 \CrudSys\OAuth2\Client\Provider\Lichess 作为提供商。

授权码流

require_once 'oauth2-lichess/vendor/autoload.php';

session_start();

use CrudSys\OAuth2\Client\Provider\Lichess;

$clientId = 'api-lichess-test';
$clientSecret = '{your-secret-client}';
$redirectUri = '{your-redirect-uri}';

if (!isset($_SESSION['codeVerifier'])) {
    $verifier = createVerifier();
    $_SESSION['codeVerifier'] = $verifier;
} else {
    $verifier = $_SESSION['codeVerifier'];
}

$provider = new Lichess([
    'clientId'      => $clientId,
    'clientSecret'  => $clientSecret,
    'redirectUri'   => $redirectUri,
]);

if (!isset($_GET['code']) && !isset($_GET['error'])) {
    $challenge = createChallenge($verifier);

    // If we don't have an authorization code then get one
    $authUrl = $provider->getAuthorizationUrl([
        'code_challenge' => $challenge,
    ]);

    $_SESSION['oauth2state'] = $provider->getState();

    echo "<a href='{$authUrl}'>Login with Lichess</a>";
} elseif ((isset($_GET['error']) && !empty($_GET['error']) ) &&
        (isset($_GET['error_description']) && !empty($_GET['error_description']) )
) {
    unset($_SESSION['oauth2state']);
    exit($_GET['error'].': '.$_GET['error_description']);
} elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
    unset($_SESSION['oauth2state']);
    exit('Invalid state, make sure HTTP sessions are enabled.');
} else {
    // Try to get an access token (using the authorization code grant)
    try {
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code'],
            'code_verifier' => $_SESSION['codeVerifier'],
        ]);
    } catch (\Exception $e) {
        exit('Failed to get access token: '.$e->getMessage());
    }

    // 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!\n<br>', $user->getUsername());
        echo "<pre>" . print_r($user, true) . "</pre>";
    } catch (\Exception $e) {
        exit('Failed to get resource owner: '.$e->getMessage());
    }

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

管理作用域

您可以通过传递它们到 getAuthorizationUrl() 方法来添加额外的范围

$options = [
    'scope' => [Lichess::SCOPE_EMAIL, Lichess::SCOPE_PREFERENCE_READ]
];

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

如果没有传递作用域,则仅使用 public

在本文档编写时,以下作用域可用:请参阅

  • PREFERENCE_READ 读取您的偏好设置。
  • PREFERENCE_WRITE 编写您的偏好设置。
  • EMAIL 读取您的电子邮件地址。
  • CHALLENGE_READ 读取传入的挑战。
  • CHALLENGE_WRITE 创建、接受、拒绝挑战。
  • CHALLENGE_BULK 创建、删除、查询大量配对。

测试

$ ./vendor/bin/phpunit

变更日志

请参阅我们的 变更日志文件 以获取详细信息。

鸣谢

特别感谢所有其他 oauth2 客户端第三方包 的创建者,我从他们那里学到了很多。

贡献

请参阅我们的 贡献指南 以获取详细信息。

许可证

MIT 许可证 (MIT)。请参阅 许可证文件 以获取更多信息。