|
2 | 2 |
|
3 | 3 | namespace Overtrue\Socialite\Providers;
|
4 | 4 |
|
| 5 | +use GuzzleHttp\Psr7\Stream; |
| 6 | +use Overtrue\Socialite\Exceptions\AuthorizeFailedException; |
5 | 7 | use Overtrue\Socialite\Exceptions\BadRequestException;
|
6 | 8 | use Overtrue\Socialite\User;
|
7 | 9 |
|
@@ -139,11 +141,45 @@ protected function mapUserToObject(array $user): User
|
139 | 141 | }
|
140 | 142 |
|
141 | 143 | return new User([
|
142 |
| - 'id' => $user['data']['id'], |
143 |
| - 'nickname' => $user['data']['nick'], |
144 |
| - 'name' => $user['data']['name'], |
145 |
| - 'email' => '', |
146 |
| - 'avatar' => $user['data']['avatar'], |
| 144 | + 'id' => $user['data']['id'] ?? null, |
| 145 | + 'nickname' => $user['data']['nick'] ?? null, |
| 146 | + 'name' => $user['data']['name'] ?? null, |
| 147 | + 'email' => $user['data']['email'] ?? null, |
| 148 | + 'avatar' => $user['data']['avatar'] ?? null, |
147 | 149 | ]);
|
148 | 150 | }
|
| 151 | + |
| 152 | + /** |
| 153 | + * @param array|string $response |
| 154 | + * |
| 155 | + * @return mixed |
| 156 | + * @return array |
| 157 | + * @throws \Overtrue\Socialite\Exceptions\AuthorizeFailedException |
| 158 | + * |
| 159 | + */ |
| 160 | + protected function normalizeAccessTokenResponse($response): array |
| 161 | + { |
| 162 | + if ($response instanceof Stream) { |
| 163 | + $response->rewind(); |
| 164 | + $response = $response->getContents(); |
| 165 | + } |
| 166 | + |
| 167 | + if (\is_string($response)) { |
| 168 | + $response = json_decode($response, true) ?? []; |
| 169 | + } |
| 170 | + |
| 171 | + if (!\is_array($response)) { |
| 172 | + throw new AuthorizeFailedException('Invalid token response', [$response]); |
| 173 | + } |
| 174 | + |
| 175 | + if (empty($response['data'][$this->accessTokenKey])) { |
| 176 | + throw new AuthorizeFailedException('Authorize Failed: ' . json_encode($response, JSON_UNESCAPED_UNICODE), $response); |
| 177 | + } |
| 178 | + |
| 179 | + return $response + [ |
| 180 | + 'access_token' => $response['data'][$this->accessTokenKey], |
| 181 | + 'refresh_token' => $response['data'][$this->refreshTokenKey] ?? null, |
| 182 | + 'expires_in' => \intval($response['data'][$this->expiresInKey] ?? 0), |
| 183 | + ]; |
| 184 | + } |
149 | 185 | }
|
0 commit comments