OAuthProxyProvider.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace AuthBundle\Security\Authentication\Provider;
  3. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  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. $authenticatedToken = new UsernamePasswordToken($user, null, "api", $user->getRoles());
  33. $authenticatedToken->setUser($user);
  34. return $authenticatedToken;
  35. }
  36. throw new AuthenticationException('The OAuth authentication failed.');
  37. }
  38. /**
  39. * @param TokenInterface $token
  40. *
  41. * @return boolean
  42. */
  43. public function supports(TokenInterface $token)
  44. {
  45. return $token instanceof UsernamePasswordToken;
  46. }
  47. }