1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class Discord |
4
|
|
|
* |
5
|
|
|
* @created 22.10.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
12
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
14
|
|
|
use chillerlan\OAuth\Core\{ClientCredentials, CSRFToken, OAuth2Provider, ProviderException, TokenRefresh}; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://discordapp.com/developers/docs/topics/oauth2 |
20
|
|
|
*/ |
21
|
|
|
class Discord extends OAuth2Provider implements ClientCredentials, CSRFToken, TokenRefresh{ |
22
|
|
|
|
23
|
|
|
public const SCOPE_BOT = 'bot'; |
24
|
|
|
public const SCOPE_CONNECTIONS = 'connections'; |
25
|
|
|
public const SCOPE_EMAIL = 'email'; |
26
|
|
|
public const SCOPE_IDENTIFY = 'identify'; |
27
|
|
|
public const SCOPE_GUILDS = 'guilds'; |
28
|
|
|
public const SCOPE_GUILDS_JOIN = 'guilds.join'; |
29
|
|
|
public const SCOPE_GDM_JOIN = 'gdm.join'; |
30
|
|
|
public const SCOPE_MESSAGES_READ = 'messages.read'; |
31
|
|
|
public const SCOPE_RPC = 'rpc'; |
32
|
|
|
public const SCOPE_RPC_API = 'rpc.api'; |
33
|
|
|
public const SCOPE_RPC_NOTIFICATIONS_READ = 'rpc.notifications.read'; |
34
|
|
|
public const SCOPE_WEBHOOK_INCOMING = 'webhook.incoming'; |
35
|
|
|
|
36
|
|
|
protected string $authURL = 'https://discordapp.com/api/oauth2/authorize'; |
37
|
|
|
protected string $accessTokenURL = 'https://discordapp.com/api/oauth2/token'; |
38
|
|
|
protected string $apiURL = 'https://discordapp.com/api/v9'; |
39
|
|
|
protected ?string $revokeURL = 'https://discordapp.com/api/oauth2/token/revoke'; |
40
|
|
|
protected ?string $apiDocs = 'https://discordapp.com/developers/'; |
41
|
|
|
protected ?string $applicationURL = 'https://discordapp.com/developers/applications/'; |
42
|
|
|
|
43
|
|
|
protected array $defaultScopes = [ |
44
|
|
|
self::SCOPE_CONNECTIONS, |
45
|
|
|
self::SCOPE_EMAIL, |
46
|
|
|
self::SCOPE_IDENTIFY, |
47
|
|
|
self::SCOPE_GUILDS, |
48
|
|
|
self::SCOPE_GUILDS_JOIN, |
49
|
|
|
self::SCOPE_GDM_JOIN, |
50
|
|
|
self::SCOPE_MESSAGES_READ, |
51
|
|
|
]; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @inheritDoc |
55
|
|
|
*/ |
56
|
|
|
public function me():ResponseInterface{ |
57
|
|
|
$response = $this->request('/users/@me'); |
58
|
|
|
$status = $response->getStatusCode(); |
59
|
|
|
|
60
|
|
|
if($status === 200){ |
61
|
|
|
return $response; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$json = MessageUtil::decodeJSON($response); |
65
|
|
|
|
66
|
|
|
if(isset($json->message)){ |
67
|
|
|
throw new ProviderException($json->message); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
} |
74
|
|
|
|