OAuthProxyUserProvider.php 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. <?php
  2. namespace Base\OAuthClientBundle\Security;
  3. use Symfony\Component\Security\Core\User\UserProviderInterface;
  4. use Symfony\Component\Security\Core\User\User;
  5. use Symfony\Component\Security\Core\User\UserInterface;
  6. use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
  7. class OAuthProxyUserProvider implements UserProviderInterface
  8. {
  9. public function getUsernameForApiKey($apiKey)
  10. {
  11. // Look up the username based on the token in the database, via
  12. // an API call, or do something entirely different
  13. $username = "nose";
  14. return $username;
  15. }
  16. public function loadUserByUsername($username)
  17. {
  18. return new User(
  19. $username,
  20. null,
  21. // the roles for the user - you may choose to determine
  22. // these dynamically somehow based on the user
  23. array('ROLE_API')
  24. );
  25. }
  26. public function refreshUser(UserInterface $user)
  27. {
  28. // this is used for storing authentication in the session
  29. // but in this example, the token is sent in each request,
  30. // so authentication can be stateless. Throwing this exception
  31. // is proper to make things stateless
  32. throw new UnsupportedUserException();
  33. }
  34. public function supportsClass($class)
  35. {
  36. return User::class === $class;
  37. }
  38. }