Skip to content

Commit 069c34e

Browse files
committed
Introduce: "Unauthenticated rate limited requests"
1 parent 91a7f96 commit 069c34e

File tree

2 files changed

+31
-5
lines changed

2 files changed

+31
-5
lines changed

lib/Github/Client.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ class Client
2222
*/
2323
const AUTH_URL_TOKEN = 'url_token';
2424

25+
/**
26+
* Constant for authentication method. Not indicates the new login, but allows
27+
* usage of unauthenticated rate limited requests for given client_id + client_secret
28+
*/
29+
const AUTH_URL_CLIENT_ID = 'url_client_id';
30+
2531
/**
2632
* Constant for authentication method. Indicates the new favored login method
2733
* with username and password via HTTP Authentication.
@@ -76,7 +82,7 @@ public function authenticate($login, $secret = null, $method = null)
7682
{
7783
$this->getHttpClient()->setOption('auth_method', $method);
7884

79-
if ($method === self::AUTH_HTTP_PASSWORD) {
85+
if ($method === self::AUTH_HTTP_PASSWORD || $method === self::AUTH_URL_CLIENT_ID) {
8086
$this
8187
->getHttpClient()
8288
->setOption('login', $login)

lib/Github/HttpClient/Listener/AuthListener.php

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,41 @@ public function preSend(RequestInterface $request)
6161
$request->addHeader('Authorization: token '. $this->options['token']);
6262
break;
6363

64+
case Client::AUTH_URL_CLIENT_ID:
65+
if (!isset($this->options['login'], $this->options['password'])) {
66+
throw new InvalidArgumentException('You need to set client_id and client_secret!');
67+
}
68+
69+
if ('GET' === $request->getMethod()) {
70+
$url = $request->getUrl();
71+
72+
$parameters = array(
73+
'client_id' => $this->options['login'],
74+
'client_secret' => $this->options['password'],
75+
);
76+
77+
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($parameters, '', '&'));
78+
79+
$request->fromUrl(new Url($url));
80+
}
81+
break;
82+
6483
case Client::AUTH_URL_TOKEN:
6584
if (!isset($this->options['token'])) {
6685
throw new InvalidArgumentException('You need to set OAuth token!');
6786
}
68-
$url = $request->getUrl();
6987

7088
if ('GET' === $request->getMethod()) {
89+
$url = $request->getUrl();
90+
7191
$parameters = array(
7292
'access_token' => $this->options['token']
7393
);
7494

75-
$url .= '?'.utf8_encode(http_build_query($parameters, '', '&'));
76-
}
95+
$url .= (false === strpos($url, '?') ? '?' : '&').utf8_encode(http_build_query($parameters, '', '&'));
7796

78-
$request->fromUrl(new Url($url));
97+
$request->fromUrl(new Url($url));
98+
}
7999
break;
80100
}
81101
}

0 commit comments

Comments
 (0)