SecurityController.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Base\OAuthServerBundle\Controller;
  3. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  4. use Symfony\Component\HttpFoundation\Request;
  5. use Symfony\Component\HttpFoundation\Response;
  6. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  7. use Symfony\Component\Security\Core\Security;
  8. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  9. class SecurityController extends Controller
  10. {
  11. /**
  12. * @Route("/oauth/v2/auth/login", name="oauth_login")
  13. *
  14. * @param Request $request
  15. *
  16. * @return Response
  17. */
  18. public function loginAction(Request $request)
  19. {
  20. /** @var $session \Symfony\Component\HttpFoundation\Session\Session */
  21. $session = $request->getSession();
  22. $authErrorKey = Security::AUTHENTICATION_ERROR;
  23. $lastUsernameKey = Security::LAST_USERNAME;
  24. // get the error if any (works with forward and redirect -- see below)
  25. if ($request->attributes->has($authErrorKey)) {
  26. $error = $request->attributes->get($authErrorKey);
  27. } elseif (null !== $session && $session->has($authErrorKey)) {
  28. $error = $session->get($authErrorKey);
  29. $session->remove($authErrorKey);
  30. } else {
  31. $error = null;
  32. }
  33. if (!$error instanceof AuthenticationException) {
  34. $error = null; // The value does not come from the security component.
  35. }
  36. // last username entered by the user
  37. $lastUsername = (null === $session) ? '' : $session->get($lastUsernameKey);
  38. $csrfToken = $this->has('security.csrf.token_manager')
  39. ? $this->get('security.csrf.token_manager')->getToken('authenticate')->getValue()
  40. : null;
  41. return $this->renderLogin(array(
  42. 'last_username' => $lastUsername,
  43. 'error' => $error,
  44. 'csrf_token' => $csrfToken,
  45. ));
  46. }
  47. /**
  48. * Renders the login template with the given parameters. Overwrite this function in
  49. * an extended controller to provide additional data for the login template.
  50. *
  51. * @param array $data
  52. *
  53. * @return Response
  54. */
  55. protected function renderLogin(array $data)
  56. {
  57. return $this->render('@BaseOAuthServerBundle/Security/login.html.twig', $data);
  58. }
  59. /**
  60. * @Route("/oauth/v2/auth/login_check", name="oauth_login_check")
  61. */
  62. public function checkAction()
  63. {
  64. throw new \RuntimeException('You must configure the check path to be handled by the firewall using form_login in your security firewall configuration.');
  65. }
  66. /**
  67. * @Route("/oauth/v2/auth/logout", name="oauth_logout")
  68. */
  69. public function logoutAction()
  70. {
  71. throw new \RuntimeException('You must activate the logout in your security firewall configuration.');
  72. }
  73. }