You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

123 lines
3.1 KiB

  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/plus.me',
  35. 'https://www.googleapis.com/auth/plus.login',
  36. 'https://www.googleapis.com/auth/plus.profile.emails.read',
  37. ];
  38. /**
  39. * {@inheritdoc}
  40. */
  41. protected function getAuthUrl($state)
  42. {
  43. return $this->buildAuthUrlFromBase('https://accounts.google.com/o/oauth2/auth', $state);
  44. }
  45. /**
  46. * {@inheritdoc}
  47. */
  48. protected function getTokenUrl()
  49. {
  50. return 'https://accounts.google.com/o/oauth2/token';
  51. }
  52. /**
  53. * Get the access token for the given code.
  54. *
  55. * @param string $code
  56. *
  57. * @return string
  58. */
  59. public function getAccessToken($code)
  60. {
  61. $postKey = (version_compare(ClientInterface::VERSION, '6') === 1) ? '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/plus/v1/people/me?', [
  84. 'query' => [
  85. 'prettyPrint' => 'false',
  86. ],
  87. 'headers' => [
  88. 'Accept' => 'application/json',
  89. 'Authorization' => 'Bearer '.$token->getToken(),
  90. ],
  91. ]);
  92. return json_decode($response->getBody(), true);
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. protected function mapUserToObject(array $user)
  98. {
  99. return new User([
  100. 'id' => $this->arrayItem($user, 'id'),
  101. 'username' => $this->arrayItem($user, 'emails.0.value'),
  102. 'nickname' => $this->arrayItem($user, 'nickname'),
  103. 'name' => $this->arrayItem($user, 'displayName'),
  104. 'email' => $this->arrayItem($user, 'emails.0.value'),
  105. 'avatar' => $this->arrayItem($user, 'image.url'),
  106. ]);
  107. }
  108. }