|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace SocialiteProviders\Crowdin; |
| 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 = 'CROWDIN'; |
| 12 | + |
| 13 | + protected $scopes = []; |
| 14 | + |
| 15 | + /** |
| 16 | + * {@inheritdoc} |
| 17 | + */ |
| 18 | + protected $consent = false; |
| 19 | + |
| 20 | + protected $scopeSeparator = ' '; |
| 21 | + |
| 22 | + protected function getAuthUrl($state): string |
| 23 | + { |
| 24 | + return $this->buildAuthUrlFromBase('https://accounts.crowdin.com/oauth/authorize', $state); |
| 25 | + } |
| 26 | + |
| 27 | + /** |
| 28 | + * {@inheritdoc} |
| 29 | + */ |
| 30 | + protected function getCodeFields($state = null) |
| 31 | + { |
| 32 | + $fields = parent::getCodeFields($state); |
| 33 | + |
| 34 | + if (! $this->consent) { |
| 35 | + $fields['prompt'] = 'none'; |
| 36 | + } |
| 37 | + |
| 38 | + return $fields; |
| 39 | + } |
| 40 | + |
| 41 | + /** |
| 42 | + * Prompt for consent each time or not. |
| 43 | + * |
| 44 | + * @return $this |
| 45 | + */ |
| 46 | + public function withConsent() |
| 47 | + { |
| 48 | + $this->consent = true; |
| 49 | + |
| 50 | + return $this; |
| 51 | + } |
| 52 | + |
| 53 | + protected function getTokenUrl(): string |
| 54 | + { |
| 55 | + return 'https://accounts.crowdin.com/oauth/token'; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * {@inheritdoc} |
| 60 | + */ |
| 61 | + protected function getUserByToken($token) |
| 62 | + { |
| 63 | + $response = $this->getHttpClient()->get( |
| 64 | + 'https://crowdin.com/api/v2/user', |
| 65 | + [ |
| 66 | + RequestOptions::HEADERS => [ |
| 67 | + 'Authorization' => 'Bearer '.$token, |
| 68 | + ], |
| 69 | + ] |
| 70 | + ); |
| 71 | + |
| 72 | + return json_decode((string) $response->getBody(), true); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + protected function mapUserToObject(array $user) |
| 79 | + { |
| 80 | + return (new User)->setRaw($user)->map([ |
| 81 | + 'id' => $user['id'], |
| 82 | + 'nickname' => $user['fullName'], |
| 83 | + 'name' => $user['username'], |
| 84 | + 'email' => $user['email'] ?? null, |
| 85 | + 'avatar' => $user['avatarUrl'], |
| 86 | + ]); |
| 87 | + } |
| 88 | +} |
0 commit comments