forum-oauth-center/src/Controllers/AuthorizeController.php

59 lines
2 KiB
PHP
Raw Normal View History

2023-09-29 14:47:36 +08:00
<?php
2023-09-29 14:49:12 +08:00
/*
* 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.
*/
2023-09-29 14:47:36 +08:00
namespace FoskyM\OAuthCenter\Controllers;
use Flarum\User\User;
use Flarum\Http\RequestUtil;
use FoskyM\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;
use Flarum\Group\Group;
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();
$params = $request->getParsedBody();
2023-10-01 22:10:21 +08:00
$oauth = new OAuth($this->settings);
2023-09-29 14:47:36 +08:00
$server = $oauth->server();
$request = $oauth->request()::createFromGlobals();
$response = $oauth->response();
if (!$server->validateAuthorizeRequest($request, $response)) {
2023-10-01 22:10:21 +08:00
$response->getResponseBody();
return new JsonResponse(json_decode($response->getResponseBody(), true));
2023-09-29 14:47:36 +08:00
}
2023-10-01 22:28:20 +08:00
$is_authorized = Arr::get($params, 'is_authorized', 0);
2023-09-29 14:47:36 +08:00
$server->handleAuthorizeRequest($request, $response, $is_authorized, $actor->id);
if ($is_authorized) {
// this is only here so that you get to see your code in the cURL request. Otherwise, we'd redirect back to the client
/* $code = substr($response->getHttpHeader('Location'), strpos($response->getHttpHeader('Location'), 'code=')+5, 40);
exit("SUCCESS! Authorization Code: $code");*/
}
2023-10-01 22:10:21 +08:00
$response->getResponseBody();
return new JsonResponse(json_decode($response->getResponseBody(), true));
2023-09-29 14:47:36 +08:00
}
}