| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596 |
- <?php
- namespace Overtrue\Socialite\Providers;
- use Overtrue\Socialite\User;
- class GitHub extends Base
- {
- public const NAME = 'github';
- protected array $scopes = ['read:user'];
- protected function getAuthUrl(): string
- {
- return $this->buildAuthUrlFromBase('https://github.com/login/oauth/authorize');
- }
- protected function getTokenUrl(): string
- {
- return 'https://github.com/login/oauth/access_token';
- }
- /**
- * @param string $token
- *
- * @return array
- * @throws \GuzzleHttp\Exception\GuzzleException
- */
- protected function getUserByToken(string $token): array
- {
- $userUrl = 'https://api.github.com/user';
- $response = $this->getHttpClient()->get(
- $userUrl,
- $this->createAuthorizationHeaders($token)
- );
- $user = json_decode($response->getBody(), true);
- if (in_array('user:email', $this->scopes)) {
- $user['email'] = $this->getEmailByToken($token);
- }
- return $user;
- }
- /**
- * @param string $token
- *
- * @return string
- */
- protected function getEmailByToken(string $token)
- {
- $emailsUrl = 'https://api.github.com/user/emails';
- try {
- $response = $this->getHttpClient()->get(
- $emailsUrl,
- $this->createAuthorizationHeaders($token)
- );
- } catch (\Throwable $e) {
- return '';
- }
- foreach (json_decode($response->getBody(), true) as $email) {
- if ($email['primary'] && $email['verified']) {
- return $email['email'];
- }
- }
- }
- protected function mapUserToObject(array $user): User
- {
- return new User([
- 'id' => $user['id'] ?? null,
- 'nickname' => $user['login'] ?? null,
- 'name' => $user['name'] ?? null,
- 'email' => $user['email'] ?? null,
- 'avatar' => $user['avatar_url'] ?? null,
- ]);
- }
- /**
- * @param string $token
- *
- * @return array
- */
- protected function createAuthorizationHeaders(string $token)
- {
- return [
- 'headers' => [
- 'Accept' => 'application/vnd.github.v3+json',
- 'Authorization' => sprintf('token %s', $token),
- ],
- ];
- }
- }
|