Re: Autenticación HTTP tipo Digest con PHP (¡urgente!)

From: Date: Mon, 02 Dec 2002 15:39:58 +0000
Subject: Re: Autenticación HTTP tipo Digest con PHP (¡urgente!)
References: 1 2  Groups: php.general.es 
Request: Send a blank email to general-es+get-29895@lists.php.net to get a copy of this message
Dilluns Desembre 2 2002 15:50, Luis Lorente dixit: > Muy buenas > > Esta pregunta la hice ya hace tiempo pero no hubo respuesta, la vuelvo a > hacer porque tengo que implementar esta tema con urgencia. > > ¿Alguien sabe como implementar autenticación HTTP Digest con PHP? > ¿O de algún recurso en internet que lo explique? Tutoriales, artículos, > ejemplos, etc. > > He mirado el RFC correspondiente pero es demasiado complicado, al menos con > el tiempo de que dispongo. En la página del m*n**l referente a autenticación HTTP habia hace tiempo (enero de 2001) un comentario de alguien llamado Chris Minnoy explicando cómo llevar a cabo autenticación HTTP digest desde PHP. Ignoro que ha ocurrido con el comentario, pero lo cierto es que actualmente ya no se encuentra en esa página. La buena noticia es que yo tengo una copia guardada, ahí va: ===================== aquí empieza =========================== chris.minnoy at minds.be 09-Jan-2001 03:34 If you want to use HTTP Digest Authentication, these functions could help (PHP4): function getHTTPheaders() { //* get all HTTP headers $headers = getallheaders(); //* set all header tags to lowercase while (list($header, $value) = each($headers)) { $header = strtolower($header); $headers2[$header] = $value; } return $headers2; } // function function analyseHeaders(&$headers) { //* filter authorization information $authorization_param = $headers['authorization']; if ($authorization_param != '') { $pos = strpos($authorization_param, ' '); $delivered_auth_type = strtolower(substr($authorization_param, 0, $pos)); $authorization_params = substr($authorization_param, $pos+1); //* analyse parameters if ($delivered_auth_type == 'basic') { list($loginname, $password) = explode(':', base64_decode($authorization_params)); $params['authorization_type'] = 'basic'; $params['username'] = $loginname; $params['password'] = $password; } else if ($delivered_auth_type == 'digest') { $params['authorization_type'] = 'digest'; $digest_params_tmp = explode(',', $authorization_params); while ($digest_param = each($digest_params_tmp)) { $tmp = trim($digest_param[1]); list($tag, $value) = explode('=', $tmp); if ($value[0] == '"') $value = substr($value, 1); $len = strlen($value); if ($value[$len-1] == '"') $value = substr($value, 0, $len-1); $params[strtolower($tag)] = $value; } } } } function requestDigestAuthentication($realm, $domain, $nonce, $opaque) { header('WWW-Authenticate: Digest '. "realm=\"$realm\", ". "domain=\"$domain\", ". "nonce=\"$nonce\", ". "opaque=\"$opaque\", ". 'qop="auth,auth-int"'); } // function ... $db_password = 'localpasswordoutofdatabase'; // is plain text, but other system would be safer $our_response = md5( md5("$username:$realm:$db_password"). ":$db_nonce:$nc:$cnonce:$qop:". md5("{$HTTP_SERVER_VARS['REQUEST_METHOD']}:{$HTTP_SERVER_VARS['REQUEST_URI']}")); /* Note: see ftp://ftp.isi.edu/in-notes/rfc2617.txt for more info Note: This code is just a sample, not a full working program !!! */ abc@def.com 20-Jan-2001 08:38 I am astonished to see that some of the posters here are using user-originated userid and password to construct sql queries without first escaping special characters, most notably apostrophe. This page should be cleansed of this security hole, before more people cut-and-paste it. iver@musite.com 07-Feb-2001 10:20 HTTP authentication does not work on IIS using the CGI even when using the method in the note by brecht@sanders.org; A bug in IIS causes it to drop all interesting headers (Www-authenticate and so forth) when a CGI produces a 401, replacing them and any output with a really unhelpful message. (see also Q176113 in the MSDN knowledgebase - it causes other problems). However, there is a possible workaround for authenticating using CGI: Use a separate file named 'nph-login.php' and a pair of functions authenticate_user() and ensure_authenticated() stashed in a library. When a script's name starts with "nph-" IIS will leave its headers alone, which means that the script has to provide all of them, but also that IIS won't mess with them. When the user needs to authenticate, ensure_authenticated() will redirect him/her to nph-login.php; when a correct username/password has been entered, this file redirects the user right back. --- begin library functions -- // Authenticate user is as in the referenced note: retrieve and // parse auth info, checking against database function authenticate_user() { // authenticate the user for a webserver realm using basic authentication global $HTTP_AUTHORIZATION, $PHP_AUTH_USER, $PHP_AUTH_PW; $authenticated = 0; if ($PHP_AUTH_USER == "" && $PHP_AUTH_PW == "" && ereg("^Basic ", $HTTP_AUTHORIZATION)) { list($PHP_AUTH_USER, $PHP_AUTH_PW) = explode(":", base64_decode(substr($HTTP_AUTHORIZATION, 6))); } if ($PHP_AUTH_USER != "" || $PHP_AUTH_PW != "") { //put your authentication here (e.g. database lookup) if ($PHP_AUTH_USER == "test" && $PHP_AUTH_PW == "123") { $authenticated = 1; } } return $authenticated; } // ensure_authenticated uses authenticate_user() and // redirects to the nph-script when that fails: function ensure_authenticated() { global $PHP_SELF; if (authenticate_user()) { return 1; } else { header("Location: nph-login.php?REQUESTED=$PHP_SELF"); exit; } } --- end library functions -- --- BEGIN FILE nph-login.php --- if (authenticate_user()) { global $REQUESTED; // get page to display from URL if (!isset($REQUESTED)) { // don't know what page wants to authenticate - // add a sane default or just exit $REQUESTED = "/"; } header("HTTP/1.0 301 Moved Permanently"); header("Location: $REQUESTED"); exit; } else { header("HTTP/1.0 401 Unauthorized"); header("WWW-Authenticate: Basic realm=\"Protected\""); header("Status: 401 Unauthorized"); header("Content-type: text/html"); } ?> <html> <head> <title>Can't log in</title> </head> <body> You have to supply a username and password. </body> </html> --- END FILE nph-login.php --- ===================== y aquí termina =========================== También te puede servir esta dirección (Por si no lo has mirado ya): http://www.google.com/search?q=php+digest+authentication Saludos Àngel

« previous php.general.es (#29895) next »