| 1 |
<?php |
| 2 |
|
| 3 |
namespace League\OAuth2\Client\Provider; |
| 4 |
|
| 5 |
use League\OAuth2\Client\Provider\Exception\GithubIdentityProviderException; |
| 6 |
use League\OAuth2\Client\Token\AccessToken; |
| 7 |
use League\OAuth2\Client\Tool\BearerAuthorizationTrait; |
| 8 |
use Psr\Http\Message\ResponseInterface; |
| 9 |
|
| 10 |
class Github extends AbstractProvider |
| 11 |
{ |
| 12 |
use BearerAuthorizationTrait; |
| 13 |
|
| 14 |
/** |
| 15 |
* Domain |
| 16 |
* |
| 17 |
* @var string |
| 18 |
*/ |
| 19 |
public $domain = 'https://github.com'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Api domain |
| 23 |
* |
| 24 |
* @var string |
| 25 |
*/ |
| 26 |
public $apiDomain = 'https://api.github.com'; |
| 27 |
|
| 28 |
/** |
| 29 |
* Get authorization url to begin OAuth flow |
| 30 |
* |
| 31 |
* @return string |
| 32 |
*/ |
| 33 |
public function getBaseAuthorizationUrl() |
| 34 |
{ |
| 35 |
return $this->domain.'/login/oauth/authorize'; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Get access token url to retrieve token |
| 40 |
* |
| 41 |
* @param array $params |
| 42 |
* |
| 43 |
* @return string |
| 44 |
*/ |
| 45 |
public function getBaseAccessTokenUrl(array $params) |
| 46 |
{ |
| 47 |
return $this->domain.'/login/oauth/access_token'; |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Get provider url to fetch user details |
| 52 |
* |
| 53 |
* @param AccessToken $token |
| 54 |
* |
| 55 |
* @return string |
| 56 |
*/ |
| 57 |
public function getResourceOwnerDetailsUrl(AccessToken $token) |
| 58 |
{ |
| 59 |
if ($this->domain === 'https://github.com') { |
| 60 |
return $this->apiDomain.'/user'; |
| 61 |
} |
| 62 |
return $this->domain.'/api/v3/user'; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get the default scopes used by this provider. |
| 67 |
* |
| 68 |
* This should not be a complete list of all scopes, but the minimum |
| 69 |
* required for the provider user interface! |
| 70 |
* |
| 71 |
* @return array |
| 72 |
*/ |
| 73 |
protected function getDefaultScopes() |
| 74 |
{ |
| 75 |
return []; |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Check a provider response for errors. |
| 80 |
* |
| 81 |
* @link https://developer.github.com/v3/#client-errors |
| 82 |
* @link https://developer.github.com/v3/oauth/#common-errors-for-the-access-token-request |
| 83 |
* @throws IdentityProviderException |
| 84 |
* @param ResponseInterface $response |
| 85 |
* @param string $data Parsed response data |
| 86 |
* @return void |
| 87 |
*/ |
| 88 |
protected function checkResponse(ResponseInterface $response, $data) |
| 89 |
{ |
| 90 |
if ($response->getStatusCode() >= 400) { |
| 91 |
throw GithubIdentityProviderException::clientException($response, $data); |
| 92 |
} elseif (isset($data['error'])) { |
| 93 |
throw GithubIdentityProviderException::oauthException($response, $data); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Generate a user object from a successful user details request. |
| 99 |
* |
| 100 |
* @param array $response |
| 101 |
* @param AccessToken $token |
| 102 |
* @return League\OAuth2\Client\Provider\ResourceOwnerInterface |
| 103 |
*/ |
| 104 |
protected function createResourceOwner(array $response, AccessToken $token) |
| 105 |
{ |
| 106 |
$user = new GithubResourceOwner($response); |
| 107 |
|
| 108 |
return $user->setDomain($this->domain); |
| 109 |
} |
| 110 |
} |
| 111 |
|