From cf91d4474782519166b1a4c6b3290dd81a03d0ad Mon Sep 17 00:00:00 2001 From: Chuck Hagenbuch Date: Thu, 1 Oct 2009 13:42:53 -0400 Subject: [PATCH] Add HTTP auth support. All adapters support BASIC; Curl and Peclhttp support a bunch more. --- framework/Http/lib/Horde/Http.php | 29 +++++++++++++++++ framework/Http/lib/Horde/Http/Request/Base.php | 35 +++++++++++++++++++-- framework/Http/lib/Horde/Http/Request/Curl.php | 31 +++++++++++++++++++ framework/Http/lib/Horde/Http/Request/Fopen.php | 17 ++++++++-- framework/Http/lib/Horde/Http/Request/Peclhttp.php | 36 ++++++++++++++++++++++ framework/Http/package.xml | 2 ++ 6 files changed, 146 insertions(+), 4 deletions(-) create mode 100644 framework/Http/lib/Horde/Http.php diff --git a/framework/Http/lib/Horde/Http.php b/framework/Http/lib/Horde/Http.php new file mode 100644 index 000000000..c2ac18ce2 --- /dev/null +++ b/framework/Http/lib/Horde/Http.php @@ -0,0 +1,29 @@ + + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ + +/** + * Constants + * + * @author Chuck Hagenbuch + * @license http://opensource.org/licenses/bsd-license.php BSD + * @category Horde + * @package Horde_Http + */ +class Horde_Http +{ + /** + * Authentication schemes + */ + const AUTH_ANY = 'ANY'; + const AUTH_BASIC = 'BASIC'; + const AUTH_DIGEST = 'DIGEST'; + const AUTH_NTLM = 'NTLM'; + const AUTH_GSSNEGOTIATE = 'GSSNEGOTIATE'; +} diff --git a/framework/Http/lib/Horde/Http/Request/Base.php b/framework/Http/lib/Horde/Http/Request/Base.php index 4df303138..d6f72b9d0 100644 --- a/framework/Http/lib/Horde/Http/Request/Base.php +++ b/framework/Http/lib/Horde/Http/Request/Base.php @@ -35,6 +35,13 @@ abstract class Horde_Http_Request_Base protected $_headers = array(); /** + * Authentication data + * @var array + * @see getAuth() + */ + protected $_auth; + + /** * Request data. Can be an array of form data that will be encoded * automatically, or a raw string * @var mixed @@ -42,6 +49,24 @@ abstract class Horde_Http_Request_Base protected $_data; /** + * Authentication username + * @var string + */ + protected $_username = ''; + + /** + * Authentication password + * @var string + */ + protected $_password = ''; + + /** + * Authentication scheme + * @var const Horde_Http::AUTH_* + */ + protected $_authenticationScheme = Horde_Http::AUTH_BASIC; + + /** * Proxy server * @var string */ @@ -51,13 +76,19 @@ abstract class Horde_Http_Request_Base * Proxy username * @var string */ - protected $_proxyUser = null; + protected $_proxyUsername = null; /** * Proxy password * @var string */ - protected $_proxyPass = null; + protected $_proxyPassword = null; + + /** + * Proxy authentication schem + * @var const Horde_Http::AUTH_* + */ + protected $_proxyAuthenticationScheme = Horde_Http::AUTH_BASIC; /** * HTTP timeout diff --git a/framework/Http/lib/Horde/Http/Request/Curl.php b/framework/Http/lib/Horde/Http/Request/Curl.php index 86a4642f0..ca02b9f09 100644 --- a/framework/Http/lib/Horde/Http/Request/Curl.php +++ b/framework/Http/lib/Horde/Http/Request/Curl.php @@ -47,6 +47,37 @@ class Horde_Http_Request_Curl extends Horde_Http_Request_Base } if ($data) { curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } + // Proxy + + // Set authentication data + if ($this->username) { + curl_setopt($curl, CURLOPT_USERPWD, $this->username . ':' . $this->password); + switch ($this->authenticationScheme) { + case Horde_Http::AUTH_ANY: + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); + break; + + case Horde_Http::AUTH_BASIC: + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); + break; + + case Horde_Http::AUTH_DIGEST: + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_DIGEST); + break; + + case Horde_Http::AUTH_GSSNEGOTIATE: + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_GSSNEGOTIATE); + break; + + case Horde_Http::AUTH_NTLM: + curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_NTLM); + break; + + default: + throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')'); + } + } + // Concatenate the headers $hdr = array(); foreach ($this->headers as $header => $value) { diff --git a/framework/Http/lib/Horde/Http/Request/Fopen.php b/framework/Http/lib/Horde/Http/Request/Fopen.php index 06febb722..9f3eea543 100644 --- a/framework/Http/lib/Horde/Http/Request/Fopen.php +++ b/framework/Http/lib/Horde/Http/Request/Fopen.php @@ -44,8 +44,21 @@ class Horde_Http_Request_Fopen extends Horde_Http_Request_Base if ($this->proxyServer) { $opts['http']['proxy'] = 'tcp://' . $this->proxyServer; $opts['http']['request_fulluri'] = true; - if ($this->proxyUser && $this->proxyPass) { - $headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->proxyUser . ':' . $this->proxyPass); + if ($this->proxyUsername && $this->proxyPassword) { + // @TODO check $this->proxyAuthenticationScheme + $headers['Proxy-Authorization'] = 'Basic ' . base64_encode($this->proxyUsername . ':' . $this->proxyPassword); + } + } + + // Set authentication data + if ($this->username) { + switch ($this->authenticationScheme) { + case Horde_Http::AUTH_BASIC: + $headers['Authorization'] = 'Basic ' . base64_encode($this->username . ':' . $this->password); + break; + + default: + throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')'); } } diff --git a/framework/Http/lib/Horde/Http/Request/Peclhttp.php b/framework/Http/lib/Horde/Http/Request/Peclhttp.php index 18de6c5bc..b9f7c4623 100644 --- a/framework/Http/lib/Horde/Http/Request/Peclhttp.php +++ b/framework/Http/lib/Horde/Http/Request/Peclhttp.php @@ -48,6 +48,42 @@ class Horde_Http_Request_Peclhttp extends Horde_Http_Request_Base $httpRequest->setRawPostData($data); } + $httpOptions = array(); + + // Proxy + + // Set authentication data + if ($this->username) { + $httpOptions['httpauth'] = $this->username . ':' . $this->password; + switch ($this->authenticationScheme) { + case Horde_Http::AUTH_ANY: + $httpOptions['httpauthtype'] = HTTP_AUTH_ANY; + break; + + case Horde_Http::AUTH_BASIC: + $httpOptions['httpauthtype'] = HTTP_AUTH_BASIC; + break; + + case Horde_Http::AUTH_DIGEST: + $httpOptions['httpauthtype'] = HTTP_AUTH_DIGEST; + break; + + case Horde_Http::AUTH_GSSNEGOTIATE: + $httpOptions['httpauthtype'] = HTTP_AUTH_GSSNEG; + break; + + case Horde_Http::AUTH_NTLM: + $httpOptions['httpauthtype'] = HTTP_AUTH_NTLM; + break; + + default: + throw new Horde_Http_Exception('Unsupported authentication scheme (' . $this->authenticationScheme . ')'); + } + } + + // Set options + $httpRequest->setOptions($httpOptions); + try { $httpResponse = $httpRequest->send(); } catch (HttpException $e) { diff --git a/framework/Http/package.xml b/framework/Http/package.xml index 89d9d69ce..7924dde21 100644 --- a/framework/Http/package.xml +++ b/framework/Http/package.xml @@ -50,6 +50,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + @@ -83,6 +84,7 @@ http://pear.php.net/dtd/package-2.0.xsd"> + -- 2.11.0