From f6d8b546fbf86ea104bac12e90f7293fa4cbe707 Mon Sep 17 00:00:00 2001
From: arnested <arnested@245635.no-reply.drupal.org>
Date: Wed, 12 Dec 2012 12:43:04 +0100
Subject: [PATCH] Issue #1864628 by arnested: Added HTTP Basic Authentication
 and PHP FastCGI.
Reply-To: Arne =?UTF-8?Q?J=C3=B8rgensen?= <arne@arnested.dk>

---
 htaccess-fastcgi.patch  | 13 +++++++++++++
 services_basic_auth.inc |  7 +++++++
 2 files changed, 20 insertions(+)
 create mode 100644 htaccess-fastcgi.patch

diff --git a/htaccess-fastcgi.patch b/htaccess-fastcgi.patch
new file mode 100644
index 0000000..e46f966
--- /dev/null
+++ b/htaccess-fastcgi.patch
@@ -0,0 +1,13 @@
+diff --git a/.htaccess b/.htaccess
+index 246edc2..e579b6b 100644
+--- a/.htaccess
++++ b/.htaccess
+@@ -128,4 +128,8 @@ DirectoryIndex index.php index.html index.htm
+       Header append Vary Accept-Encoding
+     </FilesMatch>
+   </IfModule>
++
++  # PHP FastCGI doesn't support HTTP Basic Authentication out of the
++  # box so we need this workaround. See http://drupal.org/node/1864628.
++  RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
+ </IfModule>
diff --git a/services_basic_auth.inc b/services_basic_auth.inc
index c843bd6..f84762f 100644
--- a/services_basic_auth.inc
+++ b/services_basic_auth.inc
@@ -18,6 +18,13 @@
  *  Returns nothing, or a error message if authentication fails
  */
 function _services_basic_auth_authenticate_call($settings, $method, $args) {
+  // PHP FastCGI doesn't support HTTP Basic Authentication out of the box so we
+  // need this workaround. Requires a patch to .htaccess.
+  // @see http://drupal.org/node/1864628.
+  if (isset($_SERVER['HTTP_AUTHORIZATION']) AND !empty($_SERVER['HTTP_AUTHORIZATION'])) {
+    list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)), 2);
+  }
+
   if (user_is_anonymous() && isset($_SERVER['PHP_AUTH_USER']) && isset($_SERVER['PHP_AUTH_PW'])) {
     $form_state = array();
     $form_state['values']['name'] = $_SERVER['PHP_AUTH_USER'];
-- 
1.8.0.2

