1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the wohali/oauth2-discord-new library |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
* |
8
|
|
|
* @copyright Copyright (c) Joan Touzet <[email protected]> |
9
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
10
|
|
|
* @link https://packagist.org/packages/wohali/oauth2-discord-new Packagist |
11
|
|
|
* @link https://github.com/wohali/oauth2-discord-new GitHub |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Wohali\OAuth2\Client\Provider; |
15
|
|
|
|
16
|
|
|
use League\OAuth2\Client\Provider\Exception\DiscordIdentityProviderException; |
17
|
|
|
use League\OAuth2\Client\Token\AccessToken; |
18
|
|
|
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
class Discord extends AbstractProvider |
22
|
|
|
{ |
23
|
|
|
use BearerAuthorizationTrait; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* API Domain |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
public $apiDomain = 'https://discordapp.com/api/v6'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Get authorization URL to begin OAuth flow |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
9 |
|
public function getBaseAuthorizationUrl() |
38
|
|
|
{ |
39
|
9 |
|
return $this->apiDomain.'/oauth2/authorize'; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Get access token URL to retrieve token |
44
|
|
|
* |
45
|
|
|
* @param array $params |
46
|
|
|
* |
47
|
|
|
* @return string |
48
|
|
|
*/ |
49
|
12 |
|
public function getBaseAccessTokenUrl(array $params) |
50
|
|
|
{ |
51
|
12 |
|
return $this->apiDomain.'/oauth2/token'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get provider URL to retrieve user details |
56
|
|
|
* |
57
|
|
|
* @param AccessToken $token |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
3 |
|
public function getResourceOwnerDetailsUrl(AccessToken $token) |
62
|
|
|
{ |
63
|
3 |
|
return $this->apiDomain.'/users/@me'; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the string that should be used to separate scopes when building |
68
|
|
|
* the URL for requesting an access token. |
69
|
|
|
* |
70
|
|
|
* Discord's scope separator is space (%20) |
71
|
|
|
* |
72
|
|
|
* @return string Scope separator |
73
|
|
|
*/ |
74
|
9 |
|
protected function getScopeSeparator() |
75
|
|
|
{ |
76
|
9 |
|
return ' '; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Get the default scopes used by this provider. |
81
|
|
|
* |
82
|
|
|
* This should not be a complete list of all scopes, but the minimum |
83
|
|
|
* required for the provider user interface! |
84
|
|
|
* |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
6 |
|
protected function getDefaultScopes() |
88
|
|
|
{ |
89
|
|
|
return [ |
90
|
6 |
|
'identify', |
91
|
2 |
|
'email', |
92
|
2 |
|
'connections', |
93
|
2 |
|
'guilds', |
94
|
|
|
'guilds.join' |
95
|
2 |
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Check a provider response for errors. |
100
|
|
|
* |
101
|
|
|
* @throws IdentityProviderException |
102
|
|
|
* @param ResponseInterface @response |
103
|
|
|
* @param array $data Parsed response data |
104
|
|
|
* @return void |
105
|
|
|
*/ |
106
|
9 |
|
protected function checkResponse(ResponseInterface $response, $data) |
107
|
|
|
{ |
108
|
9 |
|
if ($response->getStatusCode() >= 400) { |
109
|
3 |
|
throw DiscordIdentityProviderException::clientException($response, $data); |
110
|
|
|
} |
111
|
6 |
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Generate a user object from a successful user details request. |
115
|
|
|
* |
116
|
|
|
* @param array $response |
117
|
|
|
* @param AccessToken $token |
118
|
|
|
* @return \League\OAuth2\Client\Provider\ResourceOwnerInterface |
119
|
|
|
*/ |
120
|
3 |
|
protected function createResourceOwner(array $response, AccessToken $token) |
121
|
|
|
{ |
122
|
3 |
|
return new DiscordResourceOwner($response); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|