OAuth 1.0a and 2 support

This commit is contained in:
Evan Seguin
2012-10-02 15:59:55 -07:00
parent 0e6e912176
commit 33ba91700b
11 changed files with 125 additions and 16 deletions

View File

@@ -1,6 +1,6 @@
<?php
interface Authentication {
public function handleHeader();
public function handleHeaders();
public function handleParams();
}

View File

@@ -3,15 +3,15 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class BasicAuthentication extends HeaderAuthentication {
private $header;
private $headers;
function __construct($username, $password) {
$headerValue = $username . ":" . $password;
$this->header = "Authorization: Basic " . base64_encode($headerValue);
$this->headers = array("Authorization: Basic " . base64_encode($headerValue));
}
public function handleHeader() {
return $this->header;
public function handleHeaders() {
return $this->headers;
}
public function handleParams() {

View File

@@ -3,14 +3,14 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class CustomHeaderAuthentication extends HeaderAuthentication {
private $header;
private $headers;
function __construct($headerName, $headerValue) {
$this->header = $headerName . ": " . $headerValue;
$this->headers = array($headerName . ": " . $headerValue);
}
public function handleHeader() {
return $this->header;
public function handleHeaders() {
return $this->headers;
}
public function handleParams() {

View File

@@ -4,14 +4,14 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
class MashapeAuthentication extends HeaderAuthentication {
private $header;
private $headers;
function __construct($publicKey, $privateKey) {
$this->header = AuthenticationUtil::generateAuthenticationHeader($publicKey, $privateKey);
$this->headers = array(AuthenticationUtil::generateAuthenticationHeader($publicKey, $privateKey));
}
public function handleHeader() {
return $this->header;
public function handleHeaders() {
return $this->headers;
}
}
?>

View File

@@ -0,0 +1,27 @@
<?php
require_once(dirname(__FILE__) . "/OAuthAuthentication.php");
class OAuth10aAuthentication extends OAuthAuthentication {
function __construct($consumerKey, $consumerSecret, $redirectUrl) {
parent::__construct($consumerKey, $consumerSecret, $redirectUrl);
}
public function handleHeaders() {
if (!isset($this->accessToken) || !isset($this->accessSecret)) {
throw new MashapeClientException(
EXCEPTION_OAUTH1_AUTHORIZE,
EXCEPTION_OAUTH1_AUTHORIZE_CODE);
}
$headers = array();
$headers[] = "x-mashape-oauth-consumerkey: " . $this->consumerKey;
$headers[] = "x-mashape-oauth-consumersecret: " . $this->consumerSecret;
$headers[] = "x-mashape-oauth-accesstoken: " . $this->accessToken;
$headers[] = "x-mashape-oauth-accesssecret: " . $this->accessSecret;
return $headers;
}
}
?>

View File

@@ -0,0 +1,24 @@
<?php
require_once(dirname(__FILE__) . "/OAuthAuthentication.php");
class OAuth2Authentication extends OAuthAuthentication {
function __construct($consumerKey, $consumerSecret, $redirectUrl) {
parent::__construct($consumerKey, $consumerSecret, $redirectUrl);
}
public function handleParams() {
if ($this->accessToken == null) {
throw new MashapeClientException(
EXCEPTION_OAUTH2_AUTHORIZE,
EXCEPTION_OAUTH2_AUTHORIZE_CODE);
}
$params = array("accesstoken" => $this->accessToken);
return $params;
}
}
?>

View File

@@ -0,0 +1,42 @@
<?php
require_once(dirname(__FILE__) . "/Authentication.php");
class OAuthAuthentication implements Authentication {
protected $consumerKey;
protected $consumerSecret;
protected $redirectUrl;
protected $accessToken;
protected $accessSecret;
function __construct($consumerKey, $consumerSecret, $redirectUrl) {
$this->consumerKey = $consumerKey;
$this->consumerSecret = $consumerSecret;
$this->redirectUrl = $redirectUrl;
}
public function addAccessToken($accessToken, $accessSecret = null) {
$this->accessToken = $accessToken;
$this->accessSecret = $accessSecret;
}
public function getOAuthBaseParams() {
$params = array(
"consumerKey" => $this->consumerKey,
"consumerSecret" => $this->consumerSecret,
"redirectUrl" => $this->redirectUrl,
);
return $params;
}
public function handleParams() {
return null;
}
public function handleHeaders() {
return null;
}
}
?>

View File

@@ -9,7 +9,7 @@ class QueryAuthentication implements Authentication {
$this->params = array($queryKey => $queryValue);
}
public function handleHeader() {
public function handleHeaders() {
return null;
}

View File

@@ -41,6 +41,16 @@ define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH_CODE", 1006);
define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH",
"Query Authentication cannot be used in conjunction with content type JSON");
define("EXCEPTION_OAUTH1_AUTHORIZE_CODE", 1007);
define("EXCEPTION_OAUTH1_AUTHORIZE",
"Before consuming an OAuth endpoint, you must invoke the authorize("
."'access_token', 'access_secret') function with non-null values");
define("EXCEPTION_OAUTH2_AUTHORIZE_CODE", 1007);
define("EXCEPTION_OAUTH2_AUTHORIZE",
"Before consuming an OAuth endpoint, you must invoke the authorize("
."'access_token') function with a non-null value");
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE", 415);
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE",
"Content Type not supported. Currently only application/x-www-form-urlencoded, "

View File

@@ -35,6 +35,8 @@ require_once(dirname(__FILE__) . "/../authentication/BasicAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/CustomHeaderAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/MashapeAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/QueryAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/Oauth10aAuthentication.php");
require_once(dirname(__FILE__) . "/../authentication/Oauth2Authentication.php");
class HttpClient {

View File

@@ -73,7 +73,11 @@ class HttpUtils {
if ($handler instanceof QueryAuthentication) {
$parameters = array_merge($parameters, $handler->handleParams());
} else if ($handler instanceof HeaderAuthentication) {
$headers[] = $handler->handleHeader();
$headers = array_merge($headers, $handler->handleHeaders());
} else if ($handler instanceof Oauth10aAuthentication) {
$headers = array_merge($headers, $handler->handleHeaders());
} else if ($handler instanceof Oauth2Authentication) {
$parameters = array_merge($parameters, $handler->handleParams());
}
}
return array($headers, $parameters);