OAuthProxyListener.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. if (\AuthBundle\Utils\IpUtils::checkIp($clientIp) === false) {
  74. return $this->deny($event);
  75. }
  76. // @TODO: Generar access token para el caso de IP valida
  77. $accessToken = array(
  78. 'access_token' => '',
  79. );
  80. $auth_info['roles'] = array('ROLE_USER');
  81. // @TODO: Traer la tenencia Base de la app Base
  82. $tenancy = array(
  83. 'id' => 1,
  84. 'name' => 'Tenencia Base',
  85. );
  86. $auth_info['tenancies'] = $auth_info['tenancyCurrent'] = $tenancy;
  87. } else {
  88. return $this->deny($event);
  89. }
  90. try {
  91. $user = new CustomOAuthUser($username);
  92. if (count($auth_info)) {
  93. $user->setRoles($auth_info['roles']);
  94. $user->setTenancies($auth_info['tenancies']);
  95. $user->setTenancyCurrent($auth_info['tenancyCurrent']);
  96. }
  97. $token = new OAuthToken($accessToken, $user->getRoles());
  98. $token->setUser($user);
  99. $authToken = $this->authenticationManager->authenticate($token);
  100. $this->tokenStorage->setToken($authToken);
  101. return;
  102. } catch (\Exception $failed) {
  103. var_dump($failed->getMessage());
  104. }
  105. $this->deny($event);
  106. }
  107. /**
  108. * @param GetResponseEvent $event
  109. */
  110. private function deny(GetResponseEvent $event)
  111. {
  112. $this->tokenStorage->setToken(null);
  113. $response = new Response();
  114. $response->setStatusCode(Response::HTTP_FORBIDDEN);
  115. $event->setResponse($response);
  116. echo 'The OAuth authentication failed.' . PHP_EOL;
  117. return;
  118. }
  119. }