OAuthProxyListener.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace AuthBundle\Security\Firewall;
  3. use AuthBundle\Services\AccessTokenService;
  4. use Base\OAuthClientBundle\Security\Core\User\CustomOAuthUser;
  5. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  6. use Symfony\Component\HttpFoundation\Response;
  7. use Symfony\Component\HttpKernel\Event\GetResponseEvent;
  8. use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
  10. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  11. use Symfony\Component\Security\Http\Firewall\ListenerInterface;
  12. class OAuthProxyListener implements ListenerInterface
  13. {
  14. /**
  15. * @var TokenStorageInterface
  16. */
  17. protected $tokenStorage;
  18. /**
  19. * @var AuthenticationManagerInterface
  20. */
  21. protected $authenticationManager;
  22. /**
  23. * @var AccessTokenService
  24. */
  25. protected $accessTokenService;
  26. /**
  27. * @param TokenStorageInterface $tokenStorage
  28. * @param AuthenticationManagerInterface $authenticationManager
  29. * @param AccessTokenService $accessTokenService
  30. */
  31. public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessTokenService $accessTokenService)
  32. {
  33. $this->tokenStorage = $tokenStorage;
  34. $this->authenticationManager = $authenticationManager;
  35. $this->accessTokenService = $accessTokenService;
  36. }
  37. /**
  38. * Se crea el User y Token mediante alguno de los métodos
  39. *
  40. * 1. Http Basic
  41. * 2. Authorization
  42. * 3. Client Ip
  43. *
  44. * @param GetResponseEvent $event
  45. *
  46. * @return type
  47. */
  48. public function handle(GetResponseEvent $event)
  49. {
  50. $request = $event->getRequest();
  51. $auth_info = array();
  52. if ($request->headers->has("php-auth-user") && $request->headers->has("php-auth-pw")) {
  53. $username = $request->headers->get("php-auth-user");
  54. $password = $request->headers->get("php-auth-pw");
  55. $token = $this->accessTokenService->getToken($username, $password);
  56. $auth_info = $this->accessTokenService->getUserInfo($username, $password);
  57. } elseif ($request->headers->has("authorization")) {
  58. $authorization = $request->headers->get("authorization");
  59. $auth_info = $this->accessTokenService->requestUserInfo($authorization);
  60. if (isset($auth_info['username'])) {
  61. $username = $auth_info['username'];
  62. } else {
  63. return;
  64. }
  65. } elseif ($request->getClientIp()) {
  66. $username = $clientIp = $request->getClientIp();
  67. if (\AuthBundle\Utils\IpUtils::checkIp($clientIp) === false) {
  68. return;
  69. }
  70. $auth_info['roles'] = array('ROLE_USER');
  71. // @TODO: Traer la tenencia Base de la app Base
  72. $tenancy = array(
  73. 'id' => 1,
  74. 'name' => 'Tenencia Base',
  75. );
  76. $auth_info['tenancies'] = $auth_info['tenancyCurrent'] = $tenancy;
  77. } else {
  78. return;
  79. }
  80. $user = new CustomOAuthUser($username);
  81. $user->setRoles($auth_info['roles']);
  82. $user->setTenancies($auth_info['tenancies']);
  83. $user->setTenancyCurrent($auth_info['tenancyCurrent']);
  84. $token = new UsernamePasswordToken($user, null, "api", $user->getRoles());
  85. try {
  86. $authToken = $this->authenticationManager->authenticate($token);
  87. $this->tokenStorage->setToken($authToken);
  88. return;
  89. } catch (AuthenticationException $failed) {
  90. // ... you might log something here
  91. // To deny the authentication clear the token. This will redirect to the login page.
  92. // Make sure to only clear your token, not those of other authentication listeners.
  93. $token = $this->tokenStorage->getToken();
  94. if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  95. $this->tokenStorage->setToken(null);
  96. }
  97. return;
  98. }
  99. // By default deny authorization
  100. $response = new Response();
  101. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  102. $event->setResponse($response);
  103. }
  104. }