Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 21 additions & 5 deletions Tests/Package/AuthorizationsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,24 +583,40 @@ public function testGetAuthorizationLink()
);
}

/**
* Tests that the getAuthorizationLink method encodes the query values
*
* @return void
*/
public function testGetAuthorizationLinkEncodesValues()
{
$this->assertSame(
'https://github.com/login/oauth/authorize?client_id=12345'
. '&redirect_uri=https%3A%2F%2Fexample.com%2Fcallback%3Fa%3D1&scope=repo%20user&state=st%26scope%3Dadmin',
$this->object->getAuthorizationLink('12345', 'https://example.com/callback?a=1', 'repo user', 'st&scope=admin')
);
}

/**
* Tests the requestToken method
*
* @return void
*/
public function testRequestToken()
{
$this->response = $this->getResponseObject('');
$this->response = $this->getResponseObject('{"access_token":"gho_abc","scope":"repo","token_type":"bearer"}');

$this->client->expects($this->once())
->method('post')
->with('https://github.com/login/oauth/access_token')
->with(
'https://github.com/login/oauth/access_token',
['client_id' => '12345', 'client_secret' => 'aaa', 'code' => 'bbb', 'redirect_uri' => 'ccc'],
['Accept' => 'application/json']
)
->willReturn($this->response);

$response = (string) $this->response->getBody();

$this->assertEquals(
$response,
(object) ['access_token' => 'gho_abc', 'scope' => 'repo', 'token_type' => 'bearer'],
$this->object->requestToken('12345', 'aaa', 'bbb', 'ccc')
);
}
Expand Down
7 changes: 7 additions & 0 deletions docs/overview.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# Github API package

TODO

## OAuth web application flow

`$github->authorization->getAuthorizationLink($clientId, $redirectUri, $scope, $state)` returns the URL to send the
user to; all values are percent-encoded. `$github->authorization->requestToken($clientId, $clientSecret, $code)`
exchanges the code for a token and returns the decoded answer (`access_token`, `scope`, `token_type`). Without a
`$format`, JSON is requested; before, GitHub answered form-encoded and the method returned `null`.
21 changes: 11 additions & 10 deletions src/Package/Authorization.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

use Joomla\Github\AbstractPackage;
use Joomla\Http\Exception\UnexpectedResponseException;
use Joomla\Uri\Uri;

/**
* GitHub API Authorization class for the Joomla Framework.
Expand Down Expand Up @@ -271,23 +270,22 @@ public function getRateLimit()
*/
public function getAuthorizationLink($clientId, $redirectUri = '', $scope = '', $state = '')
{
$uri = new Uri('https://github.com/login/oauth/authorize');

$uri->setVar('client_id', $clientId);
$query = ['client_id' => $clientId];

if ($redirectUri) {
$uri->setVar('redirect_uri', urlencode($redirectUri));
$query['redirect_uri'] = $redirectUri;
}

if ($scope) {
$uri->setVar('scope', $scope);
$query['scope'] = $scope;
}

if ($state) {
$uri->setVar('state', $state);
$query['state'] = $state;
}

return (string) $uri;
// Build the query here: Joomla\Uri\Uri does not encode the values it renders.
return 'https://github.com/login/oauth/authorize?' . http_build_query($query, '', '&', \PHP_QUERY_RFC3986);
}

/**
Expand All @@ -297,9 +295,9 @@ public function getAuthorizationLink($clientId, $redirectUri = '', $scope = '',
* @param string $clientSecret The client secret you received from GitHub when you registered.
* @param string $code The code you received as a response to Step 1.
* @param string $redirectUri URL in your app where users will be sent after authorization.
* @param string $format The response format (json, xml, ).
* @param string $format The response format (json, xml). Without a format, JSON is requested.
*
* @return string
* @return object The decoded response with the access_token, scope and token_type properties.
*
* @since 1.0
* @throws \UnexpectedValueException
Expand Down Expand Up @@ -336,6 +334,9 @@ public function requestToken($clientId, $clientSecret, $code, $redirectUri = '',
throw new \UnexpectedValueException('Invalid format');
}

// Without an Accept header GitHub answers form-encoded, which cannot be decoded.
$headers['Accept'] = 'application/json';

break;
}

Expand Down
Loading