OAuthProxyProvider.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. namespace AuthBundle\Security\Authentication\Provider;
  3. use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
  4. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  5. use Symfony\Component\Security\Core\User\UserProviderInterface;
  6. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  7. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  8. class OAuthProxyProvider implements AuthenticationProviderInterface
  9. {
  10. /**
  11. * @var UserProviderInterface
  12. */
  13. private $userProvider;
  14. /**
  15. * @param UserProviderInterface $userProvider
  16. */
  17. public function __construct(UserProviderInterface $userProvider)
  18. {
  19. $this->userProvider = $userProvider;
  20. }
  21. /**
  22. * @param TokenInterface $token
  23. *
  24. * @return OAuthToken
  25. *
  26. * @throws AuthenticationException
  27. */
  28. public function authenticate(TokenInterface $token)
  29. {
  30. $user = $token->getUser();
  31. if ($user) {
  32. $token->setAuthenticated(true);
  33. return $token;
  34. }
  35. throw new AuthenticationException('The OAuth authentication failed.');
  36. }
  37. /**
  38. * @param TokenInterface $token
  39. *
  40. * @return boolean
  41. */
  42. public function supports(TokenInterface $token)
  43. {
  44. return $token instanceof OAuthToken;
  45. }
  46. }