BaiduProvider.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Overtrue\Socialite\Providers;
  3. use Overtrue\Socialite\AccessTokenInterface;
  4. use Overtrue\Socialite\ProviderInterface;
  5. use Overtrue\Socialite\User;
  6. /**
  7. * Class BaiduProvider.
  8. *
  9. * @see https://developer.baidu.com/wiki/index.php?title=docs/oauth [OAuth 2.0 授权机制说明]
  10. */
  11. class BaiduProvider extends AbstractProvider implements ProviderInterface
  12. {
  13. /**
  14. * The base url of Weibo API.
  15. *
  16. * @var string
  17. */
  18. protected $baseUrl = 'https://openapi.baidu.com';
  19. /**
  20. * The API version for the request.
  21. *
  22. * @var string
  23. */
  24. protected $version = '2.0';
  25. /**
  26. * The scopes being requested.
  27. *
  28. * @var array
  29. */
  30. protected $scopes = [''];
  31. /**
  32. * The uid of user authorized.
  33. *
  34. * @var int
  35. */
  36. protected $uid;
  37. protected $display = 'popup';
  38. /**
  39. * Get the authentication URL for the provider.
  40. *
  41. * @param string $state
  42. *
  43. * @return string
  44. */
  45. protected function getAuthUrl($state)
  46. {
  47. return $this->buildAuthUrlFromBase($this->baseUrl.'/oauth/'.$this->version.'/authorize', $state);
  48. }
  49. /**
  50. * {@inheritdoc}.
  51. */
  52. protected function getCodeFields($state = null)
  53. {
  54. return array_merge([
  55. 'response_type' => 'code',
  56. 'client_id' => $this->getConfig()->get('client_id'),
  57. 'redirect_uri' => $this->redirectUrl,
  58. 'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
  59. 'display' => $this->display,
  60. ], $this->parameters);
  61. }
  62. /**
  63. * Get the token URL for the provider.
  64. *
  65. * @return string
  66. */
  67. protected function getTokenUrl()
  68. {
  69. return $this->baseUrl.'/oauth/'.$this->version.'/token';
  70. }
  71. /**
  72. * Get the Post fields for the token request.
  73. *
  74. * @param string $code
  75. *
  76. * @return array
  77. */
  78. protected function getTokenFields($code)
  79. {
  80. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  81. }
  82. /**
  83. * Get the raw user for the given access token.
  84. *
  85. * @param \Overtrue\Socialite\AccessTokenInterface $token
  86. *
  87. * @return array
  88. */
  89. protected function getUserByToken(AccessTokenInterface $token)
  90. {
  91. $response = $this->getHttpClient()->get($this->baseUrl.'/rest/'.$this->version.'/passport/users/getInfo', [
  92. 'query' => [
  93. 'access_token' => $token->getToken(),
  94. ],
  95. 'headers' => [
  96. 'Accept' => 'application/json',
  97. ],
  98. ]);
  99. return json_decode($response->getBody(), true);
  100. }
  101. /**
  102. * Map the raw user array to a Socialite User instance.
  103. *
  104. * @param array $user
  105. *
  106. * @return \Overtrue\Socialite\User
  107. */
  108. protected function mapUserToObject(array $user)
  109. {
  110. $realname = $this->arrayItem($user, 'realname');
  111. return new User([
  112. 'id' => $this->arrayItem($user, 'userid'),
  113. 'nickname' => empty($realname) ? '' : $realname,
  114. 'name' => $this->arrayItem($user, 'username'),
  115. 'email' => '',
  116. 'avatar' => $this->arrayItem($user, 'portrait'),
  117. ]);
  118. }
  119. }