1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace OAuth\OAuth2\Service; |
4
|
|
|
|
5
|
|
|
use OAuth\OAuth2\Token\StdOAuth2Token; |
6
|
|
|
use OAuth\Common\Http\Exception\TokenResponseException; |
7
|
|
|
use OAuth\Common\Http\Uri\Uri; |
8
|
|
|
|
9
|
|
|
class GitHub extends AbstractService |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Defined scopes, see http://developer.github.com/v3/oauth/ for definitions. |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Public read-only access (includes public user profile info, public repo info, and gists) |
17
|
|
|
*/ |
18
|
|
|
const SCOPE_READONLY = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Read/write access to profile info only. |
22
|
|
|
* |
23
|
|
|
* Includes SCOPE_USER_EMAIL and SCOPE_USER_FOLLOW. |
24
|
|
|
*/ |
25
|
|
|
const SCOPE_USER = 'user'; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Read access to a user’s email addresses. |
29
|
|
|
*/ |
30
|
|
|
const SCOPE_USER_EMAIL = 'user:email'; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Access to follow or unfollow other users. |
34
|
|
|
*/ |
35
|
|
|
const SCOPE_USER_FOLLOW = 'user:follow'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Read/write access to public repos and organizations. |
39
|
|
|
*/ |
40
|
|
|
const SCOPE_PUBLIC_REPO = 'public_repo'; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Read/write access to public and private repos and organizations. |
44
|
|
|
* |
45
|
|
|
* Includes SCOPE_REPO_STATUS. |
46
|
|
|
*/ |
47
|
|
|
const SCOPE_REPO = 'repo'; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Grants access to deployment statuses for public and private repositories. |
51
|
|
|
* This scope is only necessary to grant other users or services access to deployment statuses, |
52
|
|
|
* without granting access to the code. |
53
|
|
|
*/ |
54
|
|
|
const SCOPE_REPO_DEPLOYMENT = 'repo_deployment'; |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Read/write access to public and private repository commit statuses. This scope is only necessary to grant other |
58
|
|
|
* users or services access to private repository commit statuses without granting access to the code. The repo and |
59
|
|
|
* public_repo scopes already include access to commit status for private and public repositories, respectively. |
60
|
|
|
*/ |
61
|
|
|
const SCOPE_REPO_STATUS = 'repo:status'; |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Delete access to adminable repositories. |
65
|
|
|
*/ |
66
|
|
|
const SCOPE_DELETE_REPO = 'delete_repo'; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Read access to a user’s notifications. repo is accepted too. |
70
|
|
|
*/ |
71
|
|
|
const SCOPE_NOTIFICATIONS = 'notifications'; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Write access to gists. |
75
|
|
|
*/ |
76
|
|
|
const SCOPE_GIST = 'gist'; |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Grants read and ping access to hooks in public or private repositories. |
80
|
|
|
*/ |
81
|
|
|
const SCOPE_HOOKS_READ = 'read:repo_hook'; |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Grants read, write, and ping access to hooks in public or private repositories. |
85
|
|
|
*/ |
86
|
|
|
const SCOPE_HOOKS_WRITE = 'write:repo_hook'; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Grants read, write, ping, and delete access to hooks in public or private repositories. |
90
|
|
|
*/ |
91
|
|
|
const SCOPE_HOOKS_ADMIN = 'admin:repo_hook'; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Read-only access to organization, teams, and membership. |
95
|
|
|
*/ |
96
|
|
|
const SCOPE_ORG_READ = 'read:org'; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Publicize and unpublicize organization membership. |
100
|
|
|
*/ |
101
|
|
|
const SCOPE_ORG_WRITE = 'write:org'; |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Fully manage organization, teams, and memberships. |
105
|
|
|
*/ |
106
|
|
|
const SCOPE_ORG_ADMIN = 'admin:org'; |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* List and view details for public keys. |
110
|
|
|
*/ |
111
|
|
|
const SCOPE_PUBLIC_KEY_READ = 'read:public_key'; |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Create, list, and view details for public keys. |
115
|
|
|
*/ |
116
|
|
|
const SCOPE_PUBLIC_KEY_WRITE = 'write:public_key'; |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Fully manage public keys. |
120
|
|
|
*/ |
121
|
|
|
const SCOPE_PUBLIC_KEY_ADMIN = 'admin:public_key'; |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
protected function init() |
127
|
|
|
{ |
128
|
|
|
if( $this->baseApiUri === null ) { |
|
|
|
|
129
|
|
|
$this->baseApiUri = new Uri('https://api.github.com/'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* {@inheritdoc} |
135
|
|
|
*/ |
136
|
|
|
public function getAuthorizationEndpoint() |
137
|
|
|
{ |
138
|
|
|
return new Uri('https://github.com/login/oauth/authorize'); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* {@inheritdoc} |
143
|
|
|
*/ |
144
|
|
|
public function getAccessTokenEndpoint() |
145
|
|
|
{ |
146
|
|
|
return new Uri('https://github.com/login/oauth/access_token'); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* {@inheritdoc} |
151
|
|
|
*/ |
152
|
|
|
protected function getAuthorizationMethod() |
153
|
|
|
{ |
154
|
|
|
return static::AUTHORIZATION_METHOD_QUERY_STRING; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* {@inheritdoc} |
159
|
|
|
*/ |
160
|
|
|
protected function parseAccessTokenResponse($responseBody) |
161
|
|
|
{ |
162
|
|
|
$data = json_decode($responseBody, true); |
163
|
|
|
|
164
|
|
|
if (null === $data || !is_array($data)) { |
165
|
|
|
throw new TokenResponseException('Unable to parse response.'); |
166
|
|
|
} elseif (isset($data['error'])) { |
167
|
|
|
throw new TokenResponseException('Error in retrieving token: "' . $data['error'] . '"'); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$token = new StdOAuth2Token(); |
171
|
|
|
$token->setAccessToken($data['access_token']); |
172
|
|
|
// Github tokens evidently never expire... |
173
|
|
|
$token->setEndOfLife(StdOAuth2Token::EOL_NEVER_EXPIRES); |
174
|
|
|
unset($data['access_token']); |
175
|
|
|
|
176
|
|
|
$token->setExtraParams($data); |
177
|
|
|
|
178
|
|
|
return $token; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Used to configure response type -- we want JSON from github, default is query string format |
183
|
|
|
* |
184
|
|
|
* @return array |
185
|
|
|
*/ |
186
|
|
|
protected function getExtraOAuthHeaders() |
187
|
|
|
{ |
188
|
|
|
return array('Accept' => 'application/json'); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Required for GitHub API calls. |
193
|
|
|
* |
194
|
|
|
* @return array |
195
|
|
|
*/ |
196
|
|
|
protected function getExtraApiHeaders() |
197
|
|
|
{ |
198
|
|
|
return array('Accept' => 'application/vnd.github.beta+json'); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* {@inheritdoc} |
203
|
|
|
*/ |
204
|
|
|
protected function getScopesDelimiter() |
205
|
|
|
{ |
206
|
|
|
return ','; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|