OAuth 1.0a and 2 support
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
interface Authentication {
|
interface Authentication {
|
||||||
public function handleHeader();
|
public function handleHeaders();
|
||||||
|
|
||||||
public function handleParams();
|
public function handleParams();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,15 +3,15 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
|
|||||||
|
|
||||||
class BasicAuthentication extends HeaderAuthentication {
|
class BasicAuthentication extends HeaderAuthentication {
|
||||||
|
|
||||||
private $header;
|
private $headers;
|
||||||
|
|
||||||
function __construct($username, $password) {
|
function __construct($username, $password) {
|
||||||
$headerValue = $username . ":" . $password;
|
$headerValue = $username . ":" . $password;
|
||||||
$this->header = "Authorization: Basic " . base64_encode($headerValue);
|
$this->headers = array("Authorization: Basic " . base64_encode($headerValue));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleHeader() {
|
public function handleHeaders() {
|
||||||
return $this->header;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleParams() {
|
public function handleParams() {
|
||||||
|
|||||||
@@ -3,14 +3,14 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
|
|||||||
|
|
||||||
class CustomHeaderAuthentication extends HeaderAuthentication {
|
class CustomHeaderAuthentication extends HeaderAuthentication {
|
||||||
|
|
||||||
private $header;
|
private $headers;
|
||||||
|
|
||||||
function __construct($headerName, $headerValue) {
|
function __construct($headerName, $headerValue) {
|
||||||
$this->header = $headerName . ": " . $headerValue;
|
$this->headers = array($headerName . ": " . $headerValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleHeader() {
|
public function handleHeaders() {
|
||||||
return $this->header;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleParams() {
|
public function handleParams() {
|
||||||
|
|||||||
@@ -4,14 +4,14 @@ require_once(dirname(__FILE__) . "/HeaderAuthentication.php");
|
|||||||
|
|
||||||
class MashapeAuthentication extends HeaderAuthentication {
|
class MashapeAuthentication extends HeaderAuthentication {
|
||||||
|
|
||||||
private $header;
|
private $headers;
|
||||||
|
|
||||||
function __construct($publicKey, $privateKey) {
|
function __construct($publicKey, $privateKey) {
|
||||||
$this->header = AuthenticationUtil::generateAuthenticationHeader($publicKey, $privateKey);
|
$this->headers = array(AuthenticationUtil::generateAuthenticationHeader($publicKey, $privateKey));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleHeader() {
|
public function handleHeaders() {
|
||||||
return $this->header;
|
return $this->headers;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
27
main/mashape/authentication/OAuth10aAuthentication.php
Normal file
27
main/mashape/authentication/OAuth10aAuthentication.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
24
main/mashape/authentication/OAuth2Authentication.php
Normal file
24
main/mashape/authentication/OAuth2Authentication.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
42
main/mashape/authentication/OAuthAuthentication.php
Normal file
42
main/mashape/authentication/OAuthAuthentication.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
||||||
|
|
||||||
|
|
||||||
@@ -9,7 +9,7 @@ class QueryAuthentication implements Authentication {
|
|||||||
$this->params = array($queryKey => $queryValue);
|
$this->params = array($queryKey => $queryValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function handleHeader() {
|
public function handleHeaders() {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -41,6 +41,16 @@ define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH_CODE", 1006);
|
|||||||
define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH",
|
define("EXCEPTION_CONTENT_TYPE_JSON_QUERYAUTH",
|
||||||
"Query Authentication cannot be used in conjunction with content type JSON");
|
"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_CODE", 415);
|
||||||
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE",
|
define("EXCEPTION_NOTSUPPORTED_CONTENTTYPE",
|
||||||
"Content Type not supported. Currently only application/x-www-form-urlencoded, "
|
"Content Type not supported. Currently only application/x-www-form-urlencoded, "
|
||||||
|
|||||||
@@ -35,6 +35,8 @@ require_once(dirname(__FILE__) . "/../authentication/BasicAuthentication.php");
|
|||||||
require_once(dirname(__FILE__) . "/../authentication/CustomHeaderAuthentication.php");
|
require_once(dirname(__FILE__) . "/../authentication/CustomHeaderAuthentication.php");
|
||||||
require_once(dirname(__FILE__) . "/../authentication/MashapeAuthentication.php");
|
require_once(dirname(__FILE__) . "/../authentication/MashapeAuthentication.php");
|
||||||
require_once(dirname(__FILE__) . "/../authentication/QueryAuthentication.php");
|
require_once(dirname(__FILE__) . "/../authentication/QueryAuthentication.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../authentication/Oauth10aAuthentication.php");
|
||||||
|
require_once(dirname(__FILE__) . "/../authentication/Oauth2Authentication.php");
|
||||||
|
|
||||||
class HttpClient {
|
class HttpClient {
|
||||||
|
|
||||||
|
|||||||
@@ -58,7 +58,7 @@ class HttpUtils {
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new MashapeClientException(
|
throw new MashapeClientException(
|
||||||
EXCEPTION_NOTSUPPORTED_CONTENTTYPE,
|
EXCEPTION_NOTSUPPORTED_CONTENTTYPE,
|
||||||
EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE);
|
EXCEPTION_NOTSUPPORTED_CONTENTTYPE_CODE);
|
||||||
}
|
}
|
||||||
return $data;
|
return $data;
|
||||||
@@ -73,7 +73,11 @@ class HttpUtils {
|
|||||||
if ($handler instanceof QueryAuthentication) {
|
if ($handler instanceof QueryAuthentication) {
|
||||||
$parameters = array_merge($parameters, $handler->handleParams());
|
$parameters = array_merge($parameters, $handler->handleParams());
|
||||||
} else if ($handler instanceof HeaderAuthentication) {
|
} 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);
|
return array($headers, $parameters);
|
||||||
|
|||||||
Reference in New Issue
Block a user