feat: auth

This commit is contained in:
TheClashFruit 2023-09-15 17:10:03 +02:00
parent 1fb549af61
commit 302677e41f
Signed by: TheClashFruit
GPG key ID: CF4A319B9A73290C
4 changed files with 87 additions and 35 deletions

36
auth.php Normal file
View 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.'
));
}

View file

@ -1,5 +1,9 @@
<?php
global $twig;
require_once '_config.php';
session_start();
switch ($_SERVER['REQUEST_URI']) {
case "/":

View file

@ -1,15 +0,0 @@
<?php
class Auth {
/**
* @var Discord
*/
private $discord;
function __construct($discord) {
$this->discord = $discord;
}
function getLoggedInUser() {
}
}

View file

@ -2,22 +2,10 @@
use anlutro\cURL\cURL;
class Discord {
/**
* @var string
*/
private $client;
/**
* @var string
*/
private $secret;
/**
* @var string
*/
private $redirect;
/**
* @var cURL
*/
private $curl;
private string $client;
private string $secret;
private string $redirect;
private cURL $curl;
function __construct($client, $secret, $redirect) {
$this->client = $client;
@ -27,10 +15,8 @@
$this->curl = new anlutro\cURL\cURL;
}
function ValidateCode($code) {
$curl = $this->curl;
$res = $curl->post('', [
function validateCode($code): array {
$res = $this->curl->post('https://discord.com/api/v10/oauth2/token', [
'client_id' => $this->client,
'client_secret' => $this->secret,
'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);
}
}