-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathProvider.php
115 lines (95 loc) · 2.74 KB
/
Provider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace SocialiteProviders\Discord;
use GuzzleHttp\RequestOptions;
use SocialiteProviders\Manager\OAuth2\AbstractProvider;
use SocialiteProviders\Manager\OAuth2\User;
class Provider extends AbstractProvider
{
public const IDENTIFIER = 'DISCORD';
protected $scopes = [
'identify',
'email',
];
/**
* {@inheritdoc}
*/
protected $consent = false;
protected $scopeSeparator = ' ';
protected function getAuthUrl($state): string
{
return $this->buildAuthUrlFromBase('https://discord.com/api/oauth2/authorize', $state);
}
/**
* {@inheritdoc}
*/
protected function getCodeFields($state = null)
{
$fields = parent::getCodeFields($state);
if (! $this->consent) {
$fields['prompt'] = 'none';
}
return $fields;
}
/**
* Prompt for consent each time or not.
*
* @return $this
*/
public function withConsent()
{
$this->consent = true;
return $this;
}
protected function getTokenUrl(): string
{
return 'https://discord.com/api/oauth2/token';
}
/**
* {@inheritdoc}
*/
protected function getUserByToken($token)
{
$response = $this->getHttpClient()->get(
'https://discord.com/api/users/@me',
[
RequestOptions::HEADERS => [
'Authorization' => 'Bearer '.$token,
],
]
);
return json_decode((string) $response->getBody(), true);
}
/**
* @param array $user
* @return string|null
*
* @see https://discord.com/developers/docs/reference#image-formatting-cdn-endpoints
*/
protected function formatAvatar(array $user)
{
if (empty($user['avatar'])) {
return null;
}
$isGif = preg_match('/a_.+/m', $user['avatar']) === 1;
$extension = $this->getConfig('allow_gif_avatars', true) && $isGif ? 'gif' :
$this->getConfig('avatar_default_extension', 'png');
return sprintf('https://cdn.discordapp.com/avatars/%s/%s.%s', $user['id'], $user['avatar'], $extension);
}
/**
* {@inheritdoc}
*/
protected function mapUserToObject(array $user)
{
return (new User)->setRaw($user)->map([
'id' => $user['id'],
'nickname' => $user['username'].($user['discriminator'] !== '0' ? '#'.$user['discriminator'] : ''),
'name' => $user['username'],
'email' => $user['email'] ?? null,
'avatar' => $this->formatAvatar($user),
]);
}
public static function additionalConfigKeys(): array
{
return ['allow_gif_avatars', 'avatar_default_extension'];
}
}