OAuthProxyListener.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace AuthBundle\Security\Firewall;
  3. use AuthBundle\Services\AccessTokenService;
  4. use Base\OAuthClientBundle\Security\Core\User\CustomOAuthUser;
  5. use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
  6. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  9. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  11. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  12. use Symfony\Component\Security\Http\Firewall\ListenerInterface;
  13. class OAuthProxyListener implements ListenerInterface
  14. {
  15. /**
  16. * @var TokenStorageInterface
  17. */
  18. protected $tokenStorage;
  19. /**
  20. * @var AuthenticationManagerInterface
  21. */
  22. protected $authenticationManager;
  23. /**
  24. * @var AccessTokenService
  25. */
  26. protected $accessTokenService;
  27. /**
  28. * @param TokenStorageInterface $tokenStorage
  29. * @param AuthenticationManagerInterface $authenticationManager
  30. * @param AccessTokenService $accessTokenService
  31. */
  32. public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessTokenService $accessTokenService)
  33. {
  34. $this->tokenStorage = $tokenStorage;
  35. $this->authenticationManager = $authenticationManager;
  36. $this->accessTokenService = $accessTokenService;
  37. }
  38. /**
  39. * @param GetResponseEvent $event
  40. *
  41. * @return type
  42. */
  43. public function handle(GetResponseEvent $event)
  44. {
  45. $request = $event->getRequest();
  46. if ($request->headers->has("php-auth-user") && $request->headers->has("php-auth-pw")) {
  47. $username = $request->headers->get("php-auth-user");
  48. $password = $request->headers->get("php-auth-pw");
  49. } else {
  50. return;
  51. }
  52. $token = $this->accessTokenService->getToken($username, $password);
  53. $auth_info = $this->accessTokenService->getUserInfo($username, $password);
  54. $user = new CustomOAuthUser($username);
  55. $user->setRoles($auth_info['roles']);
  56. $user->setTenancies($auth_info['tenancies']);
  57. $user->setTenancyCurrent($auth_info['tenancyCurrent']);
  58. $token = new UsernamePasswordToken($user, null, "api", $user->getRoles());
  59. try {
  60. $authToken = $this->authenticationManager->authenticate($token);
  61. $this->tokenStorage->setToken($authToken);
  62. return;
  63. } catch (AuthenticationException $failed) {
  64. // ... you might log something here
  65. // To deny the authentication clear the token. This will redirect to the login page.
  66. // Make sure to only clear your token, not those of other authentication listeners.
  67. $token = $this->tokenStorage->getToken();
  68. if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  69. $this->tokenStorage->setToken(null);
  70. }
  71. return;
  72. }
  73. // By default deny authorization
  74. $response = new Response();
  75. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  76. $event->setResponse($response);
  77. }
  78. }