diff --git a/main/mashape/authentication/Authentication.php b/main/mashape/authentication/Authentication.php index 91979bb..5a3bc14 100644 --- a/main/mashape/authentication/Authentication.php +++ b/main/mashape/authentication/Authentication.php @@ -1,6 +1,6 @@ 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() { diff --git a/main/mashape/authentication/CustomHeaderAuthentication.php b/main/mashape/authentication/CustomHeaderAuthentication.php index 28c93f6..c1e1345 100644 --- a/main/mashape/authentication/CustomHeaderAuthentication.php +++ b/main/mashape/authentication/CustomHeaderAuthentication.php @@ -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() { diff --git a/main/mashape/authentication/MashapeAuthentication.php b/main/mashape/authentication/MashapeAuthentication.php index f568e41..82d0582 100644 --- a/main/mashape/authentication/MashapeAuthentication.php +++ b/main/mashape/authentication/MashapeAuthentication.php @@ -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; } } ?> diff --git a/main/mashape/authentication/OAuth10aAuthentication.php b/main/mashape/authentication/OAuth10aAuthentication.php new file mode 100644 index 0000000..04f8d34 --- /dev/null +++ b/main/mashape/authentication/OAuth10aAuthentication.php @@ -0,0 +1,27 @@ +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; + } +} +?> + + + diff --git a/main/mashape/authentication/OAuth2Authentication.php b/main/mashape/authentication/OAuth2Authentication.php new file mode 100644 index 0000000..60f9b12 --- /dev/null +++ b/main/mashape/authentication/OAuth2Authentication.php @@ -0,0 +1,24 @@ +accessToken == null) { + throw new MashapeClientException( + EXCEPTION_OAUTH2_AUTHORIZE, + EXCEPTION_OAUTH2_AUTHORIZE_CODE); + } + $params = array("accesstoken" => $this->accessToken); + return $params; + } +} +?> + + + + diff --git a/main/mashape/authentication/OAuthAuthentication.php b/main/mashape/authentication/OAuthAuthentication.php new file mode 100644 index 0000000..c4af9dd --- /dev/null +++ b/main/mashape/authentication/OAuthAuthentication.php @@ -0,0 +1,42 @@ +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; + } +} +?> + + diff --git a/main/mashape/authentication/QueryAuthentication.php b/main/mashape/authentication/QueryAuthentication.php index 3fe9c06..3693219 100644 --- a/main/mashape/authentication/QueryAuthentication.php +++ b/main/mashape/authentication/QueryAuthentication.php @@ -9,7 +9,7 @@ class QueryAuthentication implements Authentication { $this->params = array($queryKey => $queryValue); } - public function handleHeader() { + public function handleHeaders() { return null; } diff --git a/main/mashape/exceptions/ExceptionConstants.php b/main/mashape/exceptions/ExceptionConstants.php index 783800b..0b84ce2 100755 --- a/main/mashape/exceptions/ExceptionConstants.php +++ b/main/mashape/exceptions/ExceptionConstants.php @@ -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, " diff --git a/main/mashape/http/HttpClient.php b/main/mashape/http/HttpClient.php index 1527e84..0209684 100755 --- a/main/mashape/http/HttpClient.php +++ b/main/mashape/http/HttpClient.php @@ -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 { diff --git a/main/mashape/http/HttpUtils.php b/main/mashape/http/HttpUtils.php index 00ff336..ada4849 100644 --- a/main/mashape/http/HttpUtils.php +++ b/main/mashape/http/HttpUtils.php @@ -58,7 +58,7 @@ class HttpUtils { break; default: throw new MashapeClientException( - EXCEPTION_NOTSUPPORTED_CONTENTTYPE, + EXCEPTION_NOTSUPPORTED_CONTENTTYPE, EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE); } return $data; @@ -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);