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