diff --git a/doc/authorizations.md b/doc/authorizations.md index 037bca839d9..2865fd63e19 100644 --- a/doc/authorizations.md +++ b/doc/authorizations.md @@ -1,7 +1,7 @@ ## Authorizations API [Back to the navigation](README.md) -Creating, deleting and listing authorizations. Wraps [GitHub Authorizations API](http://developer.github.com/v3/oauth/). +Creating, deleting and listing authorizations. Wraps [GitHub Authorizations API](http://developer.github.com/v3/oauth_authorizations/). #### List all authorizations. @@ -50,3 +50,21 @@ $authorization = $github->api('authorizations')->remove(1234); ```php $authorization = $github->api('authorizations')->check(1234, 'token'); ``` + +#### Reset an authorization + +```php +$authorization = $github->api('authorizations')->reset(1234, 'token'); +``` + +#### Revoke an authorization + +```php +$github->api('authorizations')->revoke(1234, 'token'); +``` + +#### Revoke all authorizations + +```php +$github->api('authorizations')->revokeAll(1234); +``` diff --git a/lib/Github/Api/Authorizations.php b/lib/Github/Api/Authorizations.php index 014e08b4486..b8c4f5bd8fe 100644 --- a/lib/Github/Api/Authorizations.php +++ b/lib/Github/Api/Authorizations.php @@ -2,25 +2,44 @@ namespace Github\Api; -use Github\Api\AbstractApi; - /** * Creating, deleting and listing authorizations. * - * @link http://developer.github.com/v3/oauth/ + * @link http://developer.github.com/v3/oauth_authorizations/ + * @author Evgeniy Guseletov */ class Authorizations extends AbstractApi { + /** + * List all authorizations. + * + * @return array + */ public function all() { return $this->get('authorizations'); } - public function show($number) + /** + * Show a single authorization. + * + * @param $clientId + * + * @return array + */ + public function show($clientId) { - return $this->get('authorizations/'.rawurlencode($number)); + return $this->get('authorizations/'.rawurlencode($clientId)); } + /** + * Create an authorization. + * + * @param array $params + * @param null $OTPCode + * + * @return array + */ public function create(array $params, $OTPCode = null) { $headers = null === $OTPCode ? array() : array('X-GitHub-OTP' => $OTPCode); @@ -28,18 +47,75 @@ public function create(array $params, $OTPCode = null) return $this->post('authorizations', $params, $headers); } - public function update($id, array $params) + /** + * Update an authorization. + * + * @param $clientId + * @param array $params + * + * @return array + */ + public function update($clientId, array $params) + { + return $this->patch('authorizations/'.rawurlencode($clientId), $params); + } + + /** + * Remove an authorization. + * + * @param $clientId + * + * @return array + */ + public function remove($clientId) + { + return $this->delete('authorizations/'.rawurlencode($clientId)); + } + + /** + * Check an authorization. + * + * @param $clientId + * @param $token + * + * @return array + */ + public function check($clientId, $token) + { + return $this->get('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token)); + } + + /** + * Reset an authorization. + * + * @param $clientId + * @param $token + * + * @return array + */ + public function reset($clientId, $token) { - return $this->patch('authorizations/'.rawurlencode($id), $params); + return $this->post('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token)); } - public function remove($id) + /** + * Remove an authorization. + * + * @param $clientId + * @param $token + */ + public function revoke($clientId, $token) { - return $this->delete('authorizations/'.rawurlencode($id)); + $this->delete('applications/'.rawurlencode($clientId).'/tokens/'.rawurlencode($token)); } - public function check($id, $token) + /** + * Revoke all authorizations. + * + * @param $clientId + */ + public function revokeAll($clientId) { - return $this->get('authorizations/'.rawurlencode($id).'/tokens/'.rawurlencode($token)); + $this->delete('applications/'.rawurlencode($clientId).'/tokens'); } } diff --git a/test/Github/Tests/Api/AuthorizationsTest.php b/test/Github/Tests/Api/AuthorizationsTest.php index 89534e50965..059c2ffe7f8 100644 --- a/test/Github/Tests/Api/AuthorizationsTest.php +++ b/test/Github/Tests/Api/AuthorizationsTest.php @@ -37,6 +37,55 @@ public function shouldShowAuthorization() $this->assertEquals($expectedArray, $api->show($id)); } + /** + * @test + */ + public function shouldAuthorization() + { + $input = array( + 'note' => '', + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('post') + ->with('authorizations', $input); + + $api->create($input); + } + + /** + * @test + */ + public function shouldUpdateAuthorization() + { + $id = 123; + $input = array( + 'note' => '', + ); + + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('patch') + ->with('authorizations/'.$id, $input); + + $api->update($id, $input); + } + + /** + * @test + */ + public function shouldDeleteAuthorization() + { + $id = 123; + $api = $this->getApiMock(); + $api->expects($this->once()) + ->method('delete') + ->with('authorizations/'.$id); + + $api->remove($id); + } + /** * @test */ @@ -49,7 +98,7 @@ public function shouldCheckAuthorization() $api = $this->getApiMock(); $api->expects($this->once()) ->method('get') - ->with('authorizations/'.$id.'/tokens/'.$token) + ->with('applications/'.$id.'/tokens/'.$token) ->will($this->returnValue($expectedArray)); $this->assertEquals($expectedArray, $api->check($id, $token)); @@ -58,50 +107,48 @@ public function shouldCheckAuthorization() /** * @test */ - public function shouldAuthorization() + public function shouldResetAuthorization() { - $input = array( - 'note' => '', - ); + $id = 123; + $token = 'abcde'; $api = $this->getApiMock(); $api->expects($this->once()) ->method('post') - ->with('authorizations', $input); + ->with('applications/'.$id.'/tokens/'.$token); - $api->create($input); + $api->reset($id, $token); } /** * @test */ - public function shouldUpdateAuthorization() + public function shouldRevokeAuthorization() { $id = 123; - $input = array( - 'note' => '', - ); + $token = 'abcde'; $api = $this->getApiMock(); $api->expects($this->once()) - ->method('patch') - ->with('authorizations/'.$id, $input); + ->method('delete') + ->with('applications/'.$id.'/tokens/'.$token); - $api->update($id, $input); + $api->revoke($id, $token); } /** * @test */ - public function shouldDeleteAuthorization() + public function shouldRevokeAllAuthorizations() { $id = 123; + $api = $this->getApiMock(); $api->expects($this->once()) ->method('delete') - ->with('authorizations/'.$id); + ->with('applications/'.$id.'/tokens'); - $api->remove($id); + $api->revokeAll($id); } protected function getApiClass()