Skip to content

Commit 11c2d9f

Browse files
committed
refs #955: deprecate Client::AUTH_* constants and replace them with AuthMethod::AUTH_* const
1 parent 0349838 commit 11c2d9f

File tree

10 files changed

+64
-56
lines changed

10 files changed

+64
-56
lines changed

UPGRADE-3.0.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
### Authentication methods
1717

18-
* `Github\Client::AUTH_URL_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
19-
* `Github\Client::AUTH_URL_CLIENT_ID` use `Github\Client::AUTH_CLIENT_ID` instead.
20-
* `Github\Client::AUTH_HTTP_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
21-
* `Github\Client::AUTH_HTTP_PASSWORD` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
18+
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.
19+
* `Github\Client::AUTH_CLIENT_ID` use `Github\AuthMethod::AUTH_CLIENT_ID` instead.
20+
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.
21+
* `Github\Client::AUTH_ACCESS_TOKEN` use `Github\AuthMethod::AUTH_ACCESS_TOKEN` instead.

doc/currentuser/repositories.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ There are three values that can be passed into the `repositories` method: `type`
1919
| sort | `full_name` | `created`, `updated`, `pushed`, `full_name`
2020
| direction | `asc` | `asc`, `desc`
2121

22-
> See https://developer.github.com/v3/repos/#list-your-repositories for possible values and additional information
22+
> See https://developer.github.com/v3/repos/#list-your-repositories for possible values and additional information
2323
2424
#### Code Example:
2525

2626
```php
27-
$client = new \Github\Client();
28-
$client->authenticate($github_token, null, \Github\Client::AUTH_ACCESS_TOKEN);
27+
$client = new \Github\Client();
28+
$client->authenticate($github_token, null, \Github\AuthMethod::AUTH_ACCESS_TOKEN);
2929
$client->currentUser()->repositories();
3030
```

