/**
  * {@inheritdoc}
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     if ($request->isXmlHttpRequest()) {
         return new JsonResponse(['success' => false, 'message' => $exception->getMessageKey()], 401);
     }
     return parent::onAuthenticationFailure($request, $exception);
 }
Пример #2
0
 public function start(Request $request, AuthenticationException $authException = null)
 {
     $apiProblem = new ApiProblem(Response::HTTP_UNAUTHORIZED);
     $message = $authException ? $authException->getMessageKey() : 'Missing credentials';
     $apiProblem->set('detail', $message);
     return $this->responseFactory->createResponse($apiProblem);
 }
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     return new JsonResponse(
         // you could translate the message
         array('message' => $exception->getMessageKey()),
         403
     );
 }
Пример #4
0
 /**
  * NOTE: I chose to throw an HTTP Exception here to let the response be rendered elsewhere -
  *       separation of concerns and all... You could always return a JsonResponse here.
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     $message = 'Invalid Credentials';
     if ($exception instanceof CustomUserMessageAuthenticationException) {
         $message = $exception->getMessageKey();
     }
     throw new HttpException(401, $message);
 }
Пример #5
0
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     if ($request->isXmlHttpRequest()) {
         if ($this->env != 'dev') {
             $msg = $this->translator->trans($exception->getMessageKey(), $exception->getMessageData(), 'security');
         } else {
             $msg = $exception->getMessage();
         }
         return new JsonResponse($msg, Response::HTTP_UNAUTHORIZED);
     }
     return parent::onAuthenticationFailure($request, $exception);
 }
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     if ($request->isXmlHttpRequest()) {
         $message = $exception->getMessageKey();
         $messageTrans = $this->translator->trans($message, array(), 'FOSUserBundle');
         if ($messageTrans === $message) {
             $messageTrans = $this->translator->trans($message, array(), 'security');
         }
         $data = array('message' => $messageTrans);
         $response = new \Symfony\Component\HttpFoundation\JsonResponse($data, 400);
         return $response;
     } else {
         return parent::onAuthenticationFailure($request, $exception);
     }
 }
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     // TODO: Implement onAuthenticationFailure() method.
     return new JsonResponse(array('message' => $exception->getMessageKey()), 403);
 }
 function it_returns_json_response_if_request_is_xml_based(Request $request, AuthenticationException $authenticationException)
 {
     $request->isXmlHttpRequest()->willReturn(true);
     $authenticationException->getMessageKey()->willReturn('Invalid credentials.');
     $this->onAuthenticationFailure($request, $authenticationException)->shouldHaveType(JsonResponse::class);
 }
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     throw new \Exception();
     return new Response(strtr($exception->getMessageKey(), $exception->getMessageData()), 403);
 }
 public function getMessageKey()
 {
     return $this->messageKey !== null ? $this->messageKey : parent::getMessageKey();
 }
 /**
  * Create a proper json response containing the error.
  *
  * @param AuthenticationException $authException The exception that started the authentication process.
  *
  * @return JsonResponse
  */
 private function createResponseFromException(AuthenticationException $authException = null)
 {
     $data = ['status' => 'unauthorized'];
     if ($authException) {
         $data['message'] = $authException->getMessageKey();
     }
     return new JsonResponse($data, JsonResponse::HTTP_UNAUTHORIZED);
 }
 /**
  * Called when authentication executed, but failed (e.g. wrong username password).
  *
  * This should return the Response sent back to the user, like a
  * RedirectResponse to the login page or a 403 response.
  *
  * If you return null, the request will continue, but the user will
  * not be authenticated. This is probably not what you want to do.
  *
  * @param Request $request
  * @param AuthenticationException $exception
  *
  * @return Response|null
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     return new JsonResponse(['message' => $this->translator->trans($exception->getMessageKey(), $exception->getMessageData())], 403);
 }
Пример #13
0
 /**
  * This is called when an interactive authentication attempt fails. This is
  * called by authentication listeners inheriting from
  * AbstractAuthenticationListener.
  *
  * @param Request $request
  * @param AuthenticationException $exception
  *
  * @return Response The response to return, never null
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     $data = array('code' => self::RESPONSE_FAILURE_CODE, 'message' => strtr($exception->getMessageKey(), $exception->getMessageData()));
     return new JsonResponse($data, self::RESPONSE_FAILURE_CODE);
 }
Пример #14
0
 /**
  * @inheritDoc
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     return new JsonResponse(['message' => $exception->getMessageKey()], 401);
 }
Пример #15
0
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     $data = array('message' => strtr($exception->getMessageKey(), $exception->getMessageData()));
     return new JsonResponse($data, 403);
 }
Пример #16
0
 /**
  * This is called when an interactive authentication attempt fails. This is
  * called by authentication listeners inheriting from
  * AbstractAuthenticationListener.
  *
  * @param Request                 $request   original request
  * @param AuthenticationException $exception exception from auth attempt
  *
  * @return Response|null
  */
 public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
 {
     return new Response($exception->getMessageKey(), Response::HTTP_NETWORK_AUTHENTICATION_REQUIRED);
 }
Пример #17
0
 /**
  * Gets the message from the specific AuthenticationException and then
  * translates it. The translation process allows us to customize the
  * messages we want - see the translations/en.yml file.
  */
 private function getMessage(AuthenticationException $authException = null)
 {
     $key = $authException ? $authException->getMessageKey() : 'authentication_required';
     $parameters = $authException ? $authException->getMessageData() : array();
     return $this->translator->trans($key, $parameters);
 }