|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SocialiteProviders\Kick; |
| 4 | + |
| 5 | +use GuzzleHttp\RequestOptions; |
| 6 | +use Illuminate\Support\Arr; |
| 7 | +use Laravel\Socialite\Two\Token; |
| 8 | +use SocialiteProviders\Manager\OAuth2\AbstractProvider; |
| 9 | +use SocialiteProviders\Manager\OAuth2\User; |
| 10 | + |
| 11 | +class Provider extends AbstractProvider |
| 12 | +{ |
| 13 | + public const IDENTIFIER = 'KICK'; |
| 14 | + |
| 15 | + protected $scopes = ['user:read']; |
| 16 | + |
| 17 | + protected $usesPKCE = true; |
| 18 | + |
| 19 | + protected function getAuthUrl($state): string |
| 20 | + { |
| 21 | + return $this->buildAuthUrlFromBase('https://id.kick.com/oauth/authorize', $state); |
| 22 | + } |
| 23 | + |
| 24 | + protected function getTokenUrl(): string |
| 25 | + { |
| 26 | + return 'https://id.kick.com/oauth/token'; |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * {@inheritdoc} |
| 31 | + */ |
| 32 | + protected function getUserByToken($token) |
| 33 | + { |
| 34 | + $response = $this->getHttpClient()->get( |
| 35 | + 'https://api.kick.com/public/v1/users', |
| 36 | + [ |
| 37 | + RequestOptions::HEADERS => [ |
| 38 | + 'Accept' => 'application/json', |
| 39 | + 'Authorization' => 'Bearer ' . $token |
| 40 | + ], |
| 41 | + ] |
| 42 | + ); |
| 43 | + |
| 44 | + return json_decode((string) $response->getBody(), true); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * {@inheritdoc} |
| 49 | + */ |
| 50 | + protected function mapUserToObject(array $user) |
| 51 | + { |
| 52 | + $user = $user['data'][0]; |
| 53 | + |
| 54 | + return (new User)->setRaw($user)->map([ |
| 55 | + 'id' => $user['user_id'], |
| 56 | + 'nickname' => $user['name'], |
| 57 | + 'name' => $user['name'], |
| 58 | + 'email' => $user['email'], |
| 59 | + 'avatar' => $user['profile_picture'] ?? null, |
| 60 | + ]); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * {@inheritdoc} |
| 65 | + */ |
| 66 | + public function refreshToken($refreshToken) |
| 67 | + { |
| 68 | + $response = $this->getRefreshTokenResponse($refreshToken); |
| 69 | + |
| 70 | + return new Token( |
| 71 | + Arr::get($response, 'access_token'), |
| 72 | + Arr::get($response, 'refresh_token'), |
| 73 | + Arr::get($response, 'expires_in'), |
| 74 | + Arr::get($response, 'scope', []) |
| 75 | + ); |
| 76 | + } |
| 77 | +} |
0 commit comments