doc/graphql.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ $rateLimits = $client->api('graphql')->execute($query);
1414
To use [GitHub v4 API (GraphQL API)](http://developer.github.com/v4/) requests must [authenticated]((../security.md)).
1515

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

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

3434

@@ -51,7 +51,7 @@ $variables = [
5151
'organizationLogin' => 'KnpLabs'
5252
];
5353

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

5656
$orgInfo = $client->api('graphql')->execute($query, $variables);
5757
```

doc/security.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ $client->authenticate($usernameOrToken, $password, $method);
1717
and guess what should contain `$password`. The `$method` can contain one of the three allowed values:
1818

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

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

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

3535
To authenticate as an integration you need to supply a JSON Web Token with `Github\Client::AUTH_JWT` to request
36-
and installation access token which is then usable with `Github\Client::AUTH_ACCESS_TOKEN`. [Github´s integration
36+
and installation access token which is then usable with `Github\AuthMethod::AUTH_ACCESS_TOKEN`. [Github´s integration
3737
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.
3838
It´s important for integration requests to use the custom Accept header `application/vnd.github.machine-man-preview`.
3939

lib/Github/AuthMethod.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace Github;
4+
5+
final class AuthMethod
6+
{
7+
/**
8+
* Authenticate using a client_id/client_secret combination.
9+
*
10+
* @var string
11+
*/
12+
const AUTH_CLIENT_ID = 'client_id_header';
13+
14+
/**
15+
* Authenticate using a GitHub access token.
16+
*
17+
* @var string
18+
*/
19+
const AUTH_ACCESS_TOKEN = 'access_token_header';
20+
21+
/**
22+
* Constant for authentication method.
23+
*
24+
* Indicates JSON Web Token authentication required for GitHub apps access
25+
* to the API.
26+
*
27+
* @var string
28+
*/
29+
const AUTH_JWT = 'jwt';
30+
}

lib/Github/Client.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -69,30 +69,6 @@
6969
*/
7070
class Client
7171
{
72-
/**
73-
* Authenticate using a client_id/client_secret combination.
74-
*
75-
* @var string
76-
*/
77-
const AUTH_CLIENT_ID = 'client_id_header';
78-
79-
/**
80-
* Authenticate using a GitHub access token.
81-
*
82-
* @var string
83-
*/
84-
const AUTH_ACCESS_TOKEN = 'access_token_header';
85-
86-
/**
87-
* Constant for authentication method.
88-
*
89-
* Indicates JSON Web Token authentication required for GitHub apps access
90-
* to the API.
91-
*
92-
* @var string
93-
*/
94-
const AUTH_JWT = 'jwt';
95-
9672
/**
9773
* @var string
9874
*/
@@ -313,7 +289,7 @@ public function api($name): AbstractApi
313289
*/
314290
public function authenticate($tokenOrLogin, $password = null, $authMethod = null): void
315291
{
316-
if (null === $authMethod && (self::AUTH_JWT === $password || self::AUTH_ACCESS_TOKEN === $password)) {
292+
if (null === $authMethod && (AuthMethod::AUTH_JWT === $password || AuthMethod::AUTH_ACCESS_TOKEN === $password)) {
317293
$authMethod = $password;
318294
$password = null;
319295
}

lib/Github/HttpClient/Plugin/Authentication.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Github\HttpClient\Plugin;
44

5-
use Github\Client;
5+
use Github\AuthMethod;
66
use Github\Exception\RuntimeException;
77
use Http\Client\Common\Plugin;
88
use Http\Promise\Promise;
@@ -58,11 +58,11 @@ public function handleRequest(RequestInterface $request, callable $next, callabl
5858
private function getAuthorizationHeader(): string
5959
{
6060
switch ($this->method) {
61-
case Client::AUTH_CLIENT_ID:
61+
case AuthMethod::AUTH_CLIENT_ID:
6262
return sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password));
63-
case Client::AUTH_ACCESS_TOKEN:
63+
case AuthMethod::AUTH_ACCESS_TOKEN:
6464
return sprintf('token %s', $this->tokenOrLogin);
65-
case Client::AUTH_JWT:
65+
case AuthMethod::AUTH_JWT:
6666
return sprintf('Bearer %s', $this->tokenOrLogin);
6767
default:
6868
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));

test/Github/Tests/ClientTest.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Github\Tests;
44

55
use Github\Api;
6+
use Github\AuthMethod;
67
use Github\Client;
78
use Github\Exception\BadMethodCallException;
89
use Github\Exception\InvalidArgumentException;
@@ -68,9 +69,9 @@ public function shouldAuthenticateUsingAllGivenParameters($login, $password, $me
6869
public function getAuthenticationFullData()
6970
{
7071
return [
71-
['token', null, Client::AUTH_ACCESS_TOKEN],
72-
['client_id', 'client_secret', Client::AUTH_CLIENT_ID],
73-
['token', null, Client::AUTH_JWT],
72+
['token', null, AuthMethod::AUTH_ACCESS_TOKEN],
73+
['client_id', 'client_secret', AuthMethod::AUTH_CLIENT_ID],
74+
['token', null, AuthMethod::AUTH_JWT],
7475
];
7576
}
7677

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

8990
$builder->expects($this->once())
9091
->method('removePlugin')
@@ -98,7 +99,7 @@ public function shouldAuthenticateUsingGivenParameters()
9899
->method('getHttpClientBuilder')
99100
->willReturn($builder);
100101

101-
$client->authenticate('token', Client::AUTH_ACCESS_TOKEN);
102+
$client->authenticate('token', AuthMethod::AUTH_ACCESS_TOKEN);
102103
}
103104

104105
/**

test/Github/Tests/Functional/CacheTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Github\Tests\Functional;
44

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

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

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

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

52-
$github->authenticate('fake_token_bbb', Client::AUTH_ACCESS_TOKEN);
53+
$github->authenticate('fake_token_bbb', AuthMethod::AUTH_ACCESS_TOKEN);
5354
$userB = $github->currentUser()->show();
5455
$this->assertEquals('octocat', $userB['login'], 'We must vary on the Authorization header.');
5556
}

test/Github/Tests/HttpClient/Plugin/AuthenticationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Github\Tests\HttpClient\Plugin;
44

5-
use Github\Client;
5+
use Github\AuthMethod;
66
use Github\HttpClient\Plugin\Authentication;
77
use GuzzleHttp\Psr7\Request;
88
use Http\Promise\FulfilledPromise;
@@ -41,9 +41,9 @@ public function testAuthenticationMethods($tokenOrLogin, $password, $method, $ex
4141
public function getAuthenticationData()
4242
{
4343
return [
44-
['access_token', null, Client::AUTH_ACCESS_TOKEN, 'token access_token'],
45-
['client_id', 'client_secret', Client::AUTH_CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
46-
['jwt_token', null, Client::AUTH_JWT, 'Bearer jwt_token'],
44+
['access_token', null, AuthMethod::AUTH_ACCESS_TOKEN, 'token access_token'],
45+
['client_id', 'client_secret', AuthMethod::AUTH_CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
46+
['jwt_token', null, AuthMethod::AUTH_JWT, 'Bearer jwt_token'],
4747
];
4848
}
4949
}

0 commit comments

Comments
 (0)