-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
97 lines (77 loc) · 2.87 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
function auth_check($realm) {
$_SESSION = array();
session_unset();
session_destroy();
session_start();
if(isset($_REQUEST['login']) && $_REQUEST['login'] != "" ||
isset($_REQUEST['password']) && $_REQUEST['password'] != "")
{
$userMngr = new User();
$user = $userMngr->load(array('login'=>$_REQUEST['login']));
if(!$user) {
$GLOBALS["authError"] = "Utilisateur inconnu";
return false;
}
if(sha1(KEY) == $_REQUEST['password']) {
return $user;
}
else {
$GLOBALS["authError"] = "Echec de l'authentification";
return false;
}
}
else
{
if(isset($_SERVER['PHP_AUTH_DIGEST'])) {
$digest = $_SERVER['PHP_AUTH_DIGEST'];
} else if(isset($_SERVER['HTTP_AUTHENTICATION']) &&
strpos(strtolower($_SERVER['HTTP_AUTHENTICATION']), "digest") == 0) {
$digest = substr($_SERVER['HTTP_AUTHORIZATION'], 7);
}
if(!isset($digest)) {
$GLOBALS["authError"] = "Digest vide";
return false;
}
// check digest string
$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,]+))@', $digest, $matches, PREG_SET_ORDER);
foreach ($matches as $m) {
$data[$m[1]] = $m[3] ? $m[3] : $m[4];
unset($needed_parts[$m[1]]);
}
// digest string is not valid
if(count($needed_parts) > 0) {
$GLOBALS["authError"] = "Digest invalide";
return false;
}
// user found ?
$userMngr = new User();
$user = $userMngr->load(array('login'=>$data['username']));
if(!$user) {
$GLOBALS["authError"] = "Utilisateur inconnu";
return false;
}
// check credentials
$A1 = md5($data['username'] . ':' . $realm . ':' . sha1(KEY));
$A2 = md5($_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$resp = md5($A1.':'.$data['nonce'].':'.$data['nc'].':'.$data['cnonce'].':'.$data['qop'].':'.$A2);
if($data['response'] != $resp) {
$GLOBALS["authError"] = "Echec de l'authentification";
return false;
}
}
return $user;
}
function auth_request($realm) {
header("WWW-Authenticate: Digest realm=\"$realm\",qop=\"auth\",nonce=\"".uniqid()."\",opaque=\"".session_id()."\"");
header("HTTP/1.0 401 Unauthorized");
echo "{\"error\":{\"id\":\"2\",\"message\":\"<h1>login failed</h1><p>".$GLOBALS["authError"]."</p>\"}}\n";
$_SESSION = array();
session_unset();
session_destroy();
die();
}
?>