GoogleProvider.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. /*
  3. * This file is part of the overtrue/socialite.
  4. *
  5. * (c) overtrue <i@overtrue.me>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Overtrue\Socialite\Providers;
  11. use GuzzleHttp\ClientInterface;
  12. use Overtrue\Socialite\AccessTokenInterface;
  13. use Overtrue\Socialite\ProviderInterface;
  14. use Overtrue\Socialite\User;
  15. /**
  16. * Class GoogleProvider.
  17. *
  18. * @see https://developers.google.com/identity/protocols/OpenIDConnect [OpenID Connect]
  19. */
  20. class GoogleProvider extends AbstractProvider implements ProviderInterface
  21. {
  22. /**
  23. * The separating character for the requested scopes.
  24. *
  25. * @var string
  26. */
  27. protected $scopeSeparator = ' ';
  28. /**
  29. * The scopes being requested.
  30. *
  31. * @var array
  32. */
  33. protected $scopes = [
  34. 'https://www.googleapis.com/auth/userinfo.email',
  35. 'https://www.googleapis.com/auth/userinfo.profile',
  36. ];
  37. /**
  38. * {@inheritdoc}
  39. */
  40. protected function getAuthUrl($state)
  41. {
  42. return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/v2/auth', $state);
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function getTokenUrl()
  48. {
  49. return 'https://www.googleapis.com/oauth2/v4/token';
  50. }
  51. /**
  52. * Get the access token for the given code.
  53. *
  54. * @param string $code
  55. *
  56. * @return string
  57. */
  58. public function getAccessToken($code)
  59. {
  60. $guzzleVersion = \defined(ClientInterface::class.'::VERSION') ? \constant(ClientInterface::class.'::VERSION') : 7;
  61. $postKey = (1 === version_compare($guzzleVersion, '6')) ? 'form_params' : 'body';
  62. $response = $this->getHttpClient()->post($this->getTokenUrl(), [
  63. $postKey => $this->getTokenFields($code),
  64. ]);
  65. return $this->parseAccessToken($response->getBody());
  66. }
  67. /**
  68. * Get the POST fields for the token request.
  69. *
  70. * @param string $code
  71. *
  72. * @return array
  73. */
  74. protected function getTokenFields($code)
  75. {
  76. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. protected function getUserByToken(AccessTokenInterface $token)
  82. {
  83. $response = $this->getHttpClient()->get('https://www.googleapis.com/userinfo/v2/me', [
  84. 'headers' => [
  85. 'Accept' => 'application/json',
  86. 'Authorization' => 'Bearer '.$token->getToken(),
  87. ],
  88. ]);
  89. return json_decode($response->getBody(), true);
  90. }
  91. /**
  92. * {@inheritdoc}
  93. */
  94. protected function mapUserToObject(array $user)
  95. {
  96. return new User([
  97. 'id' => $this->arrayItem($user, 'id'),
  98. 'username' => $this->arrayItem($user, 'email'),
  99. 'nickname' => $this->arrayItem($user, 'name'),
  100. 'name' => $this->arrayItem($user, 'name'),
  101. 'email' => $this->arrayItem($user, 'email'),
  102. 'avatar' => $this->arrayItem($user, 'picture'),
  103. ]);
  104. }
  105. }