Baidu.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace Overtrue\Socialite\Providers;
  3. use Overtrue\Socialite\User;
  4. /**
  5. * @see https://developer.baidu.com/wiki/index.php?title=docs/oauth [OAuth 2.0 授权机制说明]
  6. */
  7. class Baidu extends Base
  8. {
  9. public const NAME = 'baidu';
  10. protected string $baseUrl = 'https://openapi.baidu.com';
  11. protected string $version = '2.0';
  12. protected array $scopes = ['basic'];
  13. protected string $display = 'popup';
  14. /**
  15. * @param string $display
  16. *
  17. * @return $this
  18. */
  19. public function withDisplay(string $display): self
  20. {
  21. $this->display = $display;
  22. return $this;
  23. }
  24. /**
  25. * @param array $scopes
  26. *
  27. * @return self
  28. */
  29. public function withScopes(array $scopes): self
  30. {
  31. $this->scopes = $scopes;
  32. return $this;
  33. }
  34. /**
  35. * @return string
  36. */
  37. protected function getAuthUrl(): string
  38. {
  39. return $this->buildAuthUrlFromBase($this->baseUrl . '/oauth/' . $this->version . '/authorize');
  40. }
  41. protected function getCodeFields(): array
  42. {
  43. return [
  44. 'response_type' => 'code',
  45. 'client_id' => $this->getClientId(),
  46. 'redirect_uri' => $this->redirectUrl,
  47. 'scope' => $this->formatScopes($this->scopes, $this->scopeSeparator),
  48. 'display' => $this->display,
  49. ] + $this->parameters;
  50. }
  51. /**
  52. * @return string
  53. */
  54. protected function getTokenUrl(): string
  55. {
  56. return $this->baseUrl . '/oauth/' . $this->version . '/token';
  57. }
  58. /**
  59. * @param string $code
  60. *
  61. * @return array
  62. */
  63. protected function getTokenFields($code): array
  64. {
  65. return parent::getTokenFields($code) + ['grant_type' => 'authorization_code'];
  66. }
  67. /**
  68. * @param string $token
  69. *
  70. * @return array
  71. * @throws \GuzzleHttp\Exception\GuzzleException
  72. */
  73. protected function getUserByToken(string $token): array
  74. {
  75. $response = $this->getHttpClient()->get(
  76. $this->baseUrl . '/rest/' . $this->version . '/passport/users/getInfo',
  77. [
  78. 'query' => [
  79. 'access_token' => $token,
  80. ],
  81. 'headers' => [
  82. 'Accept' => 'application/json',
  83. ],
  84. ]
  85. );
  86. return json_decode($response->getBody(), true) ?? [];
  87. }
  88. /**
  89. * @param array $user
  90. *
  91. * @return \Overtrue\Socialite\User
  92. */
  93. protected function mapUserToObject(array $user): User
  94. {
  95. return new User(
  96. [
  97. 'id' => $user['userid'] ?? null,
  98. 'nickname' => $user['realname'] ?? null,
  99. 'name' => $user['username'] ?? null,
  100. 'email' => '',
  101. 'avatar' => $user['portrait'] ? 'http://tb.himg.baidu.com/sys/portraitn/item/' . $user['portrait'] : null,
  102. ]
  103. );
  104. }
  105. }