wohali / oauth2-discord-new
为 PHP League OAuth2-Client 提供的 Discord OAuth 2.0 客户端提供者
1.2.1
2022-12-29 18:45 UTC
Requires
- php: ^7.2|^8.0
- ext-json: *
- league/oauth2-client: ^2.0
Requires (Dev)
- mockery/mockery: ~1.3.0
- php-parallel-lint/php-parallel-lint: ~0.9
- phpunit/phpunit: ~8.0
- squizlabs/php_codesniffer: ^2.0
Conflicts
- team-reflex/oauth2-discord: >=1.0
README
此包为 PHP League 的 OAuth 2.0 客户端(v2.0 及以上版本)提供了 Discord OAuth 2.0 支持。
要求
以下版本的 PHP 受支持。
- PHP 7.2
- PHP 7.3
- PHP 7.4
- PHP 8.0
- PHP 8.1
- PHP 8.2
安装
要安装,请使用 composer
$ composer require wohali/oauth2-discord-new
用法
用法与 The League 的 OAuth 客户端相同,使用 \Wohali\OAuth2\Client\Provider\Discord
作为提供者。
示例授权代码流
以下自包含示例
- 获取授权代码
- 使用提供的授权代码获取访问令牌
- 使用提供的访问令牌查找用户的配置文件
您可以通过 注册 Discord 应用 并将重定向 URI 设置为您的服务器上的此示例脚本来尝试此脚本。然后,将 Discord 应用的客户端 ID 和密钥以及相同的 URI 放入脚本顶部的设置中。
<?php require __DIR__ . '/vendor/autoload.php'; session_start(); echo ('Main screen turn on!<br/><br/>'); $provider = new \Wohali\OAuth2\Client\Provider\Discord([ 'clientId' => '{discord-client-id}', 'clientSecret' => '{discord-client-secret}', 'redirectUri' => '{your-server-uri-to-this-script-here}' ]); if (!isset($_GET['code'])) { // Step 1. Get authorization code $authUrl = $provider->getAuthorizationUrl(); $_SESSION['oauth2state'] = $provider->getState(); header('Location: ' . $authUrl); // 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 { // Step 2. Get an access token using the provided authorization code $token = $provider->getAccessToken('authorization_code', [ 'code' => $_GET['code'] ]); // Show some token details echo '<h2>Token details:</h2>'; echo 'Token: ' . $token->getToken() . "<br/>"; echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>"; echo 'Expires: ' . $token->getExpires() . " - "; echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>"; // Step 3. (Optional) Look up the user's profile with the provided token try { $user = $provider->getResourceOwner($token); echo '<h2>Resource owner details:</h2>'; printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator()); var_export($user->toArray()); } catch (Exception $e) { // Failed to get user details exit('Oh dear...'); } }
管理作用域
在第一步创建您的 Discord 授权 URL 时,您可以指定应用程序可能授权的状态和作用域。
$options = [ 'state' => 'OPTIONAL_CUSTOM_CONFIGURED_STATE', 'scope' => ['identify', 'email', '...'] // array or string ]; $authorizationUrl = $provider->getAuthorizationUrl($options);
如果两者都没有定义,则提供者将使用内部默认值。
编写此文档时,以下作用域可用
- bot
- connections
- identify
- guilds
- guilds.join
- gdm.join
- messages.read
- rpc
- rpc.api
- rpc.notifications.read
- webhook.incoming
刷新令牌
您可以使用刷新令牌而不是通过获取全新的令牌的整个过程来刷新过期的令牌。要做到这一点,只需从您的数据存储中重新使用新鲜令牌来请求刷新即可。
// create $provider as in the initial example $existingAccessToken = getAccessTokenFromYourDataStore(); if ($existingAccessToken->hasExpired()) { $newAccessToken = $provider->getAccessToken('refresh_token', [ 'refresh_token' => $existingAccessToken->getRefreshToken() ]); // Purge old access token and store new access token to your data store. }
客户端凭证授权
Discord 为机器人开发者提供了一个客户端凭证流,以便他们可以获取用于测试目的的自己的承载令牌。这返回了机器人所有者的访问令牌。
// create $provider as in the initial example 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()); }
机器人授权
要授权机器人,请指定作用域为 bot
并适当设置 权限。
// create $provider as in the initial example $options = [ 'scope' => ['bot'], 'permissions' => 1 ]; $authorizationUrl = $provider->getAuthorizationUrl($options); // Redirect user to authorization page header('Location: ' . $authUrl);
测试
$ ./vendor/bin/parallel-lint src test
$ ./vendor/bin/phpcs src --standard=psr2 -sp
$ ./vendor/bin/phpunit --coverage-text
贡献
有关详细信息,请参阅 CONTRIBUTING。
鸣谢
许可证
MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件。