2
votes

Hello i have command line code:

curl 'http://192.168.0.1:80/' -H 'Authorization: Basic my_auth'

In php:

$process = curl_init($ip);
//just for "shore"
            curl_setopt($data, CURLOPT_HTTPHEADER,
            array(
              "Authorization: Basic " . base64_encode($username . ":" . $password)
            ));
            curl_setopt($process, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
            curl_setopt($process, CURLOPT_USERPWD, $login . ":" . $password);
            curl_setopt($process, CURLOPT_CONNECTTIMEOUT, 10);
            curl_setopt($process, CURLOPT_TIMEOUT, 10);

            curl_setopt($process, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($process, CURLOPT_VERBOSE, 1);
            curl_setopt($process, CURLOPT_HEADER, 1);

            $return = curl_exec($process);

in result i see: 401... In apache2 log:

  • HTTP 1.0, assume close after body < HTTP/1.0 401 Unauthorized < Date: Fri, 15 Jul 2016 09:27:20 GMT < Server: Boa/0.94.13 < Connection: close < WWW-Authenticate: Basic realm="service" < Content-Type: text/html; charset=ISO-8859-1 < Set-Cookie: SESSIONID=068054d0; <
  • Closing connection 53
  • Issue another request to this URL: ''
  • Hostname was found in DNS cache
  • Trying 192.168.0.1...
  • Connected to 192.168.0.1 (192.168.0.1) port 80 (#54)
  • Server auth using Basic with user 'admin'

    GET / HTTP/1.0 Authorization: Basic my_code Host: 192.168.0.1 Accept: /

  • HTTP 1.0, assume close after body < HTTP/1.0 401 Unauthorized < Date: Fri, 15 Jul 2016 09:27:20 GMT < Server: Boa/0.94.13 < Connection: close

  • Authentication problem. Ignoring this. < WWW-Authenticate: Basic realm="service" < Content-Type: text/html; charset=ISO-8859-1 < Set-Cookie: SESSIONID=068054d0; <
  • Closing connection 54
1
Oh my god , huge mistake ))) "shore" => "sure") sry for my bad englishMelixion
SOLVED: curl_setopt($process, CURLOPT_COOKIEFILE, 'cookiefile.txt');Melixion

1 Answers

0
votes

That's not how you send Basic Auth using curl. For Basic, the expected header is Authorization: Basic <base64 encoded string> where the string encoded is `:. The way to do this OOTB with curl (assuming no other configurations) is with the "-u" switch. Example:

curl -u "user:pass" http://<endpoint>/

If you send the header "manually" you have to base64 encode it yourself first.

Now, your PHP code is correct and your curl call incorrect. This implies there is an issue with how the service is interpreting Authorization.