Skip to content

Commit b1a044d

Browse files
committed
Provide access_token in header instead of URI
1 parent a626934 commit b1a044d

4 files changed

Lines changed: 60 additions & 16 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ vendor/
22
composer.phar
33
composer.lock
44
phpunit.xml
5+
build/

Tests/GithubObjectTest.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,14 @@ public function testFetchUrl($apiUrl, $path, $page, $limit, $expected)
7272
{
7373
$this->options->set('api.url', $apiUrl);
7474

75-
$this->assertThat(
76-
$this->object->fetchUrl($path, $page, $limit),
77-
$this->equalTo($expected)
75+
self::assertEquals(
76+
$expected,
77+
$this->object->fetchUrl($path, $page, $limit)
78+
);
79+
80+
self::assertEquals(
81+
array(),
82+
$this->object->authHeader()
7883
);
7984
}
8085

@@ -109,9 +114,14 @@ public function testFetchUrlToken()
109114

110115
$this->options->set('gh.token', 'MyTestToken');
111116

112-
$this->assertThat(
113-
$this->object->fetchUrl('/gists', 0, 0),
114-
$this->equalTo('https://api.github.com/gists?access_token=MyTestToken')
117+
self::assertEquals(
118+
'https://api.github.com/gists',
119+
$this->object->fetchUrl('/gists', 0, 0)
120+
);
121+
122+
self::assertEquals(
123+
array('Authorization: MyTestToken'),
124+
$this->object->authHeader()
115125
);
116126
}
117127
}

Tests/Stub/ObjectMock.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,21 @@ public function fetchUrl($path, $page = 0, $limit = 0)
3232
{
3333
return parent::fetchUrl($path, $page, $limit);
3434
}
35+
36+
/**
37+
* Returns the Authorization header, if required.
38+
*
39+
* If the options passed to the constructor contain a value for `gh.token`,
40+
* an array with a suitable Authorization header is returned, an empty array
41+
* otherwise.
42+
*
43+
* @return array Authorization header if set in options.
44+
*
45+
* @since 1.8.0
46+
*/
47+
public function authHeader()
48+
{
49+
return parent::authHeader();
50+
}
51+
3552
}

src/AbstractGithubObject.php

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,7 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
114114
// Get a new Uri object focusing the api url and given path.
115115
$uri = new Uri($this->options->get('api.url') . $path);
116116

117-
if ($this->options->get('gh.token', false))
118-
{
119-
// Use oAuth authentication - @todo set in request header ?
120-
$uri->setVar('access_token', $this->options->get('gh.token'));
121-
}
122-
else
117+
if (!$this->options->get('gh.token', false))
123118
{
124119
// Use basic authentication
125120
if ($this->options->get('api.username', false))
@@ -136,16 +131,37 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
136131
// If we have a defined page number add it to the JUri object.
137132
if ($page > 0)
138133
{
139-
$uri->setVar('page', (int) $page);
134+
$uri->setVar('page', (int)$page);
140135
}
141136

142137
// If we have a defined items per page add it to the JUri object.
143138
if ($limit > 0)
144139
{
145-
$uri->setVar('per_page', (int) $limit);
140+
$uri->setVar('per_page', (int)$limit);
146141
}
147142

148-
return (string) $uri;
143+
return (string)$uri;
144+
}
145+
146+
/**
147+
* Returns the Authorization header, if required.
148+
*
149+
* If the options passed to the constructor contain a value for `gh.token`,
150+
* an array with a suitable Authorization header is returned, an empty array
151+
* otherwise.
152+
*
153+
* @return array Authorization header if set in options.
154+
*
155+
* @since 1.8.0
156+
*/
157+
protected function authHeader()
158+
{
159+
if ($this->options->get('gh.token', false))
160+
{
161+
return array('Authorization: ' . $this->options->get('gh.token'));
162+
}
163+
164+
return array();
149165
}
150166

151167
/**
@@ -156,8 +172,8 @@ protected function fetchUrl($path, $page = 0, $limit = 0)
156172
*
157173
* @return mixed
158174
*
159-
* @since 1.0
160175
* @throws UnexpectedResponseException
176+
* @since 1.0
161177
*/
162178
protected function processResponse(Response $response, $expectedCode = 200)
163179
{

0 commit comments

Comments
 (0)