Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

PHP_AUTH_DIGEST not working ?

633 views
Skip to first unread message

Asger Joergensen

unread,
Mar 27, 2012, 4:51:56 AM3/27/12
to
Hi

Anybody know why the example below from the php manual doesn't work ?
it brings up a password dialog, but instead of logging in it
shows the password dialog again and again.

I know this is normally done using .htaccess, but my provider only
support authtype Basic, so I thought I would try the php way.

Any advice ?

Thanks in advance
Best regards
Asger-P
http://Asger-P.dk/software



<?php
$realm = 'Restricted area';

//user => password
$users = array('admin' => 'mypass', 'guest' => 'guest');


if (empty($_SERVER['PHP_AUTH_DIGEST'])) {
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm.
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');

die('Text to send if user hits Cancel button');
}


// analyze the PHP_AUTH_DIGEST variable
if (!($data = http_digest_parse($_SERVER['PHP_AUTH_DIGEST'])) ||
!isset($users[$data['username']]))
die('Wrong Credentials!');


// generate the valid response
$A1 = md5($data['username'] . ':' . $realm . ':' . $users[$data['username']]);
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$valid_response = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);

if ($data['response'] != $valid_response)
die('Wrong Credentials!');

// ok, valid username & password
echo 'You are logged in as: ' . $data['username'];


// function to parse the http auth header
function http_digest_parse($txt)
{
// protect against missing data
$needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
$data = array();
$keys = implode('|', array_keys($needed_parts));

preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);

foreach ($matches as $m) {
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}

return $needed_parts ? false : $data;
}
?>

Jerry Stuckle

unread,
Mar 27, 2012, 8:10:38 AM3/27/12
to
> // ok, valid username& password
> echo 'You are logged in as: ' . $data['username'];
>
>
> // function to parse the http auth header
> function http_digest_parse($txt)
> {
> // protect against missing data
> $needed_parts = array('nonce'=>1, 'nc'=>1, 'cnonce'=>1, 'qop'=>1, 'username'=>1, 'uri'=>1, 'response'=>1);
> $data = array();
> $keys = implode('|', array_keys($needed_parts));
>
> preg_match_all('@(' . $keys . ')=(?:([\'"])([^\2]+?)\2|([^\s,]+))@', $txt, $matches, PREG_SET_ORDER);
>
> foreach ($matches as $m) {
> $data[$m[1]] = $m[3] ? $m[3] : $m[4];
> unset($needed_parts[$m[1]]);
> }
>
> return $needed_parts ? false : $data;
> }
> ?>

Is your browser sending an AUTH_DIGEST header?

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstu...@attglobal.net
==================

Asger Joergensen

unread,
Mar 27, 2012, 8:51:48 AM3/27/12
to
Hi Jerry

Jerry Stuckle wrote:

> Is your browser sending an AUTH_DIGEST header?

I don't know, how can I check that, I'm not on the site yet so
I cant see anything that I echo.

but I do know that it never leaves the first if:
if( empty( $_SERVER['PHP_AUTH_DIGEST'] ) )
{
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Digest realm="'.$realm.
'",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');

die('Text to send if user hits Cancel button');
}

because after 3 attempts I get rejected and this message is shown
'Text to send if user hits Cancel button'

p.s. it's the same in Chrome, IE and Opera.

Thanks for Your answer.

Jerry Stuckle

unread,
Mar 27, 2012, 10:57:44 AM3/27/12
to
On 3/27/2012 8:51 AM, Asger Joergensen wrote:
> Hi Jerry
>
> Jerry Stuckle wrote:
>
>> Is your browser sending an AUTH_DIGEST header?
>
> I don't know, how can I check that, I'm not on the site yet so
> I cant see anything that I echo.
>
> but I do know that it never leaves the first if:
> if( empty( $_SERVER['PHP_AUTH_DIGEST'] ) )
> {
> header('HTTP/1.1 401 Unauthorized');
> header('WWW-Authenticate: Digest realm="'.$realm.
> '",qop="auth",nonce="'.uniqid().'",opaque="'.md5($realm).'"');
>
> die('Text to send if user hits Cancel button');
> }
>
> because after 3 attempts I get rejected and this message is shown
> 'Text to send if user hits Cancel button'
>
> p.s. it's the same in Chrome, IE and Opera.
>

Well, if you don't know if the client is sending a digest authorization
header, then you can't go very far. Try printing out some values - like
$_SERVER['PHP_AUTH_DIGEST']. Better yet, get the Live HTTP Headers
plugin for Firefox and see what's being received and sent.

I suspect since your hosting company doesn't support digest
authentication, the client never sends this type of header. But I don't
know for sure; all my systems support digest authentication.

Maybe it's time to get a new host. Digest authentication is pretty
basic to Apache.
0 new messages