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

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