|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SocialiteProviders\Trovo; |
| 4 | + |
| 5 | +use GuzzleHttp\RequestOptions; |
| 6 | +use SocialiteProviders\Manager\OAuth2\AbstractProvider; |
| 7 | +use SocialiteProviders\Manager\OAuth2\User; |
| 8 | + |
| 9 | +class Provider extends AbstractProvider |
| 10 | +{ |
| 11 | + public const IDENTIFIER = 'TROVO'; |
| 12 | + |
| 13 | + protected $scopes = ['user_details_self']; |
| 14 | + |
| 15 | + protected $scopeSeparator = '+'; |
| 16 | + |
| 17 | + protected function getAuthUrl($state): string |
| 18 | + { |
| 19 | + return $this->buildAuthUrlFromBase( |
| 20 | + 'https://open.trovo.live/page/login.html', |
| 21 | + $state |
| 22 | + ); |
| 23 | + } |
| 24 | + |
| 25 | + protected function getTokenUrl(): string |
| 26 | + { |
| 27 | + return 'https://open-api.trovo.live/openplatform/exchangetoken'; |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * {@inheritdoc} |
| 32 | + */ |
| 33 | + public function getAccessTokenResponse($code) |
| 34 | + { |
| 35 | + $fields = $this->getTokenFields($code); |
| 36 | + unset($fields['client_id']); |
| 37 | + |
| 38 | + $response = $this->getHttpClient()->post($this->getTokenUrl(), [ |
| 39 | + RequestOptions::HEADERS => [ |
| 40 | + 'Accept' => 'application/json', |
| 41 | + 'Client-ID' => $this->clientId, |
| 42 | + ], |
| 43 | + RequestOptions::JSON => $fields, |
| 44 | + ]); |
| 45 | + |
| 46 | + return json_decode((string) $response->getBody(), true); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * {@inheritdoc} |
| 51 | + */ |
| 52 | + protected function getUserByToken($token) |
| 53 | + { |
| 54 | + $response = $this->getHttpClient()->get( |
| 55 | + 'https://open-api.trovo.live/openplatform/getuserinfo', |
| 56 | + [ |
| 57 | + RequestOptions::HEADERS => [ |
| 58 | + 'Accept' => 'application/json', |
| 59 | + 'Client-ID' => $this->clientId, |
| 60 | + 'Authorization' => 'OAuth '.$token, |
| 61 | + ], |
| 62 | + ] |
| 63 | + ); |
| 64 | + |
| 65 | + return json_decode((string) $response->getBody(), true); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * {@inheritdoc} |
| 70 | + */ |
| 71 | + protected function mapUserToObject(array $user) |
| 72 | + { |
| 73 | + return (new User)->setRaw($user)->map([ |
| 74 | + 'id' => $user['userId'], |
| 75 | + 'nickname' => $user['nickName'] ?? null, |
| 76 | + 'name' => $user['userName'] ?? null, |
| 77 | + 'email' => $user['email'], |
| 78 | + 'avatar' => $user['profilePic'] ?? null, |
| 79 | + ]); |
| 80 | + } |
| 81 | +} |
0 commit comments