Published on Oct 12 2019 in Non-Java PHP

Remedy for CGI/FastCGI PHP users that face ‘Could not authenticate to server: rejected Basic challenge’ with OwnCloud 8.2

When using CGI/FastCGI with ownCloud 8.2 you will get ‘Could not authenticate to server: rejected Basic challenge’ like in the following examples.

Trying with /etc/fstab entry in place:

$ mount /home/john/oc
Please enter the username to authenticate with server
https://domain.com/owncloud/remote.php/webdav/ or hit enter for none.
  Username: john
Please enter the password to authenticate user john with server
https://domain.com/owncloud/remote.php/webdav/ or hit enter for none.
  Password:  
/sbin/mount.davfs: Mounting failed.
Could not authenticate to server: rejected Basic challenge

Trying with cadaver:

$ cadaver -t https://domain.com/owncloud/remote.php/webdav/
Authentication required for ownCloud on server `domain.com':
Username: john
Password: 
Could not open collection:
Could not authenticate to server: rejected Basic challenge
dav:/owncloud/remote.php/webdav/? 

This is because CGI uses REMOTE_USER. Make 2 small changes:

Change #1 in lib/composer/sabre/http/lib/Sapi.php:

--- Sapi.php.orig   2019-10-12 03:33:30.600308822 -0500
+++ Sapi.php    2019-10-12 03:33:41.735060516 -0500
@@ -157,6 +157,10 @@
                     $headers['Authorization'] = $value;
                     break;
 
+                case 'REDIRECT_REMOTE_USER' :
+                    $headers['Authorization'] = $value;
+                    break;
+
                 case 'HTTP_HOST' :
                     $hostName = $value;
                     $headers['Host'] = $value;

Change #2 in .htaccess:

--- .htaccess.orig  2019-10-12 03:38:50.947165351 -0500
+++ .htaccess   2019-10-12 03:38:35.675505883 -0500
@@ -70,6 +70,7 @@
 
 <IfModule mod_rewrite.c>
   RewriteEngine on
+  RewriteRule .* - [env=REMOTE_USER:%{HTTP:Authorization}]
   RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
   RewriteRule ^\.well-known/host-meta /public.php?service=host-meta [QSA,L]
   RewriteRule ^\.well-known/host-meta\.json /public.php?service=host-meta-json [QSA,L]

Now enjoy WebDAV to your OwnClound 8.2 with CGI/FastCGI :)