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;
}