-
Notifications
You must be signed in to change notification settings - Fork 0
/
DiscordAuth.php
79 lines (67 loc) · 2.39 KB
/
DiscordAuth.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
<?php
require_once("config/config.php");
require_once("DiscordRedirector.php");
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\ClientConfig;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\ServerConfig;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\AuthorizationCodeSession;
use EasyBib\OAuth2\Client\AuthorizationCodeGrant\Authorization\AuthorizationResponse;
use EasyBib\OAuth2\Client\Scope;
use GuzzleHttp\Client;
class DiscordAuth
{
protected $oauthSession;
protected $resource;
protected function setUpOAuth()
{
$httpClient = new Client(['base_uri' => 'https://discordapp.com']);
$redirector = new DiscordRedirector($this);
global $discordBotClientId, $discordBotClientSecret, $discordBotRedirectUri;
$clientConfig = new ClientConfig([
'client_id' => $discordBotClientId,
'client_secret' => $discordBotClientSecret,
'redirect_uri' => $discordBotRedirectUri
]);
$serverConfig = new ServerConfig([
'authorization_endpoint' => '/api/oauth2/authorize',
'token_endpoint' => '/api/oauth2/token',
]);
$this->oauthSession = new AuthorizationCodeSession(
$httpClient,
$redirector,
$clientConfig,
$serverConfig
);
$scope = new Scope(['identify', 'guilds']);
$this->oauthSession->setScope($scope);
}
public function gotoDiscord()
{
$this->setUpOAuth();
return $this->oauthSession->authorize();
}
public function handleAuthorizationResponse()
{
$this->setUpOAuth();
$authorizationResponse = new AuthorizationResponse($_GET);
$this->oauthSession->handleAuthorizationResponse($authorizationResponse);
}
private function setUpResource()
{
if ($this->resource != null) {
return $this->resource;
}
$handler = \GuzzleHttp\HandlerStack::create();
$handler->before('http_errors', function ($callable) {
return new \EasyBib\Guzzle\BearerAuthMiddleware($callable, $this->oauthSession);
});
$this->resource = new \GuzzleHttp\Client([
'base_uri' => 'https://discordapp.com',
'handler' => $handler,
]);
}
public function get($uri)
{
$this->setUpResource();
return $this->resource->get($uri)->getBody()->getContents();
}
}