Skip to content

Commit

Permalink
Merge pull request hwi#577 from stloyd/bugfix/cs
Browse files Browse the repository at this point in the history
Cleanup CS, add missing test, cleanup some resource owners
  • Loading branch information
stloyd committed Jul 10, 2014
2 parents c1cf178 + 2b17628 commit f889769
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 162 deletions.
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ class Configuration implements ConfigurationInterface
'mailru',
'odnoklassniki',
'qq',
'reddit',
'salesforce',
'sensio_connect',
'sina_weibo',
Expand All @@ -59,7 +60,6 @@ class Configuration implements ConfigurationInterface
'wordpress',
'yandex',
'37signals',
'reddit'
),
'oauth1' => array(
'bitbucket',
Expand Down
19 changes: 0 additions & 19 deletions OAuth/ResourceOwner/EventbriteResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,6 @@ class EventbriteResourceOwner extends GenericOAuth2ResourceOwner
'email' => 'email',
);

/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$url = $this->normalizeUrl($this->getOption('infos_url'), array(
'access_token' => $accessToken['access_token']
));

$content = $this->httpRequest($url, null, array('Authorization: Bearer '.$accessToken['access_token']))->getContent();

$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));

return $response;
}

/**
* {@inheritDoc}
*/
Expand Down
8 changes: 2 additions & 6 deletions OAuth/ResourceOwner/GenericOAuth2ResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,9 @@ class GenericOAuth2ResourceOwner extends AbstractResourceOwner
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
if ($this->options['use_bearer_authorization']) {
$url = $this->normalizeUrl($this->options['infos_url']);

$content = $this->httpRequest($url, null, array('Authorization: Bearer '.$accessToken['access_token']));
$content = $this->httpRequest($this->normalizeUrl($this->options['infos_url']), null, array('Authorization: Bearer '.$accessToken['access_token']));
} else {
$url = $this->normalizeUrl($this->options['infos_url'], array('access_token' => $accessToken['access_token']));

$content = $this->doGetUserInformationRequest($url);
$content = $this->doGetUserInformationRequest($this->normalizeUrl($this->options['infos_url'], array('access_token' => $accessToken['access_token'])));
}

$response = $this->getUserResponse();
Expand Down
15 changes: 0 additions & 15 deletions OAuth/ResourceOwner/HubicResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,6 @@ class HubicResourceOwner extends GenericOAuth2ResourceOwner
'email' => 'email',
);

/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$content = $this->httpRequest($this->normalizeUrl($this->options['infos_url']), null, array('Authorization: Bearer '.$accessToken['access_token']))->getContent();

$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));

return $response;
}

/**
* {@inheritDoc}
*/
Expand Down
95 changes: 10 additions & 85 deletions OAuth/ResourceOwner/RedditResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@

namespace HWI\Bundle\OAuthBundle\OAuth\ResourceOwner;

use Buzz\Message\Request as HttpRequest;
use Buzz\Message\RequestInterface as HttpRequestInterface;
use Buzz\Message\Response as HttpResponse;
use HWI\Bundle\OAuthBundle\Security\Core\Authentication\Token\OAuthToken;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

/**
Expand All @@ -38,55 +33,9 @@ class RedditResourceOwner extends GenericOAuth2ResourceOwner
/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
protected function doGetTokenRequest($url, array $parameters = array())
{
$request = new HttpRequest(HttpRequestInterface::METHOD_GET, $this->options['infos_url']);
$response = new HttpResponse();

$headers = array(
'User-Agent: HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle)',
'Authorization: Bearer ' . $accessToken['access_token'],
);

$request->setHeaders($headers);
$this->httpClient->send($request, $response);
$content = $this->getResponseContent($response);

$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));

return $response;
}

/**
* {@inheritDoc}
*/
public function getAccessToken(Request $request, $redirectUri, array $extraParameters = array())
{
$parameters = array(
'grant_type' => 'authorization_code',
'code' => $request->query->get('code'),
'redirect_uri' => $redirectUri,
'client_id' => $this->options['client_id']
);

return $this->getAccessResponse($this->options['access_token_url'], $parameters);
}

