Add HTTP auth support. All adapters support BASIC; Curl and Peclhttp support a bunch...
authorChuck Hagenbuch <chuck@horde.org>
Thu, 1 Oct 2009 17:42:53 +0000 (13:42 -0400)
committerChuck Hagenbuch <chuck@horde.org>
Thu, 1 Oct 2009 17:42:53 +0000 (13:42 -0400)
framework/Http/lib/Horde/Http.php [new file with mode: 0644]
framework/Http/lib/Horde/Http/Request/Base.php
framework/Http/lib/Horde/Http/Request/Curl.php
framework/Http/lib/Horde/Http/Request/Fopen.php
framework/Http/lib/Horde/Http/Request/Peclhttp.php
framework/Http/package.xml

diff --git a/framework/Http/lib/Horde/Http.php b/framework/Http/lib/Horde/Http.php
new file mode 100644 (file)
index 0000000..c2ac18c
--- /dev/null
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Copyright 2007-2009 The Horde Project (http://www.horde.org/)
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @license  http://opensource.org/licenses/bsd-license.php BSD
+ * @category Horde
+ * @package  Horde_Http
+ */
+
+/**
+ * Constants
+ *
+ * @author   Chuck Hagenbuch <chuck@horde.org>
+ * @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';
+}
index 4df3031..d6f72b9 100644 (file)
@@ -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
index 86a4642..ca02b9f 100644 (file)
@@ -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) {
index 06febb7..9f3eea5 100644 (file)
@@ -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 . ')');
             }
         }
 
index 18de6c5..b9f7c46 100644 (file)
@@ -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) {
index 89d9d69..7924dde 100644 (file)
@@ -50,6 +50,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <dir name="Response">
       </dir> <!-- /lib/Horde/Http/Response -->
      </dir> <!-- /lib/Horde/Http -->
+     <file name="Http.php" role="php" />
     </dir> <!-- /lib/Horde -->
    </dir> <!-- /lib/ -->
   </dir> <!-- / -->
@@ -83,6 +84,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Http/Response/Fopen.php" as="Horde/Http/Response/Fopen.php" />
    <install name="lib/Horde/Http/Response/Mock.php" as="Horde/Http/Response/Mock.php" />
    <install name="lib/Horde/Http/Response/Peclhttp.php" as="Horde/Http/Response/Peclhttp.php" />
+   <install name="lib/Horde/Http.php" as="Horde/Http.php" />
   </filelist>
  </phprelease>
 </package>