AccessTokenService.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. namespace AuthBundle\Services;
  3. use Buzz\Listener\BasicAuthListener;
  4. class AccessTokenService
  5. {
  6. /**
  7. * @var string
  8. */
  9. private $client_id;
  10. /**
  11. * @var string
  12. */
  13. private $client_secret;
  14. /**
  15. * @var string
  16. */
  17. private $access_token_url;
  18. /**
  19. * @var string
  20. */
  21. private $user_info_url;
  22. /**
  23. * @param string $client_id
  24. * @param string $client_secret
  25. * @param string $access_token_url
  26. * @param string $user_info_url
  27. */
  28. public function __construct($client_id, $client_secret, $access_token_url, $user_info_url)
  29. {
  30. $this->client_id = $client_id;
  31. $this->client_secret = $client_secret;
  32. $this->access_token_url = $access_token_url;
  33. $this->user_info_url = $user_info_url;
  34. }
  35. /**
  36. * @param string $username
  37. * @param string $password
  38. *
  39. * @return array
  40. */
  41. public function getToken($username, $password)
  42. {
  43. $token = @json_decode(file_get_contents("/tmp/." . base64_encode($username . ":" . $password)), true);
  44. if (!isset($token["access_token"])) {
  45. $body = [
  46. 'grant_type' => 'password',
  47. 'username' => $username,
  48. 'password' => $password,
  49. ];
  50. $token = $this->updateToken($username, $password, $body);
  51. }
  52. if (isset($token["expires_at"]) && $token["expires_at"] <= time()) {
  53. $body = [
  54. 'grant_type' => 'refresh_token',
  55. 'refresh_token' => $token['refresh_token'],
  56. ];
  57. $token = $this->updateToken($username, $password, $body);
  58. }
  59. return $token;
  60. }
  61. /**
  62. * @param string $username
  63. * @param string $password
  64. *
  65. * @return array
  66. */
  67. public function getUserInfo($username, $password)
  68. {
  69. $auth_info = array();
  70. $token = $this->getToken($username, $password);
  71. if (!isset($token["user_info"]) && isset($token["token_type"]) && isset($token["access_token"])) {
  72. $auth_info = $this->updateUserInfo($username, $password, $token);
  73. } elseif (isset($token["user_info"])) {
  74. $auth_info = $token["user_info"];
  75. }
  76. return $auth_info;
  77. }
  78. /**
  79. * @param string $username
  80. * @param string $password
  81. * @param array $body
  82. *
  83. * @return array
  84. */
  85. private function updateToken($username, $password, $body)
  86. {
  87. $browser = new \Buzz\Browser();
  88. $listener = new BasicAuthListener($this->client_id, $this->client_secret);
  89. $browser->addListener($listener);
  90. $response = $browser->post($this->access_token_url, ['Content-Type' => 'application/x-www-form-urlencoded'], http_build_query($body));
  91. $token = json_decode($response->getContent(), true);
  92. $token["expires_at"] = time() + (isset($token['expires_in']) ? $token['expires_in'] : 3600);
  93. file_put_contents("/tmp/." . base64_encode($username . ":" . $password), json_encode($token));
  94. return $token;
  95. }
  96. /**
  97. * @param string $username
  98. * @param string $password
  99. * @param array $token
  100. *
  101. * @return array
  102. */
  103. private function updateUserInfo($username, $password, $token)
  104. {
  105. $auth_info = $this->requestUserInfo(ucfirst($token["token_type"]) . " " . $token["access_token"]);
  106. $token["user_info"] = $auth_info;
  107. file_put_contents("/tmp/." . base64_encode($username . ":" . $password), json_encode($token));
  108. return $auth_info;
  109. }
  110. /**
  111. * @param string $authorization
  112. *
  113. * @return array
  114. */
  115. public function requestUserInfo($authorization)
  116. {
  117. $oauth_headers = [
  118. "Authorization" => $authorization,
  119. ];
  120. $browser = new \Buzz\Browser();
  121. $response = $browser->get($this->user_info_url, $oauth_headers);
  122. return json_decode($response->getContent(), true);
  123. }
  124. }