|
|
|
| Фсем ПрЕвЕдТ! Пока я мучался над logout в digest auth у меня всё перестало работать: значения не совпадают. Что у меня не так?
====== security.php ======
<?php function authorize($realm,$file,$userlist,$target){
require 'inc/ip.php';
$nonce = ip2long(getip()).time();
$headers = apache_request_headers();
$auth_success = false;
$stale = "false";
if (isset($headers['Authorization'])){
$authorization = $headers['Authorization'];
preg_match_all('/(,|\s|^)(\w+)=("([^"]*)"|([\w\d]*))(,|$)/',$authorization,$matches,PREG_SET_ORDER);
for ($i = 0; $i < count($matches); $i++){
$match = $matches[$i];
$auth_params[$match[2]] = $match[4] . $match[5];
}
if (!isset($userlist[$auth_params['username']])) {
header("$_SERVER[SERVER_PROTOCOL] 401 Not Authorized\r\n");
header("WWW-Authenticate: Digest realm=\"$realm\", nonce=\"$nonce\" stale=\"$stale\"\r\n");
$arr = unserialize(gzinflate(file_get_contents($file)));
$arr[$nonce] = array(ip2long(getip()),time());
unset($file);
file_put_contents($file,gzdeflate(serialize($arr)));
die;
}
$a1 = $auth_params['username'].':'.$auth_params['realm'].':'.$userlist[$auth_params['username']];
$a2 = $_SERVER['REQUEST_METHOD'] . ':' . $_SERVER['REQUEST_URI'];
$resp = md5(md5($a1) . ':' . $auth_params['nonce'] . ':' . md5($a2));
if ($resp == $auth_params['response']){
$arr = unserialize(gzinflate(file_get_contents($file)));
if (long2ip($arr[$auth_params['nonce']][0]) == $_SERVER['REMOTE_ADDR']){
unset($arr[$auth_params['nonce']]);
file_put_contents($file,gzdeflate(serialize($arr)));
$auth_success = true; // Аутентификация пройдена
} else {
$stale = "true"; die;
}
}
}
if ($auth_success){
header("Location: $target[0]\r\n");
$_SESSION['AUTH'] = true;
die;
} else {
header("$_SERVER[SERVER_PROTOCOL] 401 Not Authorized\r\n");
header("WWW-Authenticate: Digest realm=\"$realm\", nonce=\"$nonce\" stale=\"$stale\" qop=\"auth\"\r\n");
$arr = unserialize(gzinflate(file_get_contents($file)));
$arr[$nonce] = array(ip2long(getip()),time());
unset($file);
file_put_contents($file,gzdeflate(serialize($arr)));
die;
}
} ?>
|
Для кого функция getip была странной, вот её листинг:
<?php function getip(){
$ip = getenv('HTTP_X_FORWARDED_FOR');
if (!$ip || ($ip == 'undefined') || empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];
return $ip;
} ?>
|
А массив $username такого вида:
Login => Password, user => pass
|
Спасибо, что прочитали! Жду ответов )) | |
|
|
|
|
|
|
|
для: ~AquaZ~
(22.11.2009 в 21:54)
| | Вот, я помучался, и всё вышло! Кому интересно, рабочая функция безопасной аутентификации.
название защищённой области, например 'Secure zone'.
файл для мусора (чистится сам), например, 'adds/auth.tmp'
список пользователей вида
<? array('login' => 'pass', 'vasya' => 'qwerty') ?>
|
ставим в начале каждой защищённой страницы
<?
require_once "security.php";
authorize('Secure zone', 'adds/auth.tmp', array('login' => 'pass', 'vasya' => 'qwerty') );
?>
|
====== security.php ======
<?php
function authorize($realm,$file,$userlist){
$nonce = md5(microtime().getip(),true);
$headers = apache_request_headers();
$auth_success = false;
$stale = 'false';
if (isset($headers['Authorization'])){
$authorization = $headers['Authorization'];
preg_match_all('#(,|\s|^)(\w+)=("([^"]*)"|([\w\d]*))(,|$)#', $authorization, $matches, PREG_SET_ORDER);
$auth_params = Array();
for ($i = 0; $i < count($matches); $i++){
$match = $matches[$i];
$auth_params[$match[2]] = $match[4] . $match[5];
}
if (isset($userlist[$auth_params['username']])){
$a1 = $auth_params['username'].':'.$auth_params['realm'].':'.$userlist[$auth_params['username']];
$a2 = $_SERVER['REQUEST_METHOD'] . ':' . $_SERVER['REQUEST_URI'];
$resp = md5(md5($a1) . ':' . $auth_params['nonce'] . ':' . md5($a2));
if ($resp == $auth_params['response']){
$f = unserialize(gzinflate(file_get_contents($file)));
if ($f[$auth_params['nonce']][0] == ip2long(getip())){
unlink($f[$auth_params['nonce']]);
file_put_contents($file,gzdeflate(serialize($f)));
}
$auth_success = true;
} else $stale = "true";
}
}
if (!$auth_success) {
header("$_SERVER[SERVER_PROTOCOL] 401 Not Authorized\r\n");
header('WWW-Authenticate: Digest realm="'.$realm.'", nonce="'.$nonce.'", stale="'.$stale.'" qop="auth,auth-int"'."\r\n");
$arr = unserialize(gzinflate(file_get_contents($file)));
$arr[$nonce] = array(ip2long(getip()),time());
unset($file);
file_put_contents($file,gzdeflate(serialize($arr)));
}
Mycop_Hax($file);
}
##############################################################
#### == Simple IP getter == ####
#### Made by: ~AquaZ~ ####
##############################################################
function getip(){
$ip = getenv('HTTP_X_FORWARDED_FOR');
if (!$ip || ($ip == 'undefined') || empty($ip)) $ip = $_SERVER['REMOTE_ADDR'];
return $ip;
}
##############################################################
#### == Digest Authentication rubbish killer == ####
#### Made by: ~AquaZ~ ####
##############################################################
function Mycop_Hax($file,$maxlive = 1800){
$f = unserialize(gzinflate(file_get_contents($file)));
if (is_array($f)){
foreach($f as $key => $val){
if ($val[1] < time() - $maxlive) unset($f[$key]);
}
unlink($file);
file_put_contents($file,gzdeflate(serialize($f)));
}
}?>
|
| |
|
|
|