src/Core/Framework/Api/Controller/AuthController.php line 34

  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Controller;
  3. use League\OAuth2\Server\AuthorizationServer;
  4. use Shopware\Core\Framework\Api\Controller\Exception\AuthThrottledException;
  5. use Shopware\Core\Framework\Log\Package;
  6. use Shopware\Core\Framework\RateLimiter\Exception\RateLimitExceededException;
  7. use Shopware\Core\Framework\RateLimiter\RateLimiter;
  8. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  9. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  10. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\Routing\Annotation\Route;
  14. #[Route(defaults: ['_routeScope' => ['api']])]
  15. #[Package('system-settings')]
  16. class AuthController extends AbstractController
  17. {
  18.     /**
  19.      * @internal
  20.      */
  21.     public function __construct(private readonly AuthorizationServer $authorizationServer, private readonly PsrHttpFactory $psrHttpFactory, private readonly RateLimiter $rateLimiter)
  22.     {
  23.     }
  24.     #[Route(path'/api/oauth/authorize'name'api.oauth.authorize'defaults: ['auth_required' => false], methods: ['POST'])]
  25.     public function authorize(Request $request): void
  26.     {
  27.     }
  28.     #[Route(path'/api/oauth/token'name'api.oauth.token'defaults: ['auth_required' => false], methods: ['POST'])]
  29.     public function token(Request $request): Response
  30.     {
  31.         $response = new Response();
  32.         try {
  33.             $cacheKey $request->get('username') . '-' $request->getClientIp();
  34.             $this->rateLimiter->ensureAccepted(RateLimiter::OAUTH$cacheKey);
  35.         } catch (RateLimitExceededException $exception) {
  36.             throw new AuthThrottledException($exception->getWaitTime(), $exception);
  37.         }
  38.         $psr7Request $this->psrHttpFactory->createRequest($request);
  39.         $psr7Response $this->psrHttpFactory->createResponse($response);
  40.         $response $this->authorizationServer->respondToAccessTokenRequest($psr7Request$psr7Response);
  41.         $this->rateLimiter->reset(RateLimiter::OAUTH$cacheKey);
  42.         return (new HttpFoundationFactory())->createResponse($response);
  43.     }
  44. }