Skip to content

Commit

Permalink
Merge pull request #7 from widop/f-response
Browse files Browse the repository at this point in the history
Introduce OAuth response and exception
  • Loading branch information
GeLoLabs committed Feb 7, 2014
2 parents c9c8a7f + 55d2135 commit 2fb30fd
Show file tree
Hide file tree
Showing 9 changed files with 610 additions and 79 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Expand Up @@ -2,7 +2,7 @@

<phpunit colors="true" bootstrap="./tests/bootstrap.php">
<testsuites>
<testsuite name="Widop Twitter Test Suite">
<testsuite name="Widop Twitter OAuth Test Suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>
Expand Down
96 changes: 40 additions & 56 deletions src/Widop/Twitter/OAuth/OAuth.php
Expand Up @@ -172,7 +172,7 @@ public function setVersion($version)
*/
public function getRequestToken($callback = 'oob')
{
$request = $this->createRequest('/oauth/request_token');
$request = $this->createRequest('/oauth/request_token', OAuthResponse::FORMAT_STR);
$request->setOAuthParameter('oauth_callback', $callback);
$this->signRequest($request, new OAuthToken());

Expand Down Expand Up @@ -213,7 +213,7 @@ public function getAuthenticateUrl(OAuthToken $requestToken)
*/
public function getAccessToken(OAuthToken $requestToken, $verifier)
{
$request = $this->createRequest('/oauth/access_token');
$request = $this->createRequest('/oauth/access_token', OAuthResponse::FORMAT_STR);
$request->setOAuthParameter('oauth_verifier', $verifier);
$this->signRequest($request, $requestToken);

Expand All @@ -229,17 +229,11 @@ public function getAccessToken(OAuthToken $requestToken, $verifier)
*/
public function getBearerToken($grantType = 'client_credentials')
{
$request = $this->createRequest('/oauth2/token', 'POST');
$request = $this->createRequest('/oauth2/token');
$request->setPostParameter('grant_type', $grantType);
$this->signRequest($request, new BasicToken());

$response = $this->getHttpAdapter()->postContent(
$request->getUrl(),
$request->getHeaders(),
$request->getPostParameters()
)->getBody();

return $this->createBearerToken($response);
return $this->createBearerToken($this->sendRequest($request));
}

/**
Expand All @@ -251,23 +245,14 @@ public function getBearerToken($grantType = 'client_credentials')
*/
public function invalidateBearerToken(BearerToken $token)
{
$request = $this->createRequest('/oauth2/invalidate_token', 'POST');
$request = $this->createRequest('/oauth2/invalidate_token');
$request->setPostParameter('access_token', $token->getValue());
$this->signRequest($request, new BasicToken());

$response = $this->getHttpAdapter()->postContent(
$request->getUrl(),
$request->getHeaders(),
$request->getPostParameters()
)->getBody();

$result = json_decode($response, true);
$response = $this->sendRequest($request);

if (($result === null) || isset($result['errors']) || ($token->getValue() !== $result['access_token'])) {
throw new \RuntimeException(sprintf(
'An error occured when invalidating the bearer token.',
$response
));
if ($token->getValue() !== $response->getData('access_token')) {
throw new OAuthException('An error occured when invalidating the bearer token.', $response);
}
}

Expand All @@ -289,98 +274,97 @@ public function signRequest(OAuthRequest $request, TokenInterface $token)
*
* @throws \RuntimeException If the request method is not supported.
*
* @return string The response body.
* @return \Widop\Twitter\OAuth\OAuthResponse The OAuth response.
*/
public function sendRequest(OAuthRequest $request)
{
switch ($request->getMethod()) {
case OAuthRequest::METHOD_GET:
return $this->httpAdapter->getContent(
$request->getUrl(),
$request->getHeaders()
)->getBody();
$httpResponse = $this->httpAdapter->getContent($request->getUrl(), $request->getHeaders());
break;

case OAuthRequest::METHOD_POST:
$postParameters = array();
foreach ($request->getPostParameters() as $name => $value) {
$postParameters[rawurldecode($name)] = rawurldecode($value);
}

return $this->httpAdapter->postContent(
$httpResponse = $this->httpAdapter->postContent(
$request->getUrl(),
$request->getHeaders(),
$postParameters,
$request->getFileParameters()
)->getBody();
);
break;

default:
throw new \RuntimeException(sprintf(
'The request method "%s" is not supported.',
$request->getMethod()
));
}

$response = new OAuthResponse($httpResponse, $request->getResponseFormat());

if (!$response->isValid()) {
throw new OAuthException('The http response is not valid.', $response);
}

return $response;
}

/**
* Creates an OAuth request.
*
* @param string $path The OAuth path.
* @param string $path The OAuth path.
* @param string $responseFormat The response format.
*
* @return \Widop\Twitter\OAuth\OAuthRequest The OAuth request.
*/
private function createRequest($path)
private function createRequest($path, $responseFormat = OAuthResponse::FORMAT_JSON)
{
$request = new OAuthRequest();
$request->setBaseUrl($this->getUrl());
$request->setPath($path);
$request->setMethod(OAuthRequest::METHOD_POST);
$request->setResponseFormat($responseFormat);

return $request;
}

/**
* Creates an oauth token according to an http response.
* Creates an oauth token according to an OAuth response.
*
* @param string $response The http response.
* @param \Widop\Twitter\OAuth\OAuthResponse $response The OAuth response.
*
* @throws \RuntimeException If the token cannot be created.
* @throws \Widop\Twitter\OAuth\OAuthException If the token cannot be created.
*
* @return \Widop\Twitter\OAuth\Token\OAuthToken The OAuth token.
*/
private function createOAuthToken($response)
private function createOAuthToken(OAuthResponse $response)
{
parse_str($response, $datas);

if (!isset($datas['oauth_token']) || !isset($datas['oauth_token_secret'])) {
throw new \RuntimeException(sprintf(
'An error occured when creating the OAuth token. (%s)',
str_replace("\n", '', $response)
));
if (!$response->hasData('oauth_token') || !$response->hasData('oauth_token_secret')) {
throw new OAuthException('An error occured when creating the OAuth token.', $response);
}

return new OAuthToken($datas['oauth_token'], $datas['oauth_token_secret']);
return new OAuthToken($response->getData('oauth_token'), $response->getData('oauth_token_secret'));
}

/**
* Creates a bearer token according to an http response.
* Creates a bearer token according to an OAuth response.
*
* @param string $response The http response.
* @param \Widop\Twitter\OAuth\OAuthResponse $response The OAuth response.
*
* @throws \RuntimeException If the token cannot be created.
* @throws \Widop\Twitter\OAuth\OAuthException If the token cannot be created.
*
* @return \Widop\Twitter\OAuth\Token\BearerToken The Bearer token.
*/
private function createBearerToken($response)
private function createBearerToken(OAuthResponse $response)
{
$datas = json_decode($response, true);

if (($datas === null) || !isset($datas['token_type']) || !isset($datas['access_token'])) {
throw new \RuntimeException(sprintf(
'An error occured when creating the bearer token. (%s)',
str_replace("\n", '', $response)
));
if (!$response->hasData('token_type') || !$response->hasData('access_token')) {
throw new OAuthException('An error occured when creating the bearer token.', $response);
}

return new BearerToken($datas['access_token']);
return new BearerToken($response->getData('access_token'));
}
}
46 changes: 46 additions & 0 deletions src/Widop/Twitter/OAuth/OAuthException.php
@@ -0,0 +1,46 @@
<?php

/*
* This file is part of the Wid'op package.
*
* (c) Wid'op <contact@widop.com>
*
* For the full copyright and license information, please read the LICENSE
* file that was distributed with this source code.
*/

namespace Widop\Twitter\OAuth;

/**
* OAuth exception.
*
* @author GeLo <geloen.eric@gmail.com>
*/
class OAuthException extends \Exception
{
/** @var \Widop\Twitter\OAuth\OAuthResponse */
private $response;

/**
* Creates an OAuth exception.
*
* @param string $message The message.
* @param \Widop\Twitter\OAuth\OAuthResponse $response The response.
*/
public function __construct($message, OAuthResponse $response)
{
parent::__construct($message, null, null);

$this->response = $response;
}

/**
* Gets the response.
*
* @return \Widop\Twitter\OAuth\OAuthResponse The response.
*/
public function getResponse()
{
return $this->response;
}
}
25 changes: 25 additions & 0 deletions src/Widop/Twitter/OAuth/OAuthRequest.php
Expand Up @@ -51,6 +51,9 @@ class OAuthRequest
/** @var array */
private $fileParameters;

/** @var string */
private $responseFormat;

/**
* Creates an OAuth request.
*/
Expand All @@ -62,6 +65,8 @@ public function __construct()
$this->getParameters = array();
$this->postParameters = array();
$this->fileParameters = array();

$this->setResponseFormat(OAuthResponse::FORMAT_JSON);
}

/**
Expand Down Expand Up @@ -701,6 +706,26 @@ public function removeFileParameter($name)
unset($this->fileParameters[$name]);
}

/**
* Gets the response format.
*
* @return string The response format.
*/
public function getResponseFormat()
{
return $this->responseFormat;
}

/**
* Sets the response format.
*
* @param string $responseFormat The response format.
*/
public function setResponseFormat($responseFormat)
{
$this->responseFormat = $responseFormat;
}

/**
* Gets the request signature.
*
Expand Down

0 comments on commit 2fb30fd

Please sign in to comment.