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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[249] commit implement forgot_password patch from Maarten Vanraes</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd>249</dd>
<dt>Author</dt> <dd>rda</dd>
<dt>Date</dt> <dd>2011-01-08 00:20:14 +0100 (Sat, 08 Jan 2011)</dd>
</dl>
<h3>Log Message</h3>
<pre>commit implement forgot_password patch from Maarten Vanraes</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#identityCatDaptrunkcatdapyml">identity/CatDap/trunk/catdap.yml</a></li>
<li><a href="#identityCatDaptrunkrootindextt">identity/CatDap/trunk/root/index.tt</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#identityCatDaptrunklibCatDapControllerforgot_passwordpm">identity/CatDap/trunk/lib/CatDap/Controller/forgot_password.pm</a></li>
<li><a href="#identityCatDaptrunkrootemailforgot_passwordtt">identity/CatDap/trunk/root/email/forgot_password.tt</a></li>
<li>identity/CatDap/trunk/root/forgot_password/</li>
<li><a href="#identityCatDaptrunkrootforgot_passwordchecktt">identity/CatDap/trunk/root/forgot_password/check.tt</a></li>
<li><a href="#identityCatDaptrunkrootforgot_passwordcompletett">identity/CatDap/trunk/root/forgot_password/complete.tt</a></li>
<li><a href="#identityCatDaptrunkrootforgot_passwordconfirmtt">identity/CatDap/trunk/root/forgot_password/confirm.tt</a></li>
<li><a href="#identityCatDaptrunkrootforgot_passwordindextt">identity/CatDap/trunk/root/forgot_password/index.tt</a></li>
</ul>
</div>
<div id="patch"><pre>
<a id="identityCatDaptrunkcatdapyml">Modified: identity/CatDap/trunk/catdap.yml</a>
===================================================================
--- identity/CatDap/trunk/catdap.yml 2011-01-07 23:15:42 UTC (rev 248)
+++ identity/CatDap/trunk/catdap.yml 2011-01-07 23:20:14 UTC (rev 249)
@@ -35,6 +35,12 @@
login_blacklist:
- apache
+forgot_password:
+ secret:
+ path: '/tmp/'
+ prefix: 'catdap-forgot_password-'
+ timeout: 259200
+
authentication:
default_realm: ldap
realms:
@@ -55,6 +61,7 @@
user_filter: '(&(objectClass=inetOrgPerson)(uid=%s))'
user_scope: 'one'
user_field: 'uid'
+ email_filter: '(&(objectClass=inetOrgPerson)(|(mail=%s)(mailAlternateAddress=%s)))'
use_roles: 1
role_basedn: 'dc=mageia,dc=org'
role_scope: 'sub'
<a id="identityCatDaptrunklibCatDapControllerforgot_passwordpm">Added: identity/CatDap/trunk/lib/CatDap/Controller/forgot_password.pm</a>
===================================================================
--- identity/CatDap/trunk/lib/CatDap/Controller/forgot_password.pm (rev 0)
+++ identity/CatDap/trunk/lib/CatDap/Controller/forgot_password.pm 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,270 @@
+package CatDap::Controller::forgot_password;
+use Moose;
+use namespace::autoclean;
+use Email::Valid;
+use Data::UUID;
+
+BEGIN {extends 'Catalyst::Controller'; }
+
+=head1 NAME
+
+CatDap::Controller::forgot_password - Catalyst Controller
+
+=head1 DESCRIPTION
+
+Catalyst Controller.
+
+=head1 METHODS
+
+=cut
+
+
+=head2 index
+
+=cut
+
+sub index :Path :Args(0) : Form {
+ my ( $self, $c ) = @_;
+
+ if (defined $c->user) {
+ # if we're logged in, we haven't forgotten the password
+ $c->log->debug('Redirecting to /user');
+ $c->res->redirect('/user');
+ }
+}
+
+sub check : Local {
+ my ( $self, $c ) = @_;
+
+ my %details = %{$c->request->params};
+ my $username = lc($c->request->params->{uid});
+ my @errors;
+ $c->stash(errors => []);
+ my $email = $c->request->params->{mail};
+ if (! Email::Valid->address($email)) {
+ push @errors, $c->loc('Invalid email address');
+ }
+
+ if (@errors) {
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+
+ # check in LDAP now that we have validated username and email
+ my $emailfilter = $c->config->{'authentication'}{'realms'}{'ldap'}{'store'}{'email_filter'};
+ $emailfilter =~ s/\%s/$email/g,
+ $c->log->debug("Searching for email $email with filter $emailfilter");
+ my $mesg = $c->model('Proxy')->search($emailfilter);
+ if (!$mesg->entries()) {
+ push @errors,$c->loc(
+ 'This email address is not bound to an account'
+ );
+ }
+
+ if (@errors) {
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+
+ if ($mesg->code) {
+ push @errors,$mesg->error;
+ $c->log->info( sprintf("finding email $email failed: %s", $mesg->error) );
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'register/index.tt');
+ return;
+ }
+
+ my $secret = gen_secret($c, $email);
+
+ $c->stash(
+ email => {
+ 'to' => $email,
+ 'from' => ${$c->config}{'emailfrom'},
+ 'subject' => ${$c->config}{'apptitle'} . " - " . $c->loc('Forgot password'),
+ 'template' => 'forgot_password.tt',
+ },
+ url => $c->uri_for('/forgot_password/confirm') . "?secret=$secret",
+ );
+
+ $c->log->info("Sending forgot password mail to email address $email");
+ $c->forward( $c->view('Email::Template') );
+ if ( @{ $c->error } ) {
+ my $errors = join "\n",@{ $c->error };
+ $c->log->info("Sending activation mail to $email failed: $errors");
+ $c->response->body($c->loc('An error occured sending the email, please try again later. Errors [_1]', $errors));
+ $c->error(0); # Reset the error condition if you need to
+ }
+ $c->stash(template => 'forgot_password/complete.tt');
+}
+
+sub confirm : Local {
+ my ($self, $c) = @_;
+ my $secret = $c->req->param('secret');
+ my @errors;
+
+ # show confirm page which can enter new password
+ if (defined $c->user) {
+ # if we're logged in, we haven't forgotten the password
+ $c->log->debug('Redirecting to /user');
+ $c->res->redirect('/user');
+ }
+
+ # find secret
+ my $email = find_secret($c, $secret);
+ if (!$email) {
+ push @errors, "Secret has expired, please try again.";
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+ my $mesg = find_user_email($c, $email);
+ if ($mesg->code) {
+ push @errors, "Secret has expired, please try again.";
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+
+ # show template to enter a new password
+ $c->stash(secret => $secret, template => 'forgot_password/confirm.tt');
+}
+
+sub change_password : Local {
+ my ($self, $c) = @_;
+ my @errors = ();
+ my $secret = $c->req->param('secret');
+ my $newpass;
+
+ # find secret
+ my $email = find_secret($c, $secret);
+ if (!$email) {
+ push @errors, "Secret has expired, please try again.";
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+ my $mesg = find_user_email($c, $email);
+ if ( $mesg->code) {
+ push @errors, "Secret has expired, please try again.";
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/index.tt');
+ return;
+ }
+ my $entry = $mesg->entry;
+
+ # check if both passwords are equal and are confirm the validation norms
+ if ($c->req->param('newpassword1') eq $c->req->param('newpassword2')) {
+ $newpass = $c->req->param('newpassword1');
+ } else {
+ push @errors, "New passwords dont match";
+ }
+ # if error show confirm page again to retry
+ if (@errors) {
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/confirm.tt');
+ return;
+ }
+
+ # change password
+ my $pp = Net::LDAP::Control::PasswordPolicy->new;
+ $mesg = $c->model('Proxy')->set_password(
+ user => $entry->dn,
+ newpasswd => $newpass,
+ control => [ $pp ],
+ );
+ if ($mesg->code) {
+ my $perror = $mesg->error;
+ push @errors, "Password change failed: $perror";
+ $c->detach;
+ }
+
+ # if error show confirm page again to retry
+ if (@errors) {
+ $c->stash(errors => \@errors);
+ $c->stash(template => 'forgot_password/confirm.tt');
+ return;
+ }
+
+ # TODO: log in by setting the $c->user
+
+ # remove the stored secret
+ remove_secret($c, $secret);
+
+ # redirect to /
+ $c->log->debug('Redirecting to /');
+ $c->res->redirect('/');
+}
+
+sub gen_secret {
+ my ($c, $email) = @_;
+ my $ug = new Data::UUID;
+ # generate a unique secret
+ my $secret = $ug->create_str();
+ my $filename = $c->config->{'forgot_password'}{'secret'}{'path'} .'/'. $c->config->{'forgot_password'}{'secret'}{'prefix'} . $secret;
+ # store secret with email
+ open FILE, ">$filename";
+ print FILE $email;
+ close FILE;
+ return $secret;
+}
+
+sub find_secret {
+ my ($c, $secret) = @_;
+ my $email;
+ my $filename = $c->config->{'forgot_password'}{'secret'}{'path'} .'/'. $c->config->{'forgot_password'}{'secret'}{'prefix'} . $secret;
+ my $timeout = 259200; # 3days in seconds
+ if ($c->config->{'forgot_password'}{'secret'}{'timeout'}) {
+ $timeout = $c->config->{'forgot_password'}{'secret'}{'timeout'};
+ }
+
+ # find secret
+ if (!$secret || !open(FILE, "<$filename")) {
+ # if secret is wrong, timeout expired?
+ return '';
+ }
+ read(FILE, $email, 255);
+ close FILE;
+
+ # check the time, and see if it's longer than timeout
+ my @s = stat($filename);
+ if (time() > $s[9] + $timeout) {
+ # expired
+ return '';
+ }
+
+ return $email;
+}
+
+sub remove_secret {
+ my ($c, $secret) = @_;
+ my $filename = $c->config->{'forgot_password'}{'secret'}{'path'} .'/'. $c->config->{'forgot_password'}{'secret'}{'prefix'} . $secret;
+ unlink $filename;
+}
+
+sub find_user_email {
+ my ($c, $email) = @_;
+
+ # find user by email;
+ my $emailfilter = $c->config->{'authentication'}{'realms'}{'ldap'}{'store'}{'email_filter'};
+ $emailfilter =~ s/\%s/$email/g,
+ $c->log->debug("Searching for email $email with filter $emailfilter");
+ return $c->model('Proxy')->search($emailfilter);
+}
+
+
+=head1 AUTHOR
+
+Buchan Milne
+
+=head1 LICENSE
+
+This library is free software. You can redistribute it and/or modify
+it under the same terms as Perl itself.
+
+=cut
+
+__PACKAGE__->meta->make_immutable;
+
+1;
<a id="identityCatDaptrunkrootemailforgot_passwordtt">Added: identity/CatDap/trunk/root/email/forgot_password.tt</a>
===================================================================
--- identity/CatDap/trunk/root/email/forgot_password.tt (rev 0)
+++ identity/CatDap/trunk/root/email/forgot_password.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,7 @@
+[% l('Dear [_1],',cn) %]
+[% l('Your [_1] account has been requested to change the password. If you did not do this, or you do not want to change your password; you can just do nothing.',c.config.organisation) %]
+[% l('To reset your password, please follow the link below.') %]
+[% url %]
+
+--
+http://mageia.org/
<a id="identityCatDaptrunkrootforgot_passwordchecktt">Added: identity/CatDap/trunk/root/forgot_password/check.tt</a>
===================================================================
--- identity/CatDap/trunk/root/forgot_password/check.tt (rev 0)
+++ identity/CatDap/trunk/root/forgot_password/check.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,4 @@
+<h2>Success</h2>
+<p>
+[% message %]
+</p>
\ No newline at end of file
<a id="identityCatDaptrunkrootforgot_passwordcompletett">Added: identity/CatDap/trunk/root/forgot_password/complete.tt</a>
===================================================================
--- identity/CatDap/trunk/root/forgot_password/complete.tt (rev 0)
+++ identity/CatDap/trunk/root/forgot_password/complete.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,6 @@
+<h2>[% l('Email sent.') %]</h2>
+
+<p>
+ [% l('Operation was successful.') %]
+ [% l('Check your mail for password reset instructions.') %]
+</p>
<a id="identityCatDaptrunkrootforgot_passwordconfirmtt">Added: identity/CatDap/trunk/root/forgot_password/confirm.tt</a>
===================================================================
--- identity/CatDap/trunk/root/forgot_password/confirm.tt (rev 0)
+++ identity/CatDap/trunk/root/forgot_password/confirm.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,21 @@
+
+<h1>[% l('Enter new password.') %]</h1>
+
+<div id="input_form">
+<form method="post" action="/forgot_password/change_password">
+ <table>
+ <tr>
+ <td><label for="newpassword1_">[% l('New Password') %]</label></td>
+ <td><input id="newpassword1_" name="newpassword1" type="password" value="" /></td>
+ </tr><tr>
+ <td><label for="newpassword2_">[% l('Repeat New Password') %]</label></td>
+ <td><input id="newpassword2_" name="newpassword2" type="password" value="" /></td>
+ </tr><tr>
+ <td colspan="2" style="text-align:center;">
+ <input type="hidden" name="secret" value="[% secret %]" />
+ <button type="submit" value="[% l('Set new password') %]">[% l('Set new password') %]</button>
+ </td>
+ </tr>
+ </table>
+</form>
+</div>
<a id="identityCatDaptrunkrootforgot_passwordindextt">Added: identity/CatDap/trunk/root/forgot_password/index.tt</a>
===================================================================
--- identity/CatDap/trunk/root/forgot_password/index.tt (rev 0)
+++ identity/CatDap/trunk/root/forgot_password/index.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -0,0 +1,17 @@
+
+<h1>[% l('Forgot your password?') %]</h1>
+
+<div id="input_form">
+<form method="post" action="/forgot_password/check">
+ <table>
+ <tr>
+ <td><label for="mail_">[% l('Email address') %]</label></td>
+ <td><input id="mail_" type="text" name="mail" value="[% c.request.params.mail %]" /></td>
+ </tr><tr>
+ <td colspan="2" style="text-align:center;">
+ <button type="submit" value="[% l('Reset password') %]">[% l('Reset password') %]</button>
+ </td>
+ </tr>
+ </table>
+</form>
+</div>
<a id="identityCatDaptrunkrootindextt">Modified: identity/CatDap/trunk/root/index.tt</a>
===================================================================
--- identity/CatDap/trunk/root/index.tt 2011-01-07 23:15:42 UTC (rev 248)
+++ identity/CatDap/trunk/root/index.tt 2011-01-07 23:20:14 UTC (rev 249)
@@ -14,8 +14,7 @@
</div>
<div id="login_form_line">
<span><a href="/register">[% l('Register') %]</a> |
- @todo [% l('Forgotten password?') %]
- <!--<a href="/forgot_password">[% l('Forgotten password?') %]</a> -->
+ <a href="/forgot_password">[% l('Forgotten password?') %]</a>
</span>
</div>
</form>
</pre></div>
</body>
</html>
|