Skip to content

Commit

Permalink
Merge pull request #4 from gimler/cleanup
Browse files Browse the repository at this point in the history
fix cs; add/fix phpdocs; reduce code
  • Loading branch information
mazen committed Jan 10, 2012
2 parents 45b9472 + c52c27e commit 123f144
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 63 deletions.
13 changes: 10 additions & 3 deletions Provider/FacebookProvider.php
Expand Up @@ -3,13 +3,18 @@
namespace Etcpasswd\OAuthBundle\Provider;

use Etcpasswd\OAuthBundle\Provider\Token\FacebookToken;

/**
* OAuth provider for facebook
*
* @author Marcel Beerta <marcel@etcpasswd.de>
* @author Marcel Beerta <marcel@etcpasswd.de>
* @link http://developers.facebook.com/docs/authentication/
*/
class FacebookProvider extends Provider
{

/**
* {@inheritDoc}
*/
public function createTokenResponse($clientId, $secret, $code, $redirectUrl = "")
{
$url = 'https://graph.facebook.com/oauth/access_token'
Expand All @@ -35,12 +40,14 @@ public function createTokenResponse($clientId, $secret, $code, $redirectUrl = ""
return new FacebookToken($json, $accessToken, $expiresAt);
}

/**
* {@inheritDoc}
*/
public function getAuthorizationUrl($clientId, $scope, $redirectUrl)
{
return 'https://www.facebook.com/dialog/oauth'
.'?client_id='.$clientId
.'&redirect_uri='.$redirectUrl
.'&scope='.$scope;
}

}
33 changes: 13 additions & 20 deletions Provider/GithubProvider.php
Expand Up @@ -2,33 +2,16 @@

namespace Etcpasswd\OAuthBundle\Provider;

use Etcpasswd\OAuthBundle\Provider\TokenResponseInterface,
Etcpasswd\OAuthBundle\Provider\Token\GithubToken;

use Buzz\Client\ClientInterface,
Buzz\Message\Request,
Buzz\Message\Response;
use Etcpasswd\OAuthBundle\Provider\Token\GithubToken;

/**
* OAuth provider for github
*
* @see http://developer.github.com/v3/oauth
* @author Marcel Beerta <marcel@etcpasswd.de>
* @author Marcel Beerta <marcel@etcpasswd.de>
* @link http://developer.github.com/v3/oauth
*/
class GithubProvider extends Provider
{

/**
* {@inheritDoc}
*/
public function getAuthorizationUrl($clientId, $scope, $redirectUrl)
{
return 'https://github.com/login/oauth/authorize'
.'?client_id='.$clientId
.'&scope='.$scope
.'&redirect_url='.urlencode($redirectUrl);
}

/**
* {@inheritDoc}
*/
Expand Down Expand Up @@ -91,4 +74,14 @@ public function createTokenResponse($clientId, $secret, $code, $redirectUrl = ""
return new GithubToken($jsonObject, $result['access_token']);
}

/**
* {@inheritDoc}
*/
public function getAuthorizationUrl($clientId, $scope, $redirectUrl)
{
return 'https://github.com/login/oauth/authorize'
.'?client_id='.$clientId
.'&scope='.$scope
.'&redirect_url='.urlencode($redirectUrl);
}
}
25 changes: 13 additions & 12 deletions Provider/GoogleProvider.php
Expand Up @@ -2,20 +2,23 @@

namespace Etcpasswd\OAuthBundle\Provider;

use Buzz\Message\Request;
use Buzz\Message\Response;
use Etcpasswd\OAuthBundle\Provider\Token\GoogleToken;

use Buzz\Message\Request,
Buzz\Message\Response;
/**
* OAuth provider for google
*
* @author Marcel Beerta <marcel@etcpasswd.de>
* @author Marcel Beerta <marcel@etcpasswd.de>
* @link http://code.google.com/apis/accounts/docs/OAuth2.html
*/
class GoogleProvider extends Provider
{

/**
* {@inheritDoc}
*/
public function createTokenResponse($clientId, $secret, $code, $redirectUrl = "")
{

$url = 'https://www.google.com/accounts/o8/oauth2/token';

$request = new Request(Request::METHOD_POST, $url);
Expand All @@ -34,20 +37,19 @@ public function createTokenResponse($clientId, $secret, $code, $redirectUrl = ""
if (isset($data->error)) {
return;
}
$expiresAt = time()+$data->expires_in;
$expiresAt = time() + $data->expires_in;

$people = 'https://www.googleapis.com/plus/v1/people/me'
.'?key='.$clientId
.'&access_token='.$data->access_token;
$request = new Request(Request::METHOD_GET, $people);
$response = new Response();

$this->client->send($request, $response);
$me = json_decode($response->getContent());
$me = json_decode($this->request($people));

return new GoogleToken($me, $data->access_token, $expiresAt);
}

/**
* {@inheritDoc}
*/
public function getAuthorizationUrl($clientId, $scope, $redirectUrl)
{
return 'https://accounts.google.com/o/oauth2/auth'
Expand All @@ -56,5 +58,4 @@ public function getAuthorizationUrl($clientId, $scope, $redirectUrl)
.'&scope='.urlencode($scope)
.'&response_type=code';
}

}
17 changes: 10 additions & 7 deletions Provider/Provider.php
Expand Up @@ -2,14 +2,14 @@

namespace Etcpasswd\OAuthBundle\Provider;


use Buzz\Client\ClientInterface,
Buzz\Message\Request,
Buzz\Message\Response;
use Buzz\Client\ClientInterface;
use Buzz\Message\Request;
use Buzz\Message\Response;

/**
* Base Provider class
*
* @author Marcel Beerta <marcel@etcpasswd.de>
* @author Marcel Beerta <marcel@etcpasswd.de>
*/
abstract class Provider implements ProviderInterface
{
Expand All @@ -22,11 +22,14 @@ public function __construct(ClientInterface $client)

protected function request($url, $method = null)
{
$method = is_null($method) ? Request::METHOD_GET : $method;
if (null === $method) {
$method = Request::METHOD_GET;
}

$request = new Request($method, $url);
$response = new Response();
$this->client->send($request, $response);

return $response->getContent();
}

}
35 changes: 16 additions & 19 deletions Provider/ProviderInterface.php
Expand Up @@ -3,35 +3,32 @@
namespace Etcpasswd\OAuthBundle\Provider;

/**
* Interface
* Provider interface
*
* @author Marcel Beerta <marcel@etcpasswd.de>
* @author Marcel Beerta <marcel@etcpasswd.de>
*/
interface ProviderInterface
{
/**
* Returns the URL which is called to authorize a user
*
* @param string $client_id Client Id
* @param string $scope Scope requested
* @param string $redirect_url URL To redirect to after authorization
*
* @return string
*/
function getAuthorizationUrl($clientId, $scope, $redirectUrl);

/**
* Returns an authorization token which contains data about the given user
*
*
* @param string $client_id Client Id
* @param string $secret Client Secret
* @param string $redirect_url Redirect url
* @param string $code Authentication code
* @param string $clientId Client Id
* @param string $secret Client Secret
* @param string $code Authentication code
* @param string $redirectUrl Redirect url
*
* @return TokenResponseInterface|null
*/
function createTokenResponse($clientId, $secret, $code, $redirectUrl = "");


/**
* Returns the URL which is called to authorize a user
*
* @param string $clientId Client Id
* @param string $scope Scope requested
* @param string $redirectUrl URL To redirect to after authorization
*
* @return string
*/
function getAuthorizationUrl($clientId, $scope, $redirectUrl);
}
4 changes: 2 additions & 2 deletions Security/Http/Firewall/OAuthListener.php
Expand Up @@ -74,9 +74,9 @@ protected function attemptAuthentication(Request $request)
$this->options['client_secret'],
$code,
$this->assembleRedirectUrl($this->options['check_path'], $request)
);
);

if(is_null($token)) {
if (is_null($token)) {
throw new AuthenticationException('Authentication failed');
}

Expand Down

0 comments on commit 123f144

Please sign in to comment.