Skip to content

Commit

Permalink
Merge pull request #232 from shadowhand/provider-get-headers
Browse files Browse the repository at this point in the history
Enhance ability to use client to make authenticated requests
  • Loading branch information
ramsey committed Mar 10, 2015
2 parents 7faec80 + f156dd9 commit 76af605
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 38 deletions.
31 changes: 26 additions & 5 deletions src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ abstract class AbstractProvider implements ProviderInterface

public $responseType = 'json';

public $headers = null;
public $headers = [];

public $authorizationHeader;

/**
* @var GuzzleClient
Expand Down Expand Up @@ -323,17 +325,19 @@ protected function fetchUserDetails(AccessToken $token)
{
$url = $this->urlUserDetails($token);

return $this->fetchProviderData($url);
$headers = $this->getHeaders($token);

return $this->fetchProviderData($url, $headers);
}

protected function fetchProviderData($url)
protected function fetchProviderData($url, array $headers = [])
{
try {
$client = $this->getHttpClient();
$client->setBaseUrl($url);

if ($this->headers) {
$client->setDefaultOption('headers', $this->headers);
if ($headers) {
$client->setDefaultOption('headers', $headers);
}

$request = $client->get()->send();
Expand All @@ -348,6 +352,23 @@ protected function fetchProviderData($url)
return $response;
}

protected function getAuthorizationHeaders($token)
{
$headers = [];
if ($this->authorizationHeader) {
$headers['Authorization'] = $this->authorizationHeader . ' ' . $token;
}
return $headers;
}

public function getHeaders($token = null)
{
$headers = $this->headers;
if ($token) {
$headers = array_merge($headers, $this->getAuthorizationHeaders($token));
}
return $headers;
}

public function setRedirectHandler(Closure $handler)
{
Expand Down
10 changes: 2 additions & 8 deletions src/Provider/Eventbrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,7 @@

class Eventbrite extends AbstractProvider
{
public function __construct($options)
{
parent::__construct($options);
$this->headers = [
'Authorization' => 'Bearer',
];
}
public $authorizationHeader = 'Bearer';

public function urlAuthorize()
{
Expand All @@ -26,7 +20,7 @@ public function urlAccessToken()

public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
{
return 'https://www.eventbrite.com/json/user_get?access_token='.$token;
return 'https://www.eventbrite.com/json/user_get';
}

public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
Expand Down
33 changes: 19 additions & 14 deletions src/Provider/Github.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
namespace League\OAuth2\Client\Provider;

use League\OAuth2\Client\Entity\User;
use League\OAuth2\Client\Token\AccessToken;

class Github extends AbstractProvider
{
public $responseType = 'string';

public $authorizationHeader = 'token';

public $domain = 'https://github.com';

public $apiDomain = 'https://api.github.com';
Expand All @@ -22,23 +25,23 @@ public function urlAccessToken()
return $this->domain.'/login/oauth/access_token';
}

public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
public function urlUserDetails(AccessToken $token)
{
if ($this->domain === 'https://github.com') {
return $this->apiDomain.'/user?access_token='.$token;
return $this->apiDomain.'/user';
}
return $this->domain.'/api/v3/user?access_token='.$token;
return $this->domain.'/api/v3/user';
}

public function urlUserEmails(\League\OAuth2\Client\Token\AccessToken $token)
public function urlUserEmails(AccessToken $token)
{
if ($this->domain === 'https://github.com') {
return $this->apiDomain.'/user/emails?access_token='.$token;
return $this->apiDomain.'/user/emails';
}
return $this->domain.'/api/v3/user/emails?access_token='.$token;
return $this->domain.'/api/v3/user/emails';
}

public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
public function userDetails($response, AccessToken $token)
{
$user = new User();

Expand All @@ -58,37 +61,39 @@ public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $
return $user;
}

public function userUid($response, \League\OAuth2\Client\Token\AccessToken $token)
public function userUid($response, AccessToken $token)
{
return $response->id;
}

public function getUserEmails(\League\OAuth2\Client\Token\AccessToken $token)
public function getUserEmails(AccessToken $token)
{
$response = $this->fetchUserEmails($token);

return $this->userEmails(json_decode($response), $token);
}

public function userEmail($response, \League\OAuth2\Client\Token\AccessToken $token)
public function userEmail($response, AccessToken $token)
{
return isset($response->email) && $response->email ? $response->email : null;
}

public function userEmails($response, \League\OAuth2\Client\Token\AccessToken $token)
public function userEmails($response, AccessToken $token)
{
return $response;
}

public function userScreenName($response, \League\OAuth2\Client\Token\AccessToken $token)
public function userScreenName($response, AccessToken $token)
{
return $response->name;
}

protected function fetchUserEmails(\League\OAuth2\Client\Token\AccessToken $token)
protected function fetchUserEmails(AccessToken $token)
{
$url = $this->urlUserEmails($token);

return $this->fetchProviderData($url);
$headers = $this->getHeaders($token);

return $this->fetchProviderData($url, $headers);
}
}
4 changes: 3 additions & 1 deletion src/Provider/Google.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Google extends AbstractProvider
'email',
];

public $authorizationHeader = 'OAuth';

/**
* @var string If set, this will be sent to google as the "hd" parameter.
* @link https://developers.google.com/accounts/docs/OAuth2Login#hd-param
Expand Down Expand Up @@ -60,7 +62,7 @@ public function urlUserDetails(\League\OAuth2\Client\Token\AccessToken $token)
return
'https://www.googleapis.com/plus/v1/people/me?'.
'fields=id%2Cname(familyName%2CgivenName)%2CdisplayName%2C'.
'emails%2Fvalue%2Cimage%2Furl&alt=json&access_token='.$token;
'emails%2Fvalue%2Cimage%2Furl&alt=json';
}

public function userDetails($response, \League\OAuth2\Client\Token\AccessToken $token)
Expand Down
5 changes: 3 additions & 2 deletions src/Provider/LinkedIn.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class LinkedIn extends AbstractProvider
{
public $scopes = ['r_basicprofile r_emailaddress r_contactinfo'];
public $responseType = 'json';
public $authorizationHeader = 'Bearer';
public $fields = [
'id', 'email-address', 'first-name', 'last-name', 'headline',
'location', 'industry', 'picture-url', 'public-profile-url',
Expand All @@ -26,8 +27,8 @@ public function urlAccessToken()

public function urlUserDetails(AccessToken $token)
{
return 'https://api.linkedin.com/v1/people/~:('.implode(",", $this->fields)
.')?format=json&oauth2_access_token='.$token;
$fields = implode(',', $this->fields);
return 'https://api.linkedin.com/v1/people/~:(' . $fields . ')?format=json';
}

public function userDetails($response, AccessToken $token)
Expand Down
2 changes: 2 additions & 0 deletions src/Provider/ProviderInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function authorize($options = []);

public function getAccessToken($grant = 'authorization_code', $params = []);

public function getHeaders($token = null);

public function getUserDetails(AccessToken $token);

public function getUserUid(AccessToken $token);
Expand Down
27 changes: 27 additions & 0 deletions test/src/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ public function testConstructorSetsProperties()
'scopeSeparator' => ';',
'responseType' => 'csv',
'headers' => ['Foo' => 'Bar'],
'authorizationHeader' => 'Bearer',
];

$mockProvider = new MockProvider($options);
Expand Down Expand Up @@ -142,6 +143,32 @@ public function userPropertyProvider()
[$response3],
];
}

public function getHeadersTest()
{
$provider = $this->getMockForAbstractClass(
'\League\OAuth2\Client\Provider\AbstractProvider',
[
[
'clientId' => 'mock_client_id',
'clientSecret' => 'mock_secret',
'redirectUri' => 'none',
]
]
);

/**
* @var $provider AbstractProvider
*/
$this->assertEquals([], $provider->getHeaders());
$this->assertEquals([], $provider->getHeaders('mock_token'));

