forked from karbowiak/SluggardAuth
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdiscord.php
89 lines (78 loc) · 2.49 KB
/
discord.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
<?php
/**
* Discord OAuth v.3.0.0
* @copyright rijuthmenon.me
* @author Markis
*/
// Starting Session
session_start();
// Setting up GuzzleHttp for our requests
include('guzzle/autoload.php');
use GuzzleHttp\Client;
$http = new Client([
'base_uri' => 'https://discordapp.com',
'verify' => false,
]);
// Generate redirect url (Login with discord URL)
function url($clientid, $redirect, $scope)
{
return 'https://discordapp.com/oauth2/authorize?response_type=code&client_id=' . $clientid . '&redirect_uri=' . $redirect . '&scope=' . $scope;
}
// Get code and initialize the variables
function init($code, $redirect, $clientid, $clientsecretid)
{
$code = $_GET['code'];
$data = "grant_type=authorization_code&code=$code&redirect_uri=$redirect&client_id=$clientid&client_secret=$clientsecretid";
// Get authorization code by posting to discord's API
$response = $GLOBALS['http']->request('POST', '/api/oauth2/token', [
'form_params' => [
'client_id' => $clientid,
'client_secret' => $clientsecretid,
'grant_type' => 'authorization_code',
'code' => $code,
'redirect_uri' => $redirect,
]
]);
$responseBody1 = $response->getBody(true);
$results= json_decode($responseBody1, true);
$_SESSION['auth_token'] = $results['access_token'];
}
// Function to get users details and store them in SESSION variables (identify scope)
function get_user()
{
$response = $GLOBALS['http']->request('GET', '/api/users/@me', [
'headers' => [
'Authorization' => 'Bearer ' . $_SESSION['auth_token']
]
]);
$responseBody = $response->getBody(true);
$response = json_decode($responseBody, true);
$_SESSION['username'] = $response['username'];
$_SESSION['discrim'] = $response['discriminator'];
$_SESSION['user_id'] = $response['id'];
$_SESSION['user_avatar'] = $response['avatar'];
}
// Function to get logged in users guilds information. (guilds scope)
function get_guilds()
{
$response = $GLOBALS['http']->request('GET', '/api/users/@me/guilds', [
'headers' => [
'Authorization' => 'Bearer '.$_SESSION['auth_token']
]
]);
$responseBody = $response->getBody(true);
$response = json_decode($responseBody, true);
return $response;
}
// Function to get a single guild information Returns guilds object
function get_guild($id)
{
$response = $GLOBALS['http']->request('GET', '/api/guilds/' . $id, [
'headers' => [
'Authorization' => 'Bearer ' . $_SESSION['auth_token']
]
]);
$responseBody = $response->getBody(true);
$response = json_decode($responseBody, true);
return $response;
}