Skip to content

Commit a815092

Browse files
authored
Merge pull request KnpLabs#849 from GrahamCampbell/depr-authorizations
Support the new authorizations API and deprecate old one
2 parents 784b3e9 + 2fe12b8 commit a815092

File tree

2 files changed

+155
-2
lines changed

2 files changed

+155
-2
lines changed

lib/Github/Api/Authorizations.php

Lines changed: 87 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,19 @@
1111
*/
1212
class Authorizations extends AbstractApi
1313
{
14+
use AcceptHeaderTrait;
15+
16+
private function configurePreviewHeader()
17+
{
18+
$this->acceptHeaderValue = 'application/vnd.github.doctor-strange-preview+json';
19+
}
20+
1421
/**
1522
* List all authorizations.
1623
*
1724
* @return array
25+
*
26+
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
1827
*/
1928
public function all()
2029
{
@@ -27,6 +36,8 @@ public function all()
2736
* @param string $clientId
2837
*
2938
* @return array
39+
*
40+
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
3041
*/
3142
public function show($clientId)
3243
{
@@ -36,10 +47,12 @@ public function show($clientId)
3647
/**
3748
* Create an authorization.
3849
*
39-
* @param array $params
40-
* @param null $OTPCode
50+
* @param array $params
51+
* @param string|null $OTPCode
4152
*
4253
* @return array
54+
*
55+
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
4356
*/
4457
public function create(array $params, $OTPCode = null)
4558
{
@@ -55,6 +68,8 @@ public function create(array $params, $OTPCode = null)
5568
* @param array $params
5669
*
5770
* @return array
71+
*
72+
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
5873
*/
5974
public function update($clientId, array $params)
6075
{
@@ -67,6 +82,8 @@ public function update($clientId, array $params)
6782
* @param string $clientId
6883
*
6984
* @return array
85+
*
86+
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
7087
*/
7188
public function remove($clientId)
7289
{
@@ -80,30 +97,66 @@ public function remove($clientId)
8097
* @param string $token
8198
*
8299
* @return array
100+
*
101+
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::checkToken() instead.
83102
*/
84103
public function check($clientId, $token)
85104
{
86105
return $this->get('/applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
87106
}
88107

108+
/**
109+
* Check an application token.
110+
*
111+
* @param string $clientId
112+
* @param string|null $token
113+
*
114+
* @return array
115+
*/
116+
public function checkToken($clientId, $token = null)
117+
{
118+
$this->configurePreviewHeader();
119+
120+
return $this->post('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
121+
}
122+
89123
/**
90124
* Reset an authorization.
91125
*
92126
* @param string $clientId
93127
* @param string $token
94128
*
95129
* @return array
130+
*
131+
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::resetToken() instead.
96132
*/
97133
public function reset($clientId, $token)
98134
{
99135
return $this->post('/applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
100136
}
101137

138+
/**
139+
* Reset an application token.
140+
*
141+
* @param string $clientId
142+
* @param string|null $token
143+
*
144+
* @return array
145+
*/
146+
public function resetToken($clientId, $token = null)
147+
{
148+
$this->configurePreviewHeader();
149+
150+
return $this->patch('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
151+
}
152+
102153
/**
103154
* Remove an authorization.
104155
*
105156
* @param string $clientId
106157
* @param string $token
158+
*
159+
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::deleteToken() instead.
107160
*/
108161
public function revoke($clientId, $token)
109162
{
@@ -114,9 +167,41 @@ public function revoke($clientId, $token)
114167
* Revoke all authorizations.
115168
*
116169
* @param string $clientId
170+
*
171+
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::deleteGrant() instead.
117172
*/
118173
public function revokeAll($clientId)
119174
{
120175
$this->delete('/applications/'.rawurlencode($clientId).'/tokens');
121176
}
177+
178+
/**
179+
* Revoke an application token.
180+
*
181+
* @param string $clientId
182+
* @param string|null $token
183+
*
184+
* @return void
185+
*/
186+
public function deleteToken($clientId, $token = null)
187+
{
188+
$this->configurePreviewHeader();
189+
190+
$this->delete('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
191+
}
192+
193+
/**
194+
* Revoke an application authorization.
195+
*
196+
* @param string $clientId
197+
* @param string|null $token
198+
*
199+
* @return void
200+
*/
201+
public function deleteGrant($clientId, $token = null)
202+
{
203+
$this->configurePreviewHeader();
204+
205+
$this->delete('/applications/'.rawurlencode($clientId).'/grant', $token ? ['access_token' => $token] : []);
206+
}
122207
}

test/Github/Tests/Api/AuthorizationsTest.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,24 @@ public function shouldDeleteAuthorization()
8686
$api->remove($id);
8787
}
8888

89+
/**
90+
* @test
91+
*/
92+
public function shouldCheckApplicationToken()
93+
{
94+
$id = 123;
95+
$token = 'abc';
96+
$expectedArray = ['id' => $id];
97+
98+
$api = $this->getApiMock();
99+
$api->expects($this->once())
100+
->method('post')
101+
->with('/applications/'.$id.'/token', ['access_token' => $token])
102+
->will($this->returnValue($expectedArray));
103+
104+
$this->assertEquals($expectedArray, $api->checkToken($id, $token));
105+
}
106+
89107
/**
90108
* @test
91109
*/
@@ -120,6 +138,24 @@ public function shouldResetAuthorization()
120138
$api->reset($id, $token);
121139
}
122140

141+
/**
142+
* @test
143+
*/
144+
public function shouldResetApplicationToken()
145+
{
146+
$id = 123;
147+
$token = 'abcde';
148+
$expectedArray = ['id' => $id];
149+
150+
$api = $this->getApiMock();
151+
$api->expects($this->once())
152+
->method('patch')
153+
->with('/applications/'.$id.'/token', ['access_token' => $token])
154+
->will($this->returnValue($expectedArray));
155+
156+
$this->assertEquals($expectedArray, $api->resetToken($id, $token));
157+
}
158+
123159
/**
124160
* @test
125161
*/
@@ -151,6 +187,38 @@ public function shouldRevokeAllAuthorizations()
151187
$api->revokeAll($id);
152188
}
153189

190+
/**
191+
* @test
192+
*/
193+
public function shouldDeleteApplicationToken()
194+
{
195+
$id = 123;
196+
$token = 'abcde';
197+
198+
$api = $this->getApiMock();
199+
$api->expects($this->once())
200+
->method('delete')
201+
->with('/applications/'.$id.'/token', ['access_token' => $token]);
202+
203+
$api->deleteToken($id, $token);
204+
}
205+
206+
/**
207+
* @test
208+
*/
209+
public function shouldDeleteApplicationAuthorization()
210+
{
211+
$id = 123;
212+
$token = 'abcde';
213+
214+
$api = $this->getApiMock();
215+
$api->expects($this->once())
216+
->method('delete')
217+
->with('/applications/'.$id.'/grant', ['access_token' => $token]);
218+
219+
$api->deleteGrant($id, $token);
220+
}
221+
154222
/**
155223
* @return string
156224
*/

0 commit comments

Comments
 (0)