vendor/shopware/core/Framework/Api/Controller/AuthController.php line 107

Open in your IDE?
  1. <?php declare(strict_types=1);
  2. namespace Shopware\Core\Framework\Api\Controller;
  3. use League\OAuth2\Server\AuthorizationServer;
  4. use OpenApi\Annotations as OA;
  5. use Shopware\Core\Framework\Api\Controller\Exception\AuthThrottledException;
  6. use Shopware\Core\Framework\RateLimiter\Exception\RateLimitExceededException;
  7. use Shopware\Core\Framework\RateLimiter\RateLimiter;
  8. use Shopware\Core\Framework\Routing\Annotation\RouteScope;
  9. use Shopware\Core\Framework\Routing\Annotation\Since;
  10. use Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory;
  11. use Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory;
  12. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
  13. use Symfony\Component\HttpFoundation\Request;
  14. use Symfony\Component\HttpFoundation\Response;
  15. use Symfony\Component\Routing\Annotation\Route;
  16. /**
  17.  * @RouteScope(scopes={"api"})
  18.  */
  19. class AuthController extends AbstractController
  20. {
  21.     private AuthorizationServer $authorizationServer;
  22.     private PsrHttpFactory $psrHttpFactory;
  23.     private RateLimiter $rateLimiter;
  24.     public function __construct(
  25.         AuthorizationServer $authorizationServer,
  26.         PsrHttpFactory $psrHttpFactory,
  27.         RateLimiter $rateLimiter
  28.     ) {
  29.         $this->authorizationServer $authorizationServer;
  30.         $this->psrHttpFactory $psrHttpFactory;
  31.         $this->rateLimiter $rateLimiter;
  32.     }
  33.     /**
  34.      * @Since("6.0.0.0")
  35.      * @Route("/api/oauth/authorize", name="api.oauth.authorize", defaults={"auth_required"=false}, methods={"POST"})
  36.      */
  37.     public function authorize(Request $request): void
  38.     {
  39.     }
  40.     /**
  41.      * @Since("6.0.0.0")
  42.      * @OA\Post(
  43.      *     path="/oauth/token",
  44.      *     summary="Fetch an access token",
  45.      *     description="Fetch a access token that can be used to perform authenticated requests",
  46.      *     operationId="token",
  47.      *     tags={"Admin API", "Authorization & Authentication"},
  48.      *     @OA\RequestBody(
  49.      *         required=true,
  50.      *         @OA\JsonContent(
  51.      *             required={
  52.      *                  "grant_type"
  53.      *             },
  54.      *             description="For more information take a look at the [Authentication documentation](https://shopware.stoplight.io/docs/admin-api/docs/concepts/authentication-authorisation.md).",
  55.      *             @OA\Property(
  56.      *                 property="grant_type",
  57.      *                 description="The grant type that should be used. See [OAuth 2.0 grant](https://oauth2.thephpleague.com/authorization-server/which-grant/) for more information.",
  58.      *                 type="string",
  59.      *                 enum={"password", "refresh_token", "client_credentials"}
  60.      *             )
  61.      *         )
  62.      *     ),
  63.      *     @OA\Response(
  64.      *         response="200",
  65.      *         description="Authorized successfully.",
  66.      *         @OA\JsonContent(
  67.      *               @OA\Property(
  68.      *                  property="token_type",
  69.      *                  description="Type of the token.",
  70.      *                  type="string"
  71.      *              ),
  72.      *              @OA\Property(
  73.      *                  property="expires_in",
  74.      *                  description="Token lifetime in seconds.",
  75.      *                  type="integer"
  76.      *              ),
  77.      *              @OA\Property(
  78.      *                  property="access_token",
  79.      *                  description="The access token that can be used for subsequent requests",
  80.      *                  type="string"
  81.      *              )
  82.      *         )
  83.      *     )
  84.      * )
  85.      * @Route("/api/oauth/token", name="api.oauth.token", defaults={"auth_required"=false}, methods={"POST"})
  86.      */
  87.     public function token(Request $request): Response
  88.     {
  89.         $response = new Response();
  90.         try {
  91.             $cacheKey $request->get('username') . '-' $request->getClientIp();
  92.             $this->rateLimiter->ensureAccepted(RateLimiter::OAUTH$cacheKey);
  93.         } catch (RateLimitExceededException $exception) {
  94.             throw new AuthThrottledException($exception->getWaitTime(), $exception);
  95.         }
  96.         $psr7Request $this->psrHttpFactory->createRequest($request);
  97.         $psr7Response $this->psrHttpFactory->createResponse($response);
  98.         $response $this->authorizationServer->respondToAccessTokenRequest($psr7Request$psr7Response);
  99.         $this->rateLimiter->reset(RateLimiter::OAUTH$cacheKey);
  100.         return (new HttpFoundationFactory())->createResponse($response);
  101.     }
  102. }