Skip to content

Update Authorizations API #277

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion doc/authorizations.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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);
```
98 changes: 87 additions & 11 deletions lib/Github/Api/Authorizations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,120 @@

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 <[email protected]>
*/
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);

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');
}
}
81 changes: 64 additions & 17 deletions test/Github/Tests/Api/AuthorizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand All @@ -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));
Expand All @@ -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()
Expand Down