1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Class GitHub |
4
|
|
|
* |
5
|
|
|
* @created 22.10.2017 |
6
|
|
|
* @author Smiley <[email protected]> |
7
|
|
|
* @copyright 2017 Smiley |
8
|
|
|
* @license MIT |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace chillerlan\OAuth\Providers; |
12
|
|
|
|
13
|
|
|
use chillerlan\HTTP\Utils\MessageUtil; |
14
|
|
|
use chillerlan\OAuth\Core\{CSRFToken, OAuth2Provider, ProviderException, TokenRefresh}; |
15
|
|
|
use Psr\Http\Message\ResponseInterface; |
16
|
|
|
use function sprintf; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @see https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/ |
20
|
|
|
* @see https://developer.github.com/v3/ |
21
|
|
|
* @see https://docs.github.com/en/developers/apps/building-github-apps/refreshing-user-to-server-access-tokens |
22
|
|
|
*/ |
23
|
|
|
class GitHub extends OAuth2Provider implements CSRFToken, TokenRefresh{ |
24
|
|
|
|
25
|
|
|
public const SCOPE_USER = 'user'; |
26
|
|
|
public const SCOPE_USER_EMAIL = 'user:email'; |
27
|
|
|
public const SCOPE_USER_FOLLOW = 'user:follow'; |
28
|
|
|
public const SCOPE_PUBLIC_REPO = 'public_repo'; |
29
|
|
|
public const SCOPE_REPO = 'repo'; |
30
|
|
|
public const SCOPE_REPO_DEPLOYMENT = 'repo_deployment'; |
31
|
|
|
public const SCOPE_REPO_STATUS = 'repo:status'; |
32
|
|
|
public const SCOPE_REPO_INVITE = 'repo:invite'; |
33
|
|
|
public const SCOPE_REPO_DELETE = 'delete_repo'; |
34
|
|
|
public const SCOPE_NOTIFICATIONS = 'notifications'; |
35
|
|
|
public const SCOPE_GIST = 'gist'; |
36
|
|
|
public const SCOPE_REPO_HOOK_READ = 'read:repo_hook'; |
37
|
|
|
public const SCOPE_REPO_HOOK_WRITE = 'write:repo_hook'; |
38
|
|
|
public const SCOPE_REPO_HOOK_ADMIN = 'admin:repo_hook'; |
39
|
|
|
public const SCOPE_ORG_HOOK_ADMIN = 'admin:org_hook'; |
40
|
|
|
public const SCOPE_ORG_READ = 'read:org'; |
41
|
|
|
public const SCOPE_ORG_WRITE = 'write:org'; |
42
|
|
|
public const SCOPE_ORG_ADMIN = 'admin:org'; |
43
|
|
|
public const SCOPE_PUBLIC_KEY_READ = 'read:public_key'; |
44
|
|
|
public const SCOPE_PUBLIC_KEY_WRITE = 'write:public_key'; |
45
|
|
|
public const SCOPE_PUBLIC_KEY_ADMIN = 'admin:public_key'; |
46
|
|
|
public const SCOPE_GPG_KEY_READ = 'read:gpg_key'; |
47
|
|
|
public const SCOPE_GPG_KEY_WRITE = 'write:gpg_key'; |
48
|
|
|
public const SCOPE_GPG_KEY_ADMIN = 'admin:gpg_key'; |
49
|
|
|
|
50
|
|
|
protected string $authURL = 'https://github.com/login/oauth/authorize'; |
51
|
|
|
protected string $accessTokenURL = 'https://github.com/login/oauth/access_token'; |
52
|
|
|
protected string $apiURL = 'https://api.github.com'; |
53
|
|
|
protected ?string $userRevokeURL = 'https://github.com/settings/applications'; |
54
|
|
|
protected ?string $apiDocs = 'https://developer.github.com/'; |
55
|
|
|
protected ?string $applicationURL = 'https://github.com/settings/developers'; |
56
|
|
|
protected array $authHeaders = ['Accept' => 'application/json']; |
57
|
|
|
protected array $apiHeaders = ['Accept' => 'application/vnd.github.beta+json']; |
58
|
|
|
|
59
|
|
|
protected array $defaultScopes = [ |
60
|
|
|
self::SCOPE_USER, |
61
|
|
|
self::SCOPE_USER_EMAIL, |
62
|
|
|
self::SCOPE_PUBLIC_REPO, |
63
|
|
|
self::SCOPE_GIST, |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @inheritDoc |
68
|
|
|
*/ |
69
|
|
|
public function me():ResponseInterface{ |
70
|
|
|
$response = $this->request('/user'); |
71
|
|
|
$status = $response->getStatusCode(); |
72
|
|
|
|
73
|
|
|
if($status === 200){ |
74
|
|
|
return $response; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$json = MessageUtil::decodeJSON($response); |
78
|
|
|
|
79
|
|
|
if(isset($json->message)){ |
80
|
|
|
throw new ProviderException($json->message); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
throw new ProviderException(sprintf('user info error error HTTP/%s', $status)); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
} |
87
|
|
|
|