/**
* {@inheritDoc}
*/
public function refreshAccessToken($refreshToken, array $extraParameters = array())
{
$parameters = array(
'refresh_token' => $refreshToken,
'grant_type' => 'refresh_token',
'client_id' => $this->options['client_id'],
);

return $this->getAccessResponse($this->options['access_token_url'], $parameters);
return $this->httpRequest($url, null, array('Authorization: Basic ' . base64_encode(sprintf('%s:%s', $this->options['client_id'], $this->options['client_secret']))));
}

/**
Expand All @@ -97,39 +46,15 @@ protected function configureOptions(OptionsResolverInterface $resolver)
parent::configureOptions($resolver);

$resolver->setDefaults(array(
'authorization_url' => 'https://ssl.reddit.com/api/v1/authorize',
'access_token_url' => 'https://ssl.reddit.com/api/v1/access_token',
'infos_url' => 'https://oauth.reddit.com/api/v1/me.json',
'duration' => 'permanent',
'use_commas_in_scope' => true,
'csrf' => true,
'scope' => 'identity',
));
}

/**
* @param $url
* @param array $parameters
* @return array|HttpResponse
* @throws \Symfony\Component\Security\Core\Exception\AuthenticationException
*/
private function getAccessResponse($url, array $parameters)
{
$request = new HttpRequest(HttpRequestInterface::METHOD_POST, $url);
$response = new HttpResponse();

$headers = array(
'User-Agent: HWIOAuthBundle (https://github.com/hwi/HWIOAuthBundle)',
'Authorization: Basic ' . base64_encode(sprintf('%s:%s', $this->options['client_id'], $this->options['client_secret']))
);
'authorization_url' => 'https://ssl.reddit.com/api/v1/authorize',
'access_token_url' => 'https://ssl.reddit.com/api/v1/access_token',
'infos_url' => 'https://oauth.reddit.com/api/v1/me.json',

$request->setHeaders($headers);
$request->setContent($parameters);

$this->httpClient->send($request, $response);
$response = $this->getResponseContent($response);
$this->validateResponseContent($response);
'use_commas_in_scope' => true,
'csrf' => true,
'scope' => 'identity',

return $response;
'duration' => 'permanent',
));
}
}
12 changes: 6 additions & 6 deletions OAuth/ResourceOwner/SalesforceResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class SalesforceResourceOwner extends GenericOAuth2ResourceOwner
* {@inheritDoc}
*/
protected $paths = array(
'identifier' => 'user_id',
'nickname' => 'nick_name',
'realname' => 'nick_name',
'email' => 'email',
'identifier' => 'user_id',
'nickname' => 'nick_name',
'realname' => 'nick_name',
'email' => 'email',
'profilepicture' => 'photos.picture',
);

Expand Down Expand Up @@ -71,10 +71,10 @@ protected function configureOptions(OptionsResolverInterface $resolver)
'access_token_url' => 'https://login.salesforce.com/services/oauth2/token',

// @see SalesforceResourceOwner::getUserInformation()
'infos_url' => null,
'infos_url' => null,

// @see SalesforceResourceOwner::doGetUserInformationRequest()
'format' => 'json',
'format' => 'json',
));
}

Expand Down
24 changes: 3 additions & 21 deletions OAuth/ResourceOwner/SoundcloudResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,11 @@ protected function configureOptions(OptionsResolverInterface $resolver)
parent::configureOptions($resolver);

$resolver->setDefaults(array(
'access_token_url' => 'https://api.soundcloud.com/oauth2/token',
'access_token_url' => 'https://api.soundcloud.com/oauth2/token',
'authorization_url' => 'https://soundcloud.com/connect',
'infos_url' => 'https://api.soundcloud.com/me.json',
'scope' => 'non-expiring'
));
}

