72 lines
2.0 KiB
PHP
72 lines
2.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace mirzaev\marina\controllers;
|
|
|
|
// Файлы проекта
|
|
use mirzaev\marina\controllers\core;
|
|
|
|
// Встроенные библиотеки
|
|
use exception;
|
|
|
|
/**
|
|
* Контроллер головного управления
|
|
*
|
|
* @package mirzaev\marina\controllers
|
|
* @author Arsen Mirzaev Tatyano-Muradovich <arsen@mirzaev.sexy>
|
|
*/
|
|
final class index extends core
|
|
{
|
|
/**
|
|
* Главная страница
|
|
*
|
|
* @param array $parameters Параметры запроса
|
|
*/
|
|
public function index(array $parameters = []): ?string
|
|
{
|
|
if (!isset($parameters['code'])) {
|
|
|
|
// Step 1. Get authorization code
|
|
$_SESSION['oauth2state'] = $this->provider->getState();
|
|
header('Location: ' . $this->provider->getAuthorizationUrl(require '../settings/discord/settings.php'));
|
|
|
|
// Check given state against previously stored one to mitigate CSRF attack
|
|
} elseif (empty($parameters['state']) || ($parameters['state'] !== $_SESSION['oauth2state'])) {
|
|
|
|
unset($_SESSION['oauth2state']);
|
|
exit('Invalid state');
|
|
} else {
|
|
|
|
// Step 2. Get an access token using the provided authorization code
|
|
$token = $this->provider->getAccessToken('authorization_code', [
|
|
'code' => $parameters['code']
|
|
]);
|
|
|
|
// Show some token details
|
|
echo '<h2>Token details:</h2>';
|
|
echo 'Token: ' . $token->getToken() . "<br/>";
|
|
echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>";
|
|
echo 'Expires: ' . $token->getExpires() . " - ";
|
|
echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>";
|
|
|
|
// Step 3. (Optional) Look up the user's profile with the provided token
|
|
try {
|
|
|
|
$user = $this->provider->getResourceOwner($token);
|
|
|
|
echo '<h2>Resource owner details:</h2>';
|
|
printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator());
|
|
var_export($user->toArray());
|
|
} catch (exception $e) {
|
|
|
|
// Failed to get user details
|
|
exit('fail');
|
|
}
|
|
}
|
|
|
|
// Возврат (провал)
|
|
return null;
|
|
}
|
|
}
|