Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 4 additions & 4 deletions UPGRADE-3.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

### Authentication methods

* `Github\Client::AUTH_URL_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
* `Github\Client::AUTH_URL_CLIENT_ID` use `Github\Client::AUTH_CLIENT_ID` instead.
* `Github\Client::AUTH_HTTP_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
* `Github\Client::AUTH_HTTP_PASSWORD` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.
* `Github\Client::AUTH_CLIENT_ID` use `Github\AuthMethod::AUTH_CLIENT_ID` instead.
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.
6 changes: 3 additions & 3 deletions doc/currentuser/repositories.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ There are three values that can be passed into the `repositories` method: `type`
| sort | `full_name` | `created`, `updated`, `pushed`, `full_name`
| direction | `asc` | `asc`, `desc`

> See https://developer.github.com/v3/repos/#list-your-repositories for possible values and additional information
> See https://developer.github.com/v3/repos/#list-your-repositories for possible values and additional information

#### Code Example:

```php
$client = new \Github\Client();
$client->authenticate($github_token, null, \Github\Client::AUTH_ACCESS_TOKEN);
$client = new \Github\Client();
$client->authenticate($github_token, null, \Github\AuthMethod::AUTH_ACCESS_TOKEN);
$client->currentUser()->repositories();
```
6 changes: 3 additions & 3 deletions doc/graphql.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ $rateLimits = $client->api('graphql')->execute($query);
To use [GitHub v4 API (GraphQL API)](http://developer.github.com/v4/) requests must [authenticated]((../security.md)).

```php
$client->authenticate($token, null, Github\Client::AUTH_ACCESS_TOKEN);
$client->authenticate($token, null, Github\AuthMethod::AUTH_ACCESS_TOKEN);

$result = $client->api('graphql')->execute($query);
```
Expand All @@ -28,7 +28,7 @@ To use [GitHub v4 API (GraphQL API)](http://developer.github.com/v4/) with diffe
```php
$result = $client->api('graphql')->execute($query, [], 'application/vnd.github.starfox-preview+json')
```
> default accept header is `application/vnd.github.v4+json`
> default accept header is `application/vnd.github.v4+json`



Expand All @@ -51,7 +51,7 @@ $variables = [
'organizationLogin' => 'KnpLabs'
];

$client->authenticate('<your-token>', null, Github\Client::AUTH_ACCESS_TOKEN);
$client->authenticate('<your-token>', null, Github\AuthMethod::AUTH_ACCESS_TOKEN);

$orgInfo = $client->api('graphql')->execute($query, $variables);
```
10 changes: 5 additions & 5 deletions doc/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ $client->authenticate($usernameOrToken, $password, $method);
and guess what should contain `$password`. The `$method` can contain one of the three allowed values:

#### Supported methods
* `Github\Client::AUTH_CLIENT_ID` - https://developer.github.com/v3/#oauth2-keysecret
* `Github\Client::AUTH_ACCESS_TOKEN` - https://developer.github.com/v3/#oauth2-token-sent-in-a-header
* `Github\Client::AUTH_JWT` - https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app
* `Github\AuthMethod::AUTH_CLIENT_ID` - https://developer.github.com/v3/#oauth2-keysecret
* `Github\AuthMethod::AUTH_ACCESS_TOKEN` - https://developer.github.com/v3/#oauth2-token-sent-in-a-header
* `Github\AuthMethod::AUTH_JWT` - https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app

The required value of `$password` depends on the chosen `$method`. For `Github\Client::AUTH_ACCESS_TOKEN`, `Github\Client::AUTH_ACCESS_TOKEN` and
The required value of `$password` depends on the chosen `$method`. For `Github\AuthMethod::AUTH_ACCESS_TOKEN`, `Github\Client::AUTH_ACCESS_TOKEN` and
`Github\Client::JWT` methods you should provide the API token in `$usernameOrToken` variable (`$password` is omitted in
this particular case).

Expand All @@ -33,7 +33,7 @@ further requests are done as the given user.
### Authenticating as an Integration

To authenticate as an integration you need to supply a JSON Web Token with `Github\Client::AUTH_JWT` to request
and installation access token which is then usable with `Github\Client::AUTH_ACCESS_TOKEN`. [Github´s integration
and installation access token which is then usable with `Github\AuthMethod::AUTH_ACCESS_TOKEN`. [Github´s integration
authentication docs](https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/#authenticating-as-a-github-app) describe the flow in detail.
It´s important for integration requests to use the custom Accept header `application/vnd.github.machine-man-preview`.

Expand Down
30 changes: 30 additions & 0 deletions lib/Github/AuthMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Github;

final class AuthMethod
{
/**
* Authenticate using a client_id/client_secret combination.
*
* @var string
*/
const AUTH_CLIENT_ID = 'client_id_header';

/**
* Authenticate using a GitHub access token.
*
* @var string
*/
const AUTH_ACCESS_TOKEN = 'access_token_header';

/**
* Constant for authentication method.
*
* Indicates JSON Web Token authentication required for GitHub apps access
* to the API.
*
* @var string
*/
const AUTH_JWT = 'jwt';
}
8 changes: 7 additions & 1 deletion lib/Github/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,17 @@ class Client
* Authenticate using a client_id/client_secret combination.
*
* @var string
*
* @deprecated Use the AuthMethod const
*/
const AUTH_CLIENT_ID = 'client_id_header';

/**
* Authenticate using a GitHub access token.
*
* @var string
*
* @deprecated Use the AuthMethod const
*/
const AUTH_ACCESS_TOKEN = 'access_token_header';

Expand All @@ -90,6 +94,8 @@ class Client
* to the API.
*
* @var string
*
* @deprecated Use the AuthMethod const
*/
const AUTH_JWT = 'jwt';

Expand Down Expand Up @@ -313,7 +319,7 @@ public function api($name): AbstractApi
*/
public function authenticate($tokenOrLogin, $password = null, $authMethod = null): void
{
if (null === $authMethod && (self::AUTH_JWT === $password || self::AUTH_ACCESS_TOKEN === $password)) {
if (null === $authMethod && (AuthMethod::AUTH_JWT === $password || AuthMethod::AUTH_ACCESS_TOKEN === $password)) {
$authMethod = $password;
$password = null;
}
Expand Down
8 changes: 4 additions & 4 deletions lib/Github/HttpClient/Plugin/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Github\HttpClient\Plugin;

use Github\Client;
use Github\AuthMethod;
use Github\Exception\RuntimeException;
use Http\Client\Common\Plugin;
use Http\Promise\Promise;
Expand Down Expand Up @@ -58,11 +58,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
private function getAuthorizationHeader(): string
{
switch ($this->method) {
case Client::AUTH_CLIENT_ID:
case AuthMethod::AUTH_CLIENT_ID:
return sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password));
case Client::AUTH_ACCESS_TOKEN:
case AuthMethod::AUTH_ACCESS_TOKEN:
return sprintf('token %s', $this->tokenOrLogin);
case Client::AUTH_JWT:
case AuthMethod::AUTH_JWT:
return sprintf('Bearer %s', $this->tokenOrLogin);
default:
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
Expand Down
11 changes: 6 additions & 5 deletions test/Github/Tests/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Github\Tests;

use Github\Api;
use Github\AuthMethod;
use Github\Client;
use Github\Exception\BadMethodCallException;
use Github\Exception\InvalidArgumentException;
Expand Down Expand Up @@ -68,9 +69,9 @@ public function shouldAuthenticateUsingAllGivenParameters($login, $password, $me
public function getAuthenticationFullData()
{
return [
['token', null, Client::AUTH_ACCESS_TOKEN],
['client_id', 'client_secret', Client::AUTH_CLIENT_ID],
['token', null, Client::AUTH_JWT],
['token', null, AuthMethod::AUTH_ACCESS_TOKEN],
['client_id', 'client_secret', AuthMethod::AUTH_CLIENT_ID],
['token', null, AuthMethod::AUTH_JWT],
];
}

Expand All @@ -84,7 +85,7 @@ public function shouldAuthenticateUsingGivenParameters()
->getMock();
$builder->expects($this->once())
->method('addPlugin')
->with($this->equalTo(new Authentication('token', null, Client::AUTH_ACCESS_TOKEN)));
->with($this->equalTo(new Authentication('token', null, AuthMethod::AUTH_ACCESS_TOKEN)));

$builder->expects($this->once())
->method('removePlugin')
Expand All @@ -98,7 +99,7 @@ public function shouldAuthenticateUsingGivenParameters()
->method('getHttpClientBuilder')
->willReturn($builder);

$client->authenticate('token', Client::AUTH_ACCESS_TOKEN);
$client->authenticate('token', AuthMethod::AUTH_ACCESS_TOKEN);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions test/Github/Tests/Functional/CacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Github\Tests\Functional;

use Github\AuthMethod;
use Github\Client;
use GuzzleHttp\Psr7\Response;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
Expand All @@ -25,7 +26,7 @@ public function shouldServeCachedResponse()
$github = Client::createWithHttpClient($mockClient);
$github->addCache(new ArrayAdapter(), ['default_ttl'=>600]);

$github->authenticate('fake_token_aaa', Client::AUTH_ACCESS_TOKEN);
$github->authenticate('fake_token_aaa', AuthMethod::AUTH_ACCESS_TOKEN);
$userA = $github->currentUser()->show();
$this->assertEquals('nyholm', $userA['login']);

Expand All @@ -45,11 +46,11 @@ public function shouldVaryOnAuthorization()
$github = Client::createWithHttpClient($mockClient);
$github->addCache(new ArrayAdapter(), ['default_ttl'=>600]);

$github->authenticate('fake_token_aaa', Client::AUTH_ACCESS_TOKEN);
$github->authenticate('fake_token_aaa', AuthMethod::AUTH_ACCESS_TOKEN);
$userA = $github->currentUser()->show();
$this->assertEquals('nyholm', $userA['login']);

$github->authenticate('fake_token_bbb', Client::AUTH_ACCESS_TOKEN);
$github->authenticate('fake_token_bbb', AuthMethod::AUTH_ACCESS_TOKEN);
$userB = $github->currentUser()->show();
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.');
}
Expand Down
8 changes: 4 additions & 4 deletions test/Github/Tests/HttpClient/Plugin/AuthenticationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Github\Tests\HttpClient\Plugin;

use Github\Client;
use Github\AuthMethod;
use Github\HttpClient\Plugin\Authentication;
use GuzzleHttp\Psr7\Request;
use Http\Promise\FulfilledPromise;
Expand Down Expand Up @@ -41,9 +41,9 @@ public function testAuthenticationMethods($tokenOrLogin, $password, $method, $ex
public function getAuthenticationData()
{
return [
['access_token', null, Client::AUTH_ACCESS_TOKEN, 'token access_token'],
['client_id', 'client_secret', Client::AUTH_CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
['jwt_token', null, Client::AUTH_JWT, 'Bearer jwt_token'],
['access_token', null, AuthMethod::AUTH_ACCESS_TOKEN, 'token access_token'],
['client_id', 'client_secret', AuthMethod::AUTH_CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
['jwt_token', null, AuthMethod::AUTH_JWT, 'Bearer jwt_token'],
];
}
}