Skip to content

Commit de2f278

Browse files
authored
feature KnpLabs#1036 refs KnpLabs#955: deprecate Client::AUTH_* constants and replace them with AuthMethod::AUTH_* const (ipalo)
This PR was squashed before being merged into the 3.4.x-dev branch. Discussion ---------- Contribution for KnpLabs#955 Commits ------- 11c2d9f refs KnpLabs#955: deprecate Client::AUTH_* constants and replace them with AuthMethod::AUTH_* const f4774d0 refs KnpLabs#955: revert the Client::AUTH_* deletion (BC) e7f1ab9 refs KnpLabs#955: fix CR issues c9cf54e refs KnpLabs#955: add upgrade to v4.0 notes ec6656c refs KnpLabs#955: set public all constants
1 parent 0349838 commit de2f278

File tree

10 files changed

+79
-35
lines changed

10 files changed

+79
-35
lines changed

UPGRADE-4.0.md

+6
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@
33
### ResultPager
44

55
* `\Github\ResultPagerInterface::postFetch` is deprecated, and the method will be removed from the ResultPager interface/class.
6+
7+
### Authentication methods
8+
9+
* `Github\Client::AUTH_CLIENT_ID` is deprecated, use `Github\AuthMethod::CLIENT_ID` instead.
10+
* `Github\Client::AUTH_ACCESS_TOKEN` is deprecated, use `Github\AuthMethod::ACCESS_TOKEN` instead.
11+
* `Github\Client::AUTH_JWT` is deprecated, use `Github\AuthMethod::JWT` instead.

doc/currentuser/repositories.md

+3-3
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::ACCESS_TOKEN);
2929
$client->currentUser()->repositories();
3030
```

doc/graphql.md

+3-3
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::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::ACCESS_TOKEN);
5555

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

doc/security.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,23 @@ $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::CLIENT_ID` - https://developer.github.com/v3/#oauth2-keysecret
21+
* `Github\AuthMethod::ACCESS_TOKEN` - https://developer.github.com/v3/#oauth2-token-sent-in-a-header
22+
* `Github\AuthMethod::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
25-
`Github\Client::JWT` methods you should provide the API token in `$usernameOrToken` variable (`$password` is omitted in
24+
The required value of `$password` depends on the chosen `$method`. For `Github\AuthMethod::CLIENT_ID`, `Github\AuthMethod::ACCESS_TOKEN` and
25+
`Github\AuthMethod::JWT` methods you should provide the API token in `$usernameOrToken` variable (`$password` is omitted in
2626
this particular case).
2727

28-
The `Github\Client::AUTH_JWT` authentication method sends the specified JSON Web Token in an Authorization header.
28+
The `Github\AuthMethod::JWT` authentication method sends the specified JSON Web Token in an Authorization header.
2929

3030
After executing the `$client->authenticate($usernameOrToken, $secret, $method);` method using correct credentials, all
3131
further requests are done as the given user.
3232

3333
### Authenticating as an Integration
3434

35-
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
35+
To authenticate as an integration you need to supply a JSON Web Token with `Github\AuthMethod::JWT` to request
36+
and installation access token which is then usable with `Github\AuthMethod::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

@@ -64,7 +64,7 @@ $jwt = $config->builder(ChainedFormatter::withUnixTimestampDates())
6464
->getToken($config->signer(), $config->signingKey())
6565
;
6666

67-
$github->authenticate($jwt->toString(), null, Github\Client::AUTH_JWT)
67+
$github->authenticate($jwt->toString(), null, Github\AuthMethod::JWT)
6868
```
6969

7070
The `$integrationId` you can find in the about section of your github app.

lib/Github/AuthMethod.php

+30
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+
public const CLIENT_ID = 'client_id_header';
13+
14+
/**
15+
* Authenticate using a GitHub access token.
16+
*
17+
* @var string
18+
*/
19+
public const 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+
public const JWT = 'jwt';
30+
}

lib/Github/Client.php

+10-4
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,19 @@ class Client
7373
* Authenticate using a client_id/client_secret combination.
7474
*
7575
* @var string
76+
*
77+
* @deprecated Use the AuthMethod const
7678
*/
77-
const AUTH_CLIENT_ID = 'client_id_header';
79+
const AUTH_CLIENT_ID = AuthMethod::CLIENT_ID;
7880

7981
/**
8082
* Authenticate using a GitHub access token.
8183
*
8284
* @var string
85+
*
86+
* @deprecated Use the AuthMethod const
8387
*/
84-
const AUTH_ACCESS_TOKEN = 'access_token_header';
88+
const AUTH_ACCESS_TOKEN = AuthMethod::ACCESS_TOKEN;
8589

8690
/**
8791
* Constant for authentication method.
@@ -90,8 +94,10 @@ class Client
9094
* to the API.
9195
*
9296
* @var string
97+
*
98+
* @deprecated Use the AuthMethod const
9399
*/
94-
const AUTH_JWT = 'jwt';
100+
const AUTH_JWT = AuthMethod::JWT;
95101

96102
/**
97103
* @var string
@@ -313,7 +319,7 @@ public function api($name): AbstractApi
313319
*/
314320
public function authenticate($tokenOrLogin, $password = null, $authMethod = null): void
315321
{
316-
if (null === $authMethod && (self::AUTH_JWT === $password || self::AUTH_ACCESS_TOKEN === $password)) {
322+
if (null === $authMethod && (AuthMethod::JWT === $password || AuthMethod::ACCESS_TOKEN === $password)) {
317323
$authMethod = $password;
318324
$password = null;
319325
}

lib/Github/HttpClient/Plugin/Authentication.php

+4-4
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::CLIENT_ID:
6262
return sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password));
63-
case Client::AUTH_ACCESS_TOKEN:
63+
case AuthMethod::ACCESS_TOKEN:
6464
return sprintf('token %s', $this->tokenOrLogin);
65-
case Client::AUTH_JWT:
65+
case AuthMethod::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

+6-5
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::ACCESS_TOKEN],
73+
['client_id', 'client_secret', AuthMethod::CLIENT_ID],
74+
['token', null, AuthMethod::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::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::ACCESS_TOKEN);
102103
}
103104

104105
/**

test/Github/Tests/Functional/CacheTest.php

+4-3
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::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::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::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

+4-4
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::ACCESS_TOKEN, 'token access_token'],
45+
['client_id', 'client_secret', AuthMethod::CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
46+
['jwt_token', null, AuthMethod::JWT, 'Bearer jwt_token'],
4747
];
4848
}
4949
}

0 commit comments

Comments
 (0)