123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <?php
- namespace AuthBundle\Security\Firewall;
- use AuthBundle\Services\AccessTokenService;
- use Base\OAuthClientBundle\Security\Core\User\CustomOAuthUser;
- use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
- use Symfony\Component\HttpFoundation\Response;
- use Symfony\Component\HttpKernel\Event\GetResponseEvent;
- use Symfony\Component\Security\Core\Authentication\AuthenticationManagerInterface;
- use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
- use Symfony\Component\Security\Core\Exception\AuthenticationException;
- use Symfony\Component\Security\Http\Firewall\ListenerInterface;
- class OAuthProxyListener implements ListenerInterface
- {
- /**
- * @var TokenStorageInterface
- */
- protected $tokenStorage;
- /**
- * @var AuthenticationManagerInterface
- */
- protected $authenticationManager;
- /**
- * @var AccessTokenService
- */
- protected $accessTokenService;
- /**
- * @param TokenStorageInterface $tokenStorage
- * @param AuthenticationManagerInterface $authenticationManager
- * @param AccessTokenService $accessTokenService
- */
- public function __construct(TokenStorageInterface $tokenStorage, AuthenticationManagerInterface $authenticationManager, AccessTokenService $accessTokenService)
- {
- $this->tokenStorage = $tokenStorage;
- $this->authenticationManager = $authenticationManager;
- $this->accessTokenService = $accessTokenService;
- }
- /**
- * Se crea el User y Token mediante alguno de los métodos
- *
- * 1. Http Basic
- * 2. Authorization
- * 3. Client Ip
- *
- * @param GetResponseEvent $event
- *
- * @return type
- */
- public function handle(GetResponseEvent $event)
- {
- $request = $event->getRequest();
- $auth_info = array();
- if ($request->headers->has("php-auth-user") && $request->headers->has("php-auth-pw")) {
- $username = $request->headers->get("php-auth-user");
- $password = $request->headers->get("php-auth-pw");
- $token = $this->accessTokenService->getToken($username, $password);
- unset($token['user_info']);
- $accessToken = $token;
- $auth_info = $this->accessTokenService->getUserInfo($username, $password);
- } elseif ($request->headers->has("authorization")) {
- $authorization = $request->headers->get("authorization");
- $pieces = explode(' ', $authorization);
- $accessToken = array(
- 'access_token' => $pieces[1],
- );
- $auth_info = $this->accessTokenService->requestUserInfo($authorization);
- if (isset($auth_info['username'])) {
- $username = $auth_info['username'];
- } else {
- return $this->deny($event);
- }
- } elseif ($request->getClientIp()) {
- $username = $clientIp = $request->getClientIp();
- if (\AuthBundle\Utils\IpUtils::checkIp($clientIp) === false) {
- return $this->deny($event);
- }
- // @TODO: Generar access token para el caso de IP valida
- $accessToken = array(
- 'access_token' => '',
- );
- $auth_info['roles'] = array('ROLE_USER');
- // @TODO: Traer la tenencia Base de la app Base
- $tenancy = array(
- 'id' => 1,
- 'name' => 'Tenencia Base',
- );
- $auth_info['tenancies'] = $auth_info['tenancyCurrent'] = $tenancy;
- } else {
- return $this->deny($event);
- }
- try {
- $user = new CustomOAuthUser($username);
- if (count($auth_info)) {
- $user->setRoles($auth_info['roles']);
- $user->setTenancies([$auth_info['tenancies']]);
- $user->setTenancyCurrent($auth_info['tenancyCurrent']);
- }
- $token = new OAuthToken($accessToken, $user->getRoles());
- $token->setUser($user);
- $authToken = $this->authenticationManager->authenticate($token);
- $this->tokenStorage->setToken($authToken);
- return;
- } catch (\Exception $failed) {
- var_dump($failed->getMessage());
- }
- $this->deny($event);
- }
- /**
- * @param GetResponseEvent $event
- */
- private function deny(GetResponseEvent $event)
- {
- $this->tokenStorage->setToken(null);
- $response = new Response();
- $response->setStatusCode(Response::HTTP_FORBIDDEN);
- $event->setResponse($response);
- echo 'The OAuth authentication failed.' . PHP_EOL;
- return;
- }
- }
|