Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
HTTP Digest authentication with encrypted passwords
Old 04-14-2009, 01:43 PM HTTP Digest authentication with encrypted passwords
Junior Talker

Posts: 1
Name: Mark Johnson
Trades: 0
Hi everyone, I'm currently trying to implement HTTP Digest authentication to a script. I've had a thorough read of the documentation as well as a few examples, but all of then have the same problem in that they rely on the passwords being stored as plain text, a practice I've never been keen on.

The way I've tried to get around it is instead of storing a hash of the password, storing the a1 hash in the database (i.e. md5($username.":".$realm.":".$password); ). I've got this working OK for authentication through a form, but not through HTTP. Here's the class I've written:
PHP Code:
class auth_digest {
    
    public 
$username;
    public 
$nonce;
    public 
$uri;
    public 
$response;
    public 
$qop;
    public 
$nc;
    public 
$cnonce;
    
    public function 
__construct($digest) {
        if(
preg_match('/username="([^"]+)"/'$digest$username
        && 
preg_match('/nonce="([^"]+)"/'$digest$nonce)
        && 
preg_match('/uri="([^"]+)"/'$digest$uri)
        && 
preg_match('/response="([^"]+)"/'$digest$response)
        && 
preg_match('/qop="?([^,\s"]+)/'$digest$qop
        && 
preg_match('/nc=([^,\s"]+)/'$digest$nc
        && 
preg_match('/cnonce="([^"]+)"/'$digest$cnonce)) {
            
$this->username $username[1];
            
$this->nonce $nonce[1];
            
$this->uri $uri[1];
            
$this->response $response[1];
            
$this->qop $qop[1];
            
$this->nc $nc[1];
            
$this->cnonce $nonce[1];
            return 
true;
        } else {
            return 
false;
        }
    }
    
    public function 
authenticate($hash) {
        
$A1 $hash;
        
$A2 md5($_SERVER['REQUEST_METHOD'].':'.$this->uri);
        
$valid_response md5($A1.':'.$this->nonce.':'.$this->nc.':'.$this->cnonce.':'.$this->qop.':'.$A2);
        if (
$valid_response ==  $this->response) {
            return 
true;
        } else {
            return 
false;
        }
    }
}; 
The idea is that you pass $_SERVER['PHP_AUTH_DIGEST'] to the constructor which pulls out all the relevant data and stores it in the properties. This seems to work fine. You then get the user's hash from the database and pass it into the authenticate() method, which hashes it and compares it to the digest sent with the request. However, for some reason, the digest generated by the script isn't matching that sent with the request. Can anyone see something I'm doing wrong?

Last edited by MarxJ; 04-14-2009 at 02:02 PM..
MarxJ is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Reply     « Reply to HTTP Digest authentication with encrypted passwords
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.08041 seconds with 12 queries