OAuthProxyListener.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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\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. unset($token['user_info']);
  57. $accessToken = $token;
  58. $auth_info = $this->accessTokenService->getUserInfo($username, $password);
  59. } elseif ($request->headers->has("authorization")) {
  60. $authorization = $request->headers->get("authorization");
  61. $pieces = explode(' ', $authorization);
  62. $accessToken = array(
  63. 'access_token' => $pieces[1],
  64. );
  65. $auth_info = $this->accessTokenService->requestUserInfo($authorization);
  66. if (isset($auth_info['username'])) {
  67. $username = $auth_info['username'];
  68. } else {
  69. return $this->deny($event);
  70. }
  71. } elseif ($request->getClientIp()) {
  72. $username = $clientIp = $request->getClientIp();
  73. $auth = new \AuthBundle\Utils\IpUtils();
  74. if ($auth->checkIp($clientIp) === false) {
  75. return $this->deny($event);
  76. }
  77. // @TODO: Generar access token para el caso de IP valida
  78. $accessToken = array(
  79. 'access_token' => '',
  80. );
  81. $auth_info['roles'] = array('ROLE_USER');
  82. // @TODO: Traer la tenencia Base de la app Base
  83. $tenancy = array(
  84. 'id' => 1,
  85. 'name' => 'Tenencia Base',
  86. );
  87. $auth_info['tenancies'] = $auth_info['tenancyCurrent'] = $tenancy;
  88. } else {
  89. return $this->deny($event);
  90. }
  91. try {
  92. $user = new CustomOAuthUser($username);
  93. if (count($auth_info)) {
  94. $user->setRoles($auth_info['roles']);
  95. $user->setTenancies($auth_info['tenancies']);
  96. $user->setTenancyCurrent($auth_info['tenancyCurrent']);
  97. }
  98. $token = new OAuthToken($accessToken, $user->getRoles());
  99. $token->setUser($user);
  100. $authToken = $this->authenticationManager->authenticate($token);
  101. $this->tokenStorage->setToken($authToken);
  102. return;
  103. } catch (\Exception $failed) {
  104. var_dump($failed->getMessage());
  105. }
  106. $this->deny($event);
  107. }
  108. /**
  109. * @param GetResponseEvent $event
  110. */
  111. private function deny(GetResponseEvent $event)
  112. {
  113. $this->tokenStorage->setToken(null);
  114. $response = new Response();
  115. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  116. $event->setResponse($response);
  117. echo 'The OAuth authentication failed.' . PHP_EOL;
  118. return;
  119. }
  120. }