$provider->authorizationHeader = 'Bearer';
$this->assertEquals(['Authorization' => 'Bearer abc'], $provider->getHeaders('abc'));

$token = new AccessToken(['access_token' => 'xyz', 'expires_in' => 3600]);
$this->assertEquals(['Authorization' => 'Bearer xyz'], $provider->getHeaders($token));
}
}

class MockProvider extends \League\OAuth2\Client\Provider\AbstractProvider
Expand Down
10 changes: 6 additions & 4 deletions test/src/Provider/GithubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public function testUserData()

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(5);
$client->shouldReceive('setDefaultOption')->times(4);
$client->shouldReceive('post->send')->times(1)->andReturn($postResponse);
$client->shouldReceive('get->send')->times(4)->andReturn($getResponse);
$this->provider->setHttpClient($client);
Expand All @@ -128,8 +129,8 @@ public function testGithubDomainUrls()

$this->assertEquals($this->provider->domain.'/login/oauth/authorize', $this->provider->urlAuthorize());
$this->assertEquals($this->provider->domain.'/login/oauth/access_token', $this->provider->urlAccessToken());
$this->assertEquals($this->provider->apiDomain.'/user?access_token=mock_access_token', $this->provider->urlUserDetails($token));
$this->assertEquals($this->provider->apiDomain.'/user/emails?access_token=mock_access_token', $this->provider->urlUserEmails($token));
$this->assertEquals($this->provider->apiDomain.'/user', $this->provider->urlUserDetails($token));
$this->assertEquals($this->provider->apiDomain.'/user/emails', $this->provider->urlUserEmails($token));
}

