OAuthProxyAuthenticator.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
  17. class OAuthProxyAuthenticator implements SimplePreAuthenticatorInterface, AuthenticationFailureHandlerInterface
  18. {
  19. public function __construct($client_id, $client_secret, $access_token_url, $user_info_url)
  20. {
  21. $this->client_id = $client_id;
  22. $this->client_secret = $client_secret;
  23. $this->access_token_url = $access_token_url;
  24. $this->user_info_url = $user_info_url;
  25. }
  26. public function createToken(Request $request, $providerKey)
  27. {
  28. return new PreAuthenticatedToken($request->headers->get("php-auth-user"), $request->headers->get("php-auth-pw"), $providerKey);
  29. }
  30. public function supportsToken(TokenInterface $token, $providerKey)
  31. {
  32. return $token instanceof PreAuthenticatedToken && $token->getProviderKey() === $providerKey;
  33. }
  34. public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
  35. {
  36. $password = $token->getCredentials();
  37. $username = $token->getUsername();
  38. $token = @json_decode(file_get_contents("/tmp/.".base64_encode($username. ":" . $password)), true);
  39. if(!isset($token["access_token"])){
  40. $browser = new \Buzz\Browser();
  41. $listener = new BasicAuthListener($this->client_id, $this->client_secret);
  42. $browser->addListener($listener);
  43. $body = ['grant_type' => 'password',
  44. 'username' => $username,
  45. 'password' => $password,
  46. ];
  47. $response = $browser->post($this->access_token_url, ['Content-Type' => 'application/x-www-form-urlencoded'], http_build_query($body));
  48. $token = json_decode($response->getContent(), true);
  49. if($token['expires_in'])
  50. $token["expires_at"] = time() + $token['expires_in'];
  51. else
  52. $token["expires_at"] = time() + 3600;
  53. file_put_contents("/tmp/.".base64_encode($username. ":" . $password), json_encode($token));
  54. }
  55. if(isset($token["expires_at"]) and $token["expires_at"] >= time()){
  56. $browser = new \Buzz\Browser();
  57. $listener = new BasicAuthListener($this->client_id, $this->client_secret);
  58. $browser->addListener($listener);
  59. $body = ['grant_type' => 'refresh_token',
  60. 'refresh_token' => $token['refresh_token']
  61. ];
  62. $response = $browser->post($this->access_token_url, ['Content-Type' => 'application/x-www-form-urlencoded'], http_build_query($body));
  63. $token = json_decode($response->getContent(), true);
  64. if($token['expires_in'])
  65. $token["expires_at"] = time() + $token['expires_in'];
  66. else
  67. $token["expires_at"] = time() + 3600;
  68. file_put_contents("/tmp/.".base64_encode($username. ":" . $password), json_encode($token));
  69. }
  70. if(!isset($token["user_info"])){
  71. $oauth_headers = [
  72. "Authorization" => ucfirst($token["token_type"])." ".$token["access_token"],
  73. ];
  74. $browser = new \Buzz\Browser();
  75. $response = $browser->get($this->user_info_url, $oauth_headers);
  76. $auth_info = json_decode($response->getContent(), true);
  77. $token["user_info"] = $auth_info;
  78. file_put_contents("/tmp/.".base64_encode($username. ":" . $password), json_encode($token));
  79. }
  80. $user = $userProvider->loadUserByUsername($auth_info["username"]);
  81. $user->setRoles($auth_info["roles"]);
  82. $user->setTenancyCurrent($auth_info["tenancyCurrent"]);
  83. return new PreAuthenticatedToken($user, array(), $providerKey, $user->getRoles());
  84. }
  85. public function onAuthenticationFailure(Request $request, AuthenticationException $exception)
  86. {
  87. return new Response(
  88. // this contains information about *why* authentication failed
  89. // use it, or return your own message
  90. strtr($exception->getMessageKey(), $exception->getMessageData()), 401);
  91. }
  92. }