OAuthProxyListener.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. * Se crea el User y Token mediante alguno de los métodos
  40. *
  41. * 1. Http Basic
  42. * 2. Authorization
  43. * 3. Client Ip
  44. *
  45. * @param GetResponseEvent $event
  46. *
  47. * @return type
  48. */
  49. public function handle(GetResponseEvent $event)
  50. {
  51. $request = $event->getRequest();
  52. $auth_info = array();
  53. if ($request->headers->has("php-auth-user") && $request->headers->has("php-auth-pw")) {
  54. $username = $request->headers->get("php-auth-user");
  55. $password = $request->headers->get("php-auth-pw");
  56. $token = $this->accessTokenService->getToken($username, $password);
  57. $auth_info = $this->accessTokenService->getUserInfo($username, $password);
  58. } elseif ($request->headers->has("authorization")) {
  59. $authorization = $request->headers->get("authorization");
  60. $auth_info = $this->accessTokenService->requestUserInfo($authorization);
  61. if (isset($auth_info['username'])) {
  62. $username = $auth_info['username'];
  63. } else {
  64. return;
  65. }
  66. } elseif ($request->getClientIp()) {
  67. $username = $clientIp = $request->getClientIp();
  68. if (\AuthBundle\Utils\IpUtils::checkIp($clientIp) === false) {
  69. return;
  70. }
  71. $auth_info['roles'] = array('ROLE_USER');
  72. // @TODO: Traer la tenencia Base de la app Base
  73. $tenancy = array(
  74. 'id' => 1,
  75. 'name' => 'Tenencia Base',
  76. );
  77. $auth_info['tenancies'] = $auth_info['tenancyCurrent'] = $tenancy;
  78. } else {
  79. return;
  80. }
  81. $user = new CustomOAuthUser($username);
  82. $user->setRoles($auth_info['roles']);
  83. $user->setTenancies($auth_info['tenancies']);
  84. $user->setTenancyCurrent($auth_info['tenancyCurrent']);
  85. $token = new UsernamePasswordToken($user, null, "api", $user->getRoles());
  86. try {
  87. $authToken = $this->authenticationManager->authenticate($token);
  88. $this->tokenStorage->setToken($authToken);
  89. return;
  90. } catch (AuthenticationException $failed) {
  91. // ... you might log something here
  92. // To deny the authentication clear the token. This will redirect to the login page.
  93. // Make sure to only clear your token, not those of other authentication listeners.
  94. $token = $this->tokenStorage->getToken();
  95. if ($token instanceof UsernamePasswordToken && $this->providerKey === $token->getProviderKey()) {
  96. $this->tokenStorage->setToken(null);
  97. }
  98. return;
  99. }
  100. // By default deny authorization
  101. $response = new Response();
  102. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  103. $event->setResponse($response);
  104. }
  105. }