OAuthProxyAuthenticator.php 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Base\OAuthClientBundle\Security;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\Security\Core\Authentication\Token\PreAuthenticatedToken;
  6. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  7. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  8. use Symfony\Component\Security\Core\Exception\CustomUserMessageAuthenticationException;
  9. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  10. use Symfony\Component\Security\Core\User\UserProviderInterface;
  11. use Symfony\Component\Security\Http\Authentication\SimplePreAuthenticatorInterface;
  12. use Symfony\Component\Security\Http\Authentication\AuthenticationFailureHandlerInterface;
  13. use Buzz\Listener\BasicAuthListener;
  14. use Buzz\Message;
  15. use Symfony\Component\HttpFoundation\Session\Session;
  16. class OAuthProxyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
  17. {
  18. public function __construct($client_id, $client_secret, $access_token_url, $user_info_url)
  19. {
  20. $this->client_id = $client_id;
  21. $this->client_secret = $client_secret;
  22. $this->access_token_url = $access_token_url;
  23. $this->user_info_url = $user_info_url;
  24. }
  25. public function createToken(Request $request, $providerKey)
  26. {
  27. return new PreAuthenticatedToken($request->headers->get("php-auth-user"), $request->headers->get("php-auth-pw"), $providerKey);
  28. }
  29. public function supportsToken(TokenInterface $token, $providerKey)
  30. {
  31. return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
  32. }
  33. public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
  34. {
  35. $password = $token->getCredentials();
  36. $username = $token->getUsername();
  37. $token = @json_decode(file_get_contents("/tmp/.".base64_encode($username. ":" . $password)), true);
  38. if(!isset($token["access_token"])){
  39. $browser = new \Buzz\Browser();
  40. $listener = new BasicAuthListener($this->client_id, $this->client_secret);
  41. $browser->addListener($listener);
  42. $body = ['grant_type' => 'password',
  43. 'username' => $username,
  44. 'password' => $password,
  45. ];
  46. $response = $browser->post($this->access_token_url, ['Content-Type' => 'application/x-www-form-urlencoded'], http_build_query($body));
  47. $token = json_decode($response->getContent(), true);
  48. if($token['expires_in'])
  49. $token["expires_at"] = time() + $token['expires_in'];
  50. else
  51. $token["expires_at"] = time() + 3600;
  52. file_put_contents("/tmp/.".base64_encode($username. ":" . $password), json_encode($token));
  53. }
  54. $oauth_headers = [
  55. "Authorization" => ucfirst($token["token_type"])." ".$token["access_token"],
  56. ];
  57. $browser = new \Buzz\Browser();
  58. $listener = new BasicAuthListener($this->client_id, $this->client_secret);
  59. $response = $browser->get($this->user_info_url, $oauth_headers);
  60. $auth_info = json_decode($response->getContent(), true);
  61. return new PreAuthenticatedToken( $auth_info["username"], "", $providerKey, $auth_info["roles"]);
  62. }
  63. public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  64. {
  65. return new Response(
  66. // this contains information about *why* authentication failed
  67. // use it, or return your own message
  68. strtr($exception->getMessageKey(), $exception->getMessageData()), 401);
  69. }
  70. }