Authorize.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. /**
  20. * OAuth2 implementation for GNU social
  21. *
  22. * @package OAuth2
  23. * @category API
  24. *
  25. * @author Diogo Peralta Cordeiro <mail@diogo.site>
  26. * @author Hugo Sales <hugo@hsal.es>
  27. * @copyright 2022 Free Software Foundation, Inc http://www.fsf.org
  28. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  29. */
  30. namespace Plugin\OAuth2\Controller;
  31. use App\Core\Controller;
  32. use App\Util\Common;
  33. use App\Util\Exception\NotFoundException;
  34. use League\OAuth2\Server\Entities\UserEntityInterface;
  35. use League\OAuth2\Server\Exception\OAuthServerException;
  36. use Nyholm\Psr7\Factory\Psr17Factory;
  37. use Plugin\OAuth2\OAuth2;
  38. use Psr\Http\Message\ResponseFactoryInterface;
  39. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  40. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  41. use Symfony\Component\HttpFoundation\JsonResponse;
  42. use Symfony\Component\HttpFoundation\Request;
  43. use Symfony\Component\HttpFoundation\RequestStack;
  44. class Authorize extends Controller
  45. {
  46. public function __construct(
  47. RequestStack $stack,
  48. private ResponseFactoryInterface $response_factory,
  49. ) {
  50. parent::__construct($stack);
  51. }
  52. public function handle(Request $request)
  53. {
  54. // @var \League\OAuth2\Server\AuthorizationServer $server
  55. $server = OAuth2::$authorization_server;
  56. $response = $this->response_factory->createResponse();
  57. $httpFoundationFactory = new HttpFoundationFactory;
  58. try {
  59. // Validate the HTTP request and return an AuthorizationRequest object.
  60. // The auth request object can be serialized into a user's session
  61. $psr17Factory = new Psr17Factory();
  62. $psrHttpFactory = new PsrHttpFactory($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
  63. $psrRequest = $psrHttpFactory->createRequest($request);
  64. $authRequest = $server->validateAuthorizationRequest($psrRequest);
  65. $user = Common::ensureLoggedIn($request);
  66. // Once the user has logged in set the user on the AuthorizationRequest
  67. $authRequest->setUser(
  68. new class($user->getId()) implements UserEntityInterface {
  69. public function __construct(private int $id)
  70. {
  71. }
  72. public function getIdentifier(): int
  73. {
  74. return $this->id;
  75. }
  76. },
  77. );
  78. // Once the user has approved or denied the client update the status
  79. // (true = approved, false = denied)
  80. $authRequest->setAuthorizationApproved(true);
  81. // Return the HTTP redirect response
  82. return $httpFoundationFactory->createResponse($server->completeAuthorizationRequest($authRequest, $response));
  83. } catch (OAuthServerException $exception) {
  84. return $httpFoundationFactory->createResponse($exception->generateHttpResponse($response));
  85. } catch (NotFoundException) {
  86. return new JsonResponse(['error' => 'No such client', 'error_description' => 'This client is not registered']);
  87. }
  88. }
  89. }