forum-oauth-center/src/Controllers/AuthorizeController.php
YUCLing a4b3a57e1d
add: serverside record models
chore: ui improvements
fix: login modal fail to show if not logged in
fix: cannot set null for nullable fields
2024-04-05 12:42:19 +08:00

67 lines
2.3 KiB
PHP

<?php
/*
* This file is part of foskym/flarum-oauth-center.
*
* Copyright (c) 2023 FoskyM.
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace RhodesIsland\OAuthCenter\Controllers;
use Flarum\Http\RequestUtil;
use RhodesIsland\OAuthCenter\Models\Record;
use RhodesIsland\OAuthCenter\OAuth;
use Illuminate\Support\Arr;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\JsonResponse;
use Flarum\Settings\SettingsRepositoryInterface;
class AuthorizeController implements RequestHandlerInterface
{
protected $settings;
public function __construct(SettingsRepositoryInterface $settings)
{
$this->settings = $settings;
}
public function handle(ServerRequestInterface $request): ResponseInterface
{
$actor = RequestUtil::getActor($request);
$actor->assertRegistered();
if (!$actor->hasPermission('rhodes-island-oauth-center.use-oauth')) {
// TODO: i18n description
return new JsonResponse([ 'error' => 'no_permission', 'error_description' => 'Don\'t have the permissions of oauth' ]);
}
$params = $request->getParsedBody();
$oauth = new OAuth($this->settings);
$server = $oauth->server();
$request = $oauth->request()::createFromGlobals();
$response = $oauth->response();
if (!$server->validateAuthorizeRequest($request, $response)) {
return new JsonResponse($response->getParameters(), $response->getStatusCode(), $response->getHttpHeaders());
}
$is_authorized = Arr::get($params, 'is_authorized', 0);
$server->handleAuthorizeRequest($request, $response, $is_authorized, $actor->id);
if ($is_authorized) {
/*Record::create([
'client_id' => Arr::get($params, 'client_id'),
'user_id' => $actor->id,
'authorized_at' => date('Y-m-d H:i:s')
]);*/
return new JsonResponse([
"redirect" => $response->getHttpHeader("Location")
]);
}
return new JsonResponse($response->getParameters(), $response->getStatusCode(), $response->getHttpHeaders());
}
}