public function testGithubEnterpriseDomainUrls()
Expand All @@ -147,8 +148,8 @@ public function testGithubEnterpriseDomainUrls()

$this->assertEquals($this->provider->domain.'/login/oauth/authorize', $this->provider->urlAuthorize());
$this->assertEquals($this->provider->domain.'/login/oauth/access_token', $this->provider->urlAccessToken());
$this->assertEquals($this->provider->domain.'/api/v3/user?access_token=mock_access_token', $this->provider->urlUserDetails($token));
$this->assertEquals($this->provider->domain.'/api/v3/user/emails?access_token=mock_access_token', $this->provider->urlUserEmails($token));
$this->assertEquals($this->provider->domain.'/api/v3/user', $this->provider->urlUserDetails($token));
$this->assertEquals($this->provider->domain.'/api/v3/user/emails', $this->provider->urlUserEmails($token));
}

public function testUserEmails()
Expand All @@ -161,6 +162,7 @@ public function testUserEmails()

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(2);
$client->shouldReceive('setDefaultOption')->times(1);
$client->shouldReceive('post->send')->times(1)->andReturn($postResponse);
$client->shouldReceive('get->send')->times(1)->andReturn($getResponse);
$this->provider->setHttpClient($client);
Expand Down
3 changes: 1 addition & 2 deletions test/src/Provider/GoogleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ public function testGetAccessToken()

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);

# print_r($token);die();

$this->assertEquals('mock_access_token', $token->accessToken);
$this->assertLessThanOrEqual(time() + 3600, $token->expires);
$this->assertGreaterThanOrEqual(time(), $token->expires);
Expand All @@ -86,6 +84,7 @@ public function testUserData()

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(5);
$client->shouldReceive('setDefaultOption')->times(4);
$client->shouldReceive('post->send')->times(1)->andReturn($postResponse);
$client->shouldReceive('get->send')->times(4)->andReturn($getResponse);
$this->provider->setHttpClient($client);
Expand Down
3 changes: 1 addition & 2 deletions test/src/Provider/LinkedInTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ public function testGetAccessToken()

$token = $this->provider->getAccessToken('authorization_code', ['code' => 'mock_authorization_code']);

# print_r($token);die();

$this->assertEquals('mock_access_token', $token->accessToken);
$this->assertLessThanOrEqual(time() + 3600, $token->expires);
$this->assertGreaterThanOrEqual(time(), $token->expires);
Expand All @@ -82,6 +80,7 @@ public function testUserData()

$client = m::mock('Guzzle\Service\Client');
$client->shouldReceive('setBaseUrl')->times(5);
$client->shouldReceive('setDefaultOption')->times(4);
$client->shouldReceive('post->send')->times(1)->andReturn($postResponse);
$client->shouldReceive('get->send')->times(4)->andReturn($getResponse);
$this->provider->setHttpClient($client);
Expand Down

0 comments on commit 76af605

Please sign in to comment.