vendor/ssh/myjwtbundle/Controller/SecurityController.php line 30

Open in your IDE?
  1. <?php
  2. namespace SSH\MyJwtBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
  4. use Symfony\Component\Routing\Annotation\Route;
  5. use Symfony\Component\HttpFoundation\Request;
  6. use Nelmio\ApiDocBundle\Annotation\Operation;
  7. use Nelmio\ApiDocBundle\Annotation\Model;
  8. use OpenApi\Annotations as OA;
  9. use SSH\MyJwtBundle\Entity\TokenApiUser;
  10. use SSH\MyJwtBundle\Entity\ApiUser;
  11. use Symfony\Component\HttpFoundation\JsonResponse;
  12. use SSH\MyJwtBundle\Manager\ExceptionManager;
  13. use SSH\MyJwtBundle\Security\TokenAuthenticator;
  14. use SSH\MyJwtBundle\Annotations\Mapping;
  15. class SecurityController extends Controller
  16. {
  17.     /**
  18.      *
  19.      * @Route("/", name="home", methods={"GET"})
  20.      * @Route("/api", name="api_home", methods={"GET"})
  21.      * @Route("/api/doc/", name="api_doc_home", methods={"GET"})
  22.      *
  23.      *
  24.      * @param Request $request
  25.      */
  26.     public function homeAction(Request $request)
  27.     {
  28.         return $this->redirectToRoute('nelmio_api_doc.swagger_ui', ['area' => 'default']);
  29.     }
  30.     /**
  31.      * Page authenticate
  32.      *
  33.      * @Route("/tokenauthenticate", name="jwt_token_authenticate", methods={"POST"})
  34.      * @Operation(
  35.      *     tags={"Authentication"},
  36.      *     summary="Autentification to WS",
  37.      *     @OA\Parameter(
  38.      *         name="token",
  39.      *         in="query",
  40.      *         @OA\Schema(type="string"),
  41.      *         description="ws token",
  42.      *         required=true
  43.      *     ),
  44.      *     @OA\Response(
  45.      *         response="200",
  46.      *         description="Returned when successful"
  47.      *     ),
  48.      *     @OA\Response(
  49.      *         response="403",
  50.      *         description="Returned when the ws-user is not authorized"
  51.      *     ),
  52.      *     @OA\Response(
  53.      *         response="404",
  54.      *         description="Returned when the user is not found"
  55.      *     )
  56.      * )
  57.      *
  58.      */
  59.     public function tokenAuthenticate(Request $requestExceptionManager $exceptionManagerTokenAuthenticator $tokenManager)
  60.     {
  61.         $user $this->getDoctrine()
  62.                 ->getRepository(TokenApiUser::class)
  63.                 ->checkUserByToken($request->get('token'));
  64.         if ($user) {
  65.             return $tokenManager->createToken($request$user);
  66.         }
  67.         $exceptionManager->throwOtherException('FORBIDDEN_ACCESS', [], 401);
  68.     }
  69.     /**
  70.      * Page authenticate
  71.      *
  72.      * @Route("/authenticate", name="jwt_loginpassword_authenticate", methods={"POST"})
  73.      * @Mapping(object="SSH\MyJwtBundle\Request\Authentication", as="Authentication")
  74.      * @Operation(
  75.      *     tags={"Authentication"},
  76.      *     summary="Autentification to WS",
  77.      *     @OA\Parameter(
  78.      *         name="token",
  79.      *         in="query",
  80.      *         @OA\Schema(type="string"),
  81.      *         description="ws token",
  82.      *         required=true
  83.      *     ),
  84.      *     @OA\Response(
  85.      *         response="200",
  86.      *         description="Returned when successful"
  87.      *     ),
  88.      *     @OA\Response(
  89.      *         response="403",
  90.      *         description="Returned when the ws-user is not authorized"
  91.      *     ),
  92.      *     @OA\Response(
  93.      *         response="404",
  94.      *         description="Returned when the user is not found"
  95.      *     )
  96.      * )
  97.      *
  98.      */
  99.     public function authenticateAction(Request $requestExceptionManager $exceptionManagerTokenAuthenticator $tokenManager)
  100.     {
  101.         $auth = (array) $request->get('Authentication');
  102.         $user $this->getDoctrine()
  103.                 ->getRepository(ApiUser::class)
  104.                 ->findOneBy(['username' => strtolower($auth['username']),
  105. //            'roles' => 'ROLE_' . $auth['intention'],
  106.                     'password' => $auth['password']
  107.         ]);
  108.         if (!$user || ($user && !in_array('ROLE_' $auth['intention'], $user->getRoles()))) {
  109.             $exceptionManager->throwAccessDeniedException('FORBIDDEN_ACCESS');
  110.         }
  111.         $claims = [
  112.             'code' => $user->getCode(),
  113.             'roles' => $user->getRoles()
  114.         ];
  115.         $data $tokenManager->createToken($request$claims$user->getExpireIn());
  116.         $data['user'] = ['code' => $user->getCode(), 'roles' => $user->getRoles()];
  117.         return $data;
  118.     }
  119. }