<?php
namespace SSH\MyJwtBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController as Controller;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\Request;
use Nelmio\ApiDocBundle\Annotation\Operation;
use Nelmio\ApiDocBundle\Annotation\Model;
use OpenApi\Annotations as OA;
use SSH\MyJwtBundle\Entity\TokenApiUser;
use SSH\MyJwtBundle\Entity\ApiUser;
use Symfony\Component\HttpFoundation\JsonResponse;
use SSH\MyJwtBundle\Manager\ExceptionManager;
use SSH\MyJwtBundle\Security\TokenAuthenticator;
use SSH\MyJwtBundle\Annotations\Mapping;
class SecurityController extends Controller
{
/**
*
* @Route("/", name="home", methods={"GET"})
* @Route("/api", name="api_home", methods={"GET"})
* @Route("/api/doc/", name="api_doc_home", methods={"GET"})
*
*
* @param Request $request
*/
public function homeAction(Request $request)
{
return $this->redirectToRoute('nelmio_api_doc.swagger_ui', ['area' => 'default']);
}
/**
* Page authenticate
*
* @Route("/tokenauthenticate", name="jwt_token_authenticate", methods={"POST"})
* @Operation(
* tags={"Authentication"},
* summary="Autentification to WS",
* @OA\Parameter(
* name="token",
* in="query",
* @OA\Schema(type="string"),
* description="ws token",
* required=true
* ),
* @OA\Response(
* response="200",
* description="Returned when successful"
* ),
* @OA\Response(
* response="403",
* description="Returned when the ws-user is not authorized"
* ),
* @OA\Response(
* response="404",
* description="Returned when the user is not found"
* )
* )
*
*/
public function tokenAuthenticate(Request $request, ExceptionManager $exceptionManager, TokenAuthenticator $tokenManager)
{
$user = $this->getDoctrine()
->getRepository(TokenApiUser::class)
->checkUserByToken($request->get('token'));
if ($user) {
return $tokenManager->createToken($request, $user);
}
$exceptionManager->throwOtherException('FORBIDDEN_ACCESS', [], 401);
}
/**
* Page authenticate
*
* @Route("/authenticate", name="jwt_loginpassword_authenticate", methods={"POST"})
* @Mapping(object="SSH\MyJwtBundle\Request\Authentication", as="Authentication")
* @Operation(
* tags={"Authentication"},
* summary="Autentification to WS",
* @OA\Parameter(
* name="token",
* in="query",
* @OA\Schema(type="string"),
* description="ws token",
* required=true
* ),
* @OA\Response(
* response="200",
* description="Returned when successful"
* ),
* @OA\Response(
* response="403",
* description="Returned when the ws-user is not authorized"
* ),
* @OA\Response(
* response="404",
* description="Returned when the user is not found"
* )
* )
*
*/
public function authenticateAction(Request $request, ExceptionManager $exceptionManager, TokenAuthenticator $tokenManager)
{
$auth = (array) $request->get('Authentication');
$user = $this->getDoctrine()
->getRepository(ApiUser::class)
->findOneBy(['username' => strtolower($auth['username']),
// 'roles' => 'ROLE_' . $auth['intention'],
'password' => $auth['password']
]);
if (!$user || ($user && !in_array('ROLE_' . $auth['intention'], $user->getRoles()))) {
$exceptionManager->throwAccessDeniedException('FORBIDDEN_ACCESS');
}
$claims = [
'code' => $user->getCode(),
'roles' => $user->getRoles()
];
$data = $tokenManager->createToken($request, $claims, $user->getExpireIn());
$data['user'] = ['code' => $user->getCode(), 'roles' => $user->getRoles()];
return $data;
}
}