forked from CRSS/Website
feat: auth
This commit is contained in:
parent
1fb549af61
commit
302677e41f
36
auth.php
Normal file
36
auth.php
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
<?php
|
||||||
|
global $discord;
|
||||||
|
|
||||||
|
require_once "_config.php";
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
|
if(isset($_GET['code'])) {
|
||||||
|
$res = $discord->validateCode($_GET['code']);
|
||||||
|
|
||||||
|
if(!$res->error) {
|
||||||
|
$_SESSION['access_token'] = $res->access_token;
|
||||||
|
$_SESSION['refresh_token'] = $res->refresh_token;
|
||||||
|
$_SESSION['expires_in'] = $res->expires_in;
|
||||||
|
|
||||||
|
$guilds = $discord->getGuilds($res->access_token);
|
||||||
|
|
||||||
|
if(!in_array('', $guilds)) {
|
||||||
|
echo json_encode(array(
|
||||||
|
'error' => true,
|
||||||
|
'error_description' => 'You are not in the CRSS guild.'
|
||||||
|
));
|
||||||
|
} else {
|
||||||
|
$_SESSION['user'] = $discord->getUser($res->access_token);
|
||||||
|
|
||||||
|
header('Location: /');
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode($res);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo json_encode(array(
|
||||||
|
'error' => true,
|
||||||
|
'error_description' => 'No code provided.'
|
||||||
|
));
|
||||||
|
}
|
|
@ -1,6 +1,10 @@
|
||||||
<?php
|
<?php
|
||||||
|
global $twig;
|
||||||
|
|
||||||
require_once '_config.php';
|
require_once '_config.php';
|
||||||
|
|
||||||
|
session_start();
|
||||||
|
|
||||||
switch ($_SERVER['REQUEST_URI']) {
|
switch ($_SERVER['REQUEST_URI']) {
|
||||||
case "/":
|
case "/":
|
||||||
echo $twig->render('index.twig', [
|
echo $twig->render('index.twig', [
|
||||||
|
|
|
@ -1,15 +0,0 @@
|
||||||
<?php
|
|
||||||
class Auth {
|
|
||||||
/**
|
|
||||||
* @var Discord
|
|
||||||
*/
|
|
||||||
private $discord;
|
|
||||||
|
|
||||||
function __construct($discord) {
|
|
||||||
$this->discord = $discord;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getLoggedInUser() {
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -2,22 +2,10 @@
|
||||||
use anlutro\cURL\cURL;
|
use anlutro\cURL\cURL;
|
||||||
|
|
||||||
class Discord {
|
class Discord {
|
||||||
/**
|
private string $client;
|
||||||
* @var string
|
private string $secret;
|
||||||
*/
|
private string $redirect;
|
||||||
private $client;
|
private cURL $curl;
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $secret;
|
|
||||||
/**
|
|
||||||
* @var string
|
|
||||||
*/
|
|
||||||
private $redirect;
|
|
||||||
/**
|
|
||||||
* @var cURL
|
|
||||||
*/
|
|
||||||
private $curl;
|
|
||||||
|
|
||||||
function __construct($client, $secret, $redirect) {
|
function __construct($client, $secret, $redirect) {
|
||||||
$this->client = $client;
|
$this->client = $client;
|
||||||
|
@ -27,10 +15,8 @@
|
||||||
$this->curl = new anlutro\cURL\cURL;
|
$this->curl = new anlutro\cURL\cURL;
|
||||||
}
|
}
|
||||||
|
|
||||||
function ValidateCode($code) {
|
function validateCode($code): array {
|
||||||
$curl = $this->curl;
|
$res = $this->curl->post('https://discord.com/api/v10/oauth2/token', [
|
||||||
|
|
||||||
$res = $curl->post('', [
|
|
||||||
'client_id' => $this->client,
|
'client_id' => $this->client,
|
||||||
'client_secret' => $this->secret,
|
'client_secret' => $this->secret,
|
||||||
'grant_type' => 'authorization_code',
|
'grant_type' => 'authorization_code',
|
||||||
|
@ -54,4 +40,45 @@
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function refreshToken($refresh_token): array {
|
||||||
|
$res = $this->curl->post('https://discord.com/api/oauth2/token', [
|
||||||
|
'client_id' => $this->client,
|
||||||
|
'client_secret' => $this->secret,
|
||||||
|
'grant_type' => 'authorization_code',
|
||||||
|
'refresh_token' => $refresh_token
|
||||||
|
]);
|
||||||
|
|
||||||
|
$json = json_decode($res->body, true);
|
||||||
|
|
||||||
|
if($json['error']) {
|
||||||
|
return array(
|
||||||
|
'error' => true,
|
||||||
|
'error_description' => $json['error_description']
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
return array(
|
||||||
|
'error' => false,
|
||||||
|
'access_token' => $json['access_token'],
|
||||||
|
'refresh_token' => $json['refresh_token'],
|
||||||
|
'expires_in' => $json['expires_in']
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getUser($token): array {
|
||||||
|
$res = $this->curl->newRequest('get', '/users/@me')
|
||||||
|
->setHeader('Authorization', 'Bearer ' . $token)
|
||||||
|
->send();
|
||||||
|
|
||||||
|
return json_decode($res->body, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getGuilds($token): array {
|
||||||
|
$res = $this->curl->newRequest('get', '/users/@me/guilds')
|
||||||
|
->setHeader('Authorization', 'Bearer ' . $token)
|
||||||
|
->send();
|
||||||
|
|
||||||
|
return json_decode($res->body, true);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue