My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes    
 
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<?php

$REVISIONS[__FILE__] = '$URL$ $Date$';

// Copyright 2008, nicerobot.org

// This file is part of DigestJ.
//
// DigestJ is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// DigestJ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with DigestJ. If not, see <http://www.gnu.org/licenses/>.

// http://tools.ietf.org/html/rfc2617

#$CONFIG['debugging'] = 1;

include_once('../config.php');
include('users.php');

if (isset($_REQUEST['check'])) {
header('HTTP/1.1 200 OK');
$user = get_user($_REQUEST['check']);
if (isset($user)) {
print $user['username'];
}
exit(0);
}

if (is_authenticated()) {
header('HTTP/1.1 200 OK');
header('Location: ..');
exit(0);
}

authenticate();

function authenticate()
{
global $CONFIG;

$error= '';

$data = array(
// Default the algorithm to the session's
'algorithm' => $_SESSION['algorithm'],
'HTTP_AUTHENTICATE' => $_SERVER['HTTP_AUTHENTICATE']
);

if (empty($data['HTTP_AUTHENTICATE'])) {
$data['HTTP_AUTHENTICATE'] = $_REQUEST['HTTP_AUTHENTICATE'];
}

$data['HTTP_AUTHENTICATE'] = stripslashes($data['HTTP_AUTHENTICATE']);

// Only proceed if there is an HTTP_AUTHENTICATE header
if (!empty($data['HTTP_AUTHENTICATE'])) {

// Parse out the name/value pairs
preg_match_all('@(\w+)=[\'"]?([^\'",]+)[\'"]?@', $data['HTTP_AUTHENTICATE'], $matches, PREG_SET_ORDER);

foreach ($matches as $m) {
$data[$m[1]] = $m[2];
}

// Look for the user.
$data['user'] = get_user($data['username']);

// Process to register if there is a register in the header and the user doesn't exists.
if ($data['register'] && empty($data['user']))
{
require_once('../3rdparty/recaptcha/recaptchalib.php');

if ($data["recaptcha_response"]) {
$data['captcha'] = recaptcha_check_answer($CONFIG['recaptcha']['private'],
$_SERVER["REMOTE_ADDR"],
$data["salt"],
$data["recaptcha_response"]);

if ($data['captcha']->is_valid) {
$data['user'] = array(
'username' => $data['username'],
'user' => 1,
'realm' => $data['realm'],
'salt' => $data['salt'],
'algorithm' => $data['algorithm'],
'hash' => $data['register']
);

if (save_user($data['user'])) {
$_SESSION['response'] = $R;
$_SESSION['authenticated'] = $data['username'];
header('HTTP/1.1 200 OK');
print "Ok";
dump_debugging_data($data);
exit(0);
}
$error = 'Can not write to database.';
}
}
}

// Process to verification if there is a response in the header and the user exists.
if ($data['response'] && $data['user'])
{

$data['A1'] = $data['user']['hash'];
$data['A2'] = hash($data['algorithm'], $_SERVER['REQUEST_METHOD'].':'.$data['uri']);
$data['R'] = hash($data['algorithm'],
$data['A1'].':'
.$data['nonce'].':'
.$data['nc'].':'
.$data['cnonce'].':'
.$data['qop'].':'
.$data['A2']);

if (!strcasecmp($data['response'],$data['R']))
{
save_auth($data['user']);
$_SESSION['response'] = $R;
$_SESSION['authenticated'] = $data['username'];
header('HTTP/1.1 200 OK');
print "Ok";
dump_debugging_data($data);
exit(0);
}
save_fail($data);

}

}

// There needs to be a persistent store to minitor login attempts over time.
// - If a single user has too many failures, they need to be given assistance.
// - If there are too main failures from a single IP in a short amount of time,
// they need to be warned. This can partially be dealt with using the session
// but certainly an attacker wouldn't be saving the session key.

// To support standard Digest, simply change this to Digest instead of DigestJ
$response = 'WWW-Authenticate: ' . $CONFIG['scheme']
.' realm="'.$_SESSION['realm'].'"'
.',nonce="'.$_SESSION['nonce'].'"'
.',opaque="'.hash($data['algorithm'],$_SESSION['realm']).'"'
.',algorithm="'.$data['algorithm'].'"'
.',qop="'.$_SESSION['qop'].'"'
;

//
if (!empty($data['user']['salt'])) {
# non-standard
$response .= ',salt="'.get_user_salt($data['username']).'"';
}

//
if (!empty($CONFIG['migrate'])) {
# non-standard
$response .= ',migrate="1"';
}

if (is_debugging()) {
$response .= ',debug="1"';
}

header('HTTP/1.1 401 Unauthorized');
header($response);

if (!empty($error))
{
print $error;
}
else if (isset($data['register']))
{
if (!empty($data['captcha']))
{
print 'Incorrect please try again';
//$data['captcha']->error
}
else
{
print "Invalid registration request.";
}
}
else if (isset($data['opaque']))
{
print "Your Username or Password is incorrect.";
}
else
{
print "Invalid request.";
}

dump_debugging_data($data);
exit(401);
}

function dump_debugging_data($data) {
global $CONFIG;

if (is_debugging()) {
print "\nA1= "
. $data['username'] . ':'
. $data['realm'] . ':'
. get_user_hash($data['username']);

if ($CONFIG['with_salt']) {
print ' salt=' . get_user_salt($data['username']);
}

print "\n\t{$data['A1']}\nA2= "
. $_SERVER['REQUEST_METHOD'] . ':' . $data['uri'] . "\n\t"
. $data['A2'] ."\nR= "
. $data['A1'] . ':'
. $data['nonce'] . ':'
. $data['nc'] . ':'
. $data['cnonce'] . ':'
. $data['qop'] . ':'
. $data['A2']. "\n\t"
. $data['R']."\n";

print_r($data);
print_r($CONFIG);
print_r($_SESSION);
//print_r($_SERVER);
//print_r($_REQUEST);
}
}
?>

Change log

r148 by nicerobot on Aug 11, 2008   Diff
more error checking. slight change to
recaptcha popup.
Go to: 
Project members, sign in to write a code review

Older revisions

r146 by nicerobot on Aug 11, 2008   Diff
added constraint checking
r143 by nicerobot on Aug 11, 2008   Diff
added salt
r138 by nicerobot on Aug 11, 2008   Diff
recaptcha support.
All revisions of this file

File info

Size: 6473 bytes, 235 lines

File properties

svn:copyright
Copyright 2008, nicerobot.org
svn:keywords
URL Date
Powered by Google Project Hosting