Skip to content

Commit

Permalink
Merge pull request #849 from GrahamCampbell/depr-authorizations
Browse files Browse the repository at this point in the history
Support the new authorizations API and deprecate old one
  • Loading branch information
acrobat committed Mar 11, 2020
2 parents 784b3e9 + 2fe12b8 commit a815092
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 2 deletions.
89 changes: 87 additions & 2 deletions lib/Github/Api/Authorizations.php
Expand Up @@ -11,10 +11,19 @@
*/
class Authorizations extends AbstractApi
{
use AcceptHeaderTrait;

private function configurePreviewHeader()
{
$this->acceptHeaderValue = 'application/vnd.github.doctor-strange-preview+json';
}

/**
* List all authorizations.
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
*/
public function all()
{
Expand All @@ -27,6 +36,8 @@ public function all()
* @param string $clientId
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
*/
public function show($clientId)
{
Expand All @@ -36,10 +47,12 @@ public function show($clientId)
/**
* Create an authorization.
*
* @param array $params
* @param null $OTPCode
* @param array $params
* @param string|null $OTPCode
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
*/
public function create(array $params, $OTPCode = null)
{
Expand All @@ -55,6 +68,8 @@ public function create(array $params, $OTPCode = null)
* @param array $params
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
*/
public function update($clientId, array $params)
{
Expand All @@ -67,6 +82,8 @@ public function update($clientId, array $params)
* @param string $clientId
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 13th November 2020. No replacement will be offered. The "web application flow" should be used instead.
*/
public function remove($clientId)
{
Expand All @@ -80,30 +97,66 @@ public function remove($clientId)
* @param string $token
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::checkToken() instead.
*/
public function check($clientId, $token)
{
return $this->get('/applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
}

/**
* Check an application token.
*
* @param string $clientId
* @param string|null $token
*
* @return array
*/
public function checkToken($clientId, $token = null)
{
$this->configurePreviewHeader();

return $this->post('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
}

/**
* Reset an authorization.
*
* @param string $clientId
* @param string $token
*
* @return array
*
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::resetToken() instead.
*/
public function reset($clientId, $token)
{
return $this->post('/applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token));
}

/**
* Reset an application token.
*
* @param string $clientId
* @param string|null $token
*
* @return array
*/
public function resetToken($clientId, $token = null)
{
$this->configurePreviewHeader();

return $this->patch('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
}

/**
* Remove an authorization.
*
* @param string $clientId
* @param string $token
*
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::deleteToken() instead.
*/
public function revoke($clientId, $token)
{
Expand All @@ -114,9 +167,41 @@ public function revoke($clientId, $token)
* Revoke all authorizations.
*
* @param string $clientId
*
* @deprecated GitHub will remove this endpoint on 1st July 2020. Use self::deleteGrant() instead.
*/
public function revokeAll($clientId)
{
$this->delete('/applications/'.rawurlencode($clientId).'/tokens');
}

/**
* Revoke an application token.
*
* @param string $clientId
* @param string|null $token
*
* @return void
*/
public function deleteToken($clientId, $token = null)
{
$this->configurePreviewHeader();

$this->delete('/applications/'.rawurlencode($clientId).'/token', $token ? ['access_token' => $token] : []);
}

/**
* Revoke an application authorization.
*
* @param string $clientId
* @param string|null $token
*
* @return void
*/
public function deleteGrant($clientId, $token = null)
{
$this->configurePreviewHeader();

$this->delete('/applications/'.rawurlencode($clientId).'/grant', $token ? ['access_token' => $token] : []);
}
}
68 changes: 68 additions & 0 deletions test/Github/Tests/Api/AuthorizationsTest.php
Expand Up @@ -86,6 +86,24 @@ public function shouldDeleteAuthorization()
$api->remove($id);
}

/**
* @test
*/
public function shouldCheckApplicationToken()
{
$id = 123;
$token = 'abc';
$expectedArray = ['id' => $id];

$api = $this->getApiMock();
$api->expects($this->once())
->method('post')
->with('/applications/'.$id.'/token', ['access_token' => $token])
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->checkToken($id, $token));
}

/**
* @test
*/
Expand Down Expand Up @@ -120,6 +138,24 @@ public function shouldResetAuthorization()
$api->reset($id, $token);
}

/**
* @test
*/
public function shouldResetApplicationToken()
{
$id = 123;
$token = 'abcde';
$expectedArray = ['id' => $id];

$api = $this->getApiMock();
$api->expects($this->once())
->method('patch')
->with('/applications/'.$id.'/token', ['access_token' => $token])
->will($this->returnValue($expectedArray));

$this->assertEquals($expectedArray, $api->resetToken($id, $token));
}

/**
* @test
*/
Expand Down Expand Up @@ -151,6 +187,38 @@ public function shouldRevokeAllAuthorizations()
$api->revokeAll($id);
}

/**
* @test
*/
public function shouldDeleteApplicationToken()
{
$id = 123;
$token = 'abcde';

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('/applications/'.$id.'/token', ['access_token' => $token]);

$api->deleteToken($id, $token);
}

/**
* @test
*/
public function shouldDeleteApplicationAuthorization()
{
$id = 123;
$token = 'abcde';

$api = $this->getApiMock();
$api->expects($this->once())
->method('delete')
->with('/applications/'.$id.'/grant', ['access_token' => $token]);

$api->deleteGrant($id, $token);
}

/**
* @return string
*/
Expand Down

0 comments on commit a815092

Please sign in to comment.