Skip to content

Commit f7b14da

Browse files
committed
Prepare deprecation of authentication methods
1 parent 953c9b4 commit f7b14da

File tree

5 files changed

+90
-8
lines changed

5 files changed

+90
-8
lines changed

doc/security.md

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,16 @@ $client->authenticate($usernameOrToken, $password, $method);
1515
`$usernameOrToken` is, of course, the username (or in some cases token/client ID, more details you can find below),
1616
and guess what should contain `$password`. The `$method` can contain one of the five allowed values:
1717

18-
* `Github\Client::AUTH_URL_TOKEN`
19-
* `Github\Client::AUTH_URL_CLIENT_ID`
20-
* `Github\Client::AUTH_HTTP_TOKEN`
21-
* `Github\Client::AUTH_HTTP_PASSWORD`
22-
* `Github\Client::AUTH_JWT`
18+
#### Deprecated methods
19+
* `Github\Client::AUTH_URL_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
20+
* `Github\Client::AUTH_URL_CLIENT_ID` use `Github\Client::AUTH_CLIENT_ID` instead.
21+
* `Github\Client::AUTH_HTTP_TOKEN` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
22+
* `Github\Client::AUTH_HTTP_PASSWORD` use `Github\Client::AUTH_ACCESS_TOKEN` instead.
23+
24+
#### Supported methods
25+
* `Github\Client::AUTH_CLIENT_ID` - https://developer.github.com/v3/#oauth2-keysecret
26+
* `Github\Client::AUTH_ACCESS_TOKEN` - https://developer.github.com/v3/#oauth2-token-sent-in-a-header
27+
* `Github\Client::AUTH_JWT` - https://developer.github.com/apps/building-github-apps/authenticating-with-github-apps/#authenticating-as-a-github-app
2328

2429
The required value of `$password` depends on the chosen `$method`. For `Github\Client::AUTH_URL_TOKEN`,
2530
`Github\Client::AUTH_HTTP_TOKEN` and `Github\Client::JWT` methods you should provide the API token in

lib/Github/Client.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,48 @@ class Client
7272
/**
7373
* Constant for authentication method. Indicates the default, but deprecated
7474
* login with username and token in URL.
75+
*
76+
* @deprecated Use `Client::AUTH_ACCESS_TOKEN` instead. See https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters
7577
*/
7678
const AUTH_URL_TOKEN = 'url_token';
7779

7880
/**
7981
* Constant for authentication method. Not indicates the new login, but allows
8082
* usage of unauthenticated rate limited requests for given client_id + client_secret.
83+
*
84+
* @deprecated Use `Client::AUTH_CLIENT_ID` instead. See https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters
8185
*/
8286
const AUTH_URL_CLIENT_ID = 'url_client_id';
8387

8488
/**
8589
* Constant for authentication method. Indicates the new favored login method
8690
* with username and password via HTTP Authentication.
91+
*
92+
* @deprecated Use `Client::AUTH_ACCESS_TOKEN` instead. See https://developer.github.com/changes/2019-11-05-deprecated-passwords-and-authorizations-api/#authenticating-using-query-parameters
8793
*/
8894
const AUTH_HTTP_PASSWORD = 'http_password';
8995

9096
/**
9197
* Constant for authentication method. Indicates the new login method with
9298
* with username and token via HTTP Authentication.
99+
*
100+
* @deprecated Use `Client::AUTH_ACCESS_TOKEN` instead.
93101
*/
94102
const AUTH_HTTP_TOKEN = 'http_token';
95103

104+
/**
105+
* Authenticate using a client_id/client_secret combination.
106+
*/
107+
const AUTH_CLIENT_ID = 'client_id_header';
108+
109+
/**
110+
* Authenticate using a GitHub access token.
111+
*/
112+
const AUTH_ACCESS_TOKEN = 'access_token_header';
113+
96114
/**
97115
* Constant for authentication method. Indicates JSON Web Token
98-
* authentication required for integration access to the API.
116+
* authentication required for GitHub apps access to the API.
99117
*/
100118
const AUTH_JWT = 'jwt';
101119

lib/Github/HttpClient/Plugin/Authentication.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,23 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla
3434
{
3535
switch ($this->method) {
3636
case Client::AUTH_HTTP_PASSWORD:
37+
@trigger_error('Using the "Client::AUTH_HTTP_PASSWORD" authentication method is deprecated in knp-labs/php-github-api v2.15 and will be removed in knp-labs/php-github-api v3.0. Use "Client::AUTH_ACCESS_TOKEN" instead.', E_USER_DEPRECATED);
38+
case Client::AUTH_CLIENT_ID:
3739
$request = $request->withHeader(
3840
'Authorization',
3941
sprintf('Basic %s', base64_encode($this->tokenOrLogin.':'.$this->password))
4042
);
4143
break;
4244

4345
case Client::AUTH_HTTP_TOKEN:
46+
@trigger_error('Using the "Client::AUTH_HTTP_TOKEN" authentication method is deprecated in knp-labs/php-github-api v2.15 and will be removed in knp-labs/php-github-api v3.0. Use "Client::AUTH_ACCESS_TOKEN" instead.', E_USER_DEPRECATED);
47+
case Client::AUTH_ACCESS_TOKEN:
4448
$request = $request->withHeader('Authorization', sprintf('token %s', $this->tokenOrLogin));
4549
break;
4650

4751
case Client::AUTH_URL_CLIENT_ID:
52+
@trigger_error('Using the "Client::AUTH_URL_CLIENT_ID" authentication method is deprecated in knp-labs/php-github-api v2.15 and will be removed in knp-labs/php-github-api v3.0. Use "Client::AUTH_CLIENT_ID" instead.', E_USER_DEPRECATED);
53+
4854
$uri = $request->getUri();
4955
$query = $uri->getQuery();
5056

@@ -61,6 +67,8 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla
6167
break;
6268

6369
case Client::AUTH_URL_TOKEN:
70+
@trigger_error('Using the "Client::AUTH_URL_TOKEN" authentication method is deprecated in knp-labs/php-github-api v2.15 and will be removed in knp-labs/php-github-api v3.0. Use "Client::AUTH_ACCESS_TOKEN" instead.', E_USER_DEPRECATED);
71+
6472
$uri = $request->getUri();
6573
$query = $uri->getQuery();
6674

@@ -72,11 +80,9 @@ public function doHandleRequest(RequestInterface $request, callable $next, calla
7280
$uri = $uri->withQuery($query);
7381
$request = $request->withUri($uri);
7482
break;
75-
7683
case Client::AUTH_JWT:
7784
$request = $request->withHeader('Authorization', sprintf('Bearer %s', $this->tokenOrLogin));
7885
break;
79-
8086
default:
8187
throw new RuntimeException(sprintf('%s not yet implemented', $this->method));
8288
break;

test/Github/Tests/ClientTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,10 @@ public function getAuthenticationFullData()
7171
['login', 'password', Client::AUTH_HTTP_PASSWORD],
7272
['token', null, Client::AUTH_HTTP_TOKEN],
7373
['token', null, Client::AUTH_URL_TOKEN],
74+
['token', null, Client::AUTH_ACCESS_TOKEN],
7475
['client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID],
76+
['client_id', 'client_secret', Client::AUTH_CLIENT_ID],
77+
['token', null, Client::AUTH_JWT],
7578
];
7679
}
7780

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
namespace Github\Tests\HttpClient\Plugin;
4+
5+
use Github\Client;
6+
use Github\HttpClient\Plugin\Authentication;
7+
use GuzzleHttp\Psr7\Request;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class AuthenticationTest extends TestCase
11+
{
12+
/**
13+
* @dataProvider getAuthenticationData
14+
*/
15+
public function testAuthenticationMethods($tokenOrLogin, $password, $method, $expectedHeader = null, $expectedUrl = null)
16+
{
17+
$request = new Request('GET', '/');
18+
$plugin = new Authentication($tokenOrLogin, $password, $method);
19+
20+
/** @var Request $newRequest */
21+
$newRequest = null;
22+
$plugin->doHandleRequest($request, static function ($request) use (&$newRequest) {
23+
/** @var Request $newRequest */
24+
$newRequest = $request;
25+
}, static function () {
26+
throw new \RuntimeException('Did not expect plugin to call first');
27+
});
28+
29+
$this->assertNotNull($newRequest);
30+
31+
if ($expectedHeader) {
32+
$this->assertContains($expectedHeader, $newRequest->getHeader('Authorization'));
33+
} else {
34+
$this->assertEquals($expectedUrl, $newRequest->getUri()->__toString());
35+
}
36+
}
37+
38+
public function getAuthenticationData()
39+
{
40+
return [
41+
['login', 'password', Client::AUTH_HTTP_PASSWORD, sprintf('Basic %s', base64_encode('login'.':'.'password'))],
42+
['access_token', null, Client::AUTH_HTTP_TOKEN, 'token access_token'],
43+
['token', null, Client::AUTH_URL_TOKEN, null, '/?access_token=token'],
44+
['access_token', null, Client::AUTH_ACCESS_TOKEN, 'token access_token'],
45+
['client_id', 'client_secret', Client::AUTH_URL_CLIENT_ID, null, '/?client_id=client_id&client_secret=client_secret'],
46+
['client_id', 'client_secret', Client::AUTH_CLIENT_ID, sprintf('Basic %s', base64_encode('client_id'.':'.'client_secret'))],
47+
['jwt_token', null, Client::AUTH_JWT, 'Bearer jwt_token'],
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)