/**
* {@inheritDoc}
*/
public function getUserInformation(array $accessToken, array $extraParameters = array())
{
$url = $this->normalizeUrl($this->getOption('infos_url'), array(
'oauth_token' => $accessToken['access_token']
'scope' => 'non-expiring'
));

$content = $this->httpRequest($url, null, array('Authorization: Bearer '.$accessToken['access_token']))->getContent();

$response = $this->getUserResponse();
$response->setResponse($content);
$response->setResourceOwner($this);
$response->setOAuthToken(new OAuthToken($accessToken));

return $response;
}
}
}
4 changes: 1 addition & 3 deletions OAuth/ResourceOwner/YandexResourceOwner.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,7 @@ class YandexResourceOwner extends GenericOAuth2ResourceOwner
protected function doGetUserInformationRequest($url, array $parameters = array())
{
// Yandex require to pass the OAuth token as 'oauth_token' instead of 'access_token'
$url = str_replace('access_token', 'oauth_token', $url);

return $this->httpRequest($url);
return $this->httpRequest(str_replace('access_token', 'oauth_token', $url));
}

/**
Expand Down
4 changes: 2 additions & 2 deletions Resources/config/oauth.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
<parameter key="hwi_oauth.resource_owner.linkedin.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\LinkedinResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.mailru.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\MailRuResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.qq.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\QQResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.reddit.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\RedditResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.salesforce.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SalesforceResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.sensio_connect.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SensioConnectResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.sina_weibo.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SinaWeiboResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.soundcloud.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SoundcloudResourceOwner</parameter>
Expand All @@ -52,8 +54,6 @@
<parameter key="hwi_oauth.resource_owner.yandex.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\YandexResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.odnoklassniki.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\OdnoklassnikiResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.37signals.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\ThirtySevenSignalsResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.salesforce.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\SalesforceResourceOwner</parameter>
<parameter key="hwi_oauth.resource_owner.reddit.class">HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\RedditResourceOwner</parameter>

<parameter key="hwi_oauth.resource_ownermap.class">HWI\Bundle\OAuthBundle\Security\Http\ResourceOwnerMap</parameter>
<parameter key="hwi_oauth.security.oauth_utils.class">HWI\Bundle\OAuthBundle\Security\OAuthUtils</parameter>
Expand Down
42 changes: 42 additions & 0 deletions Tests/OAuth/ResourceOwner/RedditResourceOwnerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the HWIOAuthBundle package.
*
* (c) Hardware.Info <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace HWI\Bundle\OAuthBundle\Tests\OAuth\ResourceOwner;

use HWI\Bundle\OAuthBundle\OAuth\ResourceOwner\RedditResourceOwner;

class RedditResourceOwnerTest extends GenericOAuth2ResourceOwnerTest
{
protected $userResponse = <<<json
{
"id": "1",
"name": "bar"
}
json;

protected $csrf = true;

protected $expectedUrls = array(
'authorization_url' => 'http://user.auth/?test=2&response_type=code&client_id=clientid&scope=identity&state=random&redirect_uri=http%3A%2F%2Fredirect.to%2F'
);

protected $paths = array(
'identifier' => 'id',
'nickname' => 'name',
'realname' => null,
'email' => null,
);

protected function setUpResourceOwner($name, $httpUtils, array $options)
{
return new RedditResourceOwner($this->buzzClient, $httpUtils, $options, $name, $this->storage);
}
}
8 changes: 4 additions & 4 deletions Tests/OAuth/ResourceOwner/SoundcloudResourceOwnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ class SoundcloudResourceOwnerTest extends GenericOAuth2ResourceOwnerTest
}
json;

protected $expectedUrls = array(
'authorization_url' => 'http://user.auth/?test=2&response_type=code&client_id=clientid&scope=non-expiring&redirect_uri=http%3A%2F%2Fredirect.to%2F',
'authorization_url_csrf' => 'http://user.auth/?test=2&response_type=code&client_id=clientid&scope=non-expiring&state=random&redirect_uri=http%3A%2F%2Fredirect.to%2F'
);
protected $expectedUrls = array(
'authorization_url' => 'http://user.auth/?test=2&response_type=code&client_id=clientid&scope=non-expiring&redirect_uri=http%3A%2F%2Fredirect.to%2F',
'authorization_url_csrf' => 'http://user.auth/?test=2&response_type=code&client_id=clientid&scope=non-expiring&state=random&redirect_uri=http%3A%2F%2Fredirect.to%2F'
);

protected $paths = array(
'identifier' => 'id',
Expand Down

0 comments on commit f889769

Please sign in to comment.