302
|
302
|
|
code via email.
|
303
|
303
|
|
"""
|
304
|
304
|
|
|
305
|
|
- |
username = request.forms.getunicode ('username')
|
306
|
|
- |
email = request.forms.getunicode ('email')
|
|
305
|
+ |
username = request.forms.getunicode('username')
|
|
306
|
+ |
email = request.forms.getunicode('email')
|
307
|
307
|
|
|
308
|
308
|
|
if not username or not email:
|
309
|
|
- |
redirect (application.get_url ('change_password'))
|
|
309
|
+ |
redirect(application.get_url('change_password'))
|
310
|
310
|
|
|
311
|
|
- |
user = database.get_user_by_username (username)
|
|
311
|
+ |
user = database.get_user_by_username(username)
|
312
|
312
|
|
|
313
|
313
|
|
if not user:
|
314
|
|
- |
redirect (application.get_url ('change_password'))
|
|
314
|
+ |
redirect(application.get_url('change_password'))
|
315
|
315
|
|
|
316
|
316
|
|
# Make sure the given email matches the one that we have in the database
|
317
|
317
|
|
if user['email'] != email:
|
318
|
|
- |
redirect (application.get_url ('change_password'))
|
|
318
|
+ |
redirect(application.get_url('change_password'))
|
319
|
319
|
|
|
320
|
320
|
|
# Is there another valid token already (from a previous request)?
|
321
|
321
|
|
# If yes, do not send another one (to prevent multiple requests or spam)
|
322
|
|
- |
if database.is_password_reset_token_valid (user['id']):
|
323
|
|
- |
redirect (application.get_url ('change_password'))
|
|
322
|
+ |
if database.is_password_reset_token_valid(user['id']):
|
|
323
|
+ |
redirect(application.get_url('change_password'))
|
324
|
324
|
|
|
325
|
325
|
|
# Generate secret token to send via email
|
326
|
|
- |
secret_token = random.ascii_string (32)
|
|
326
|
+ |
secret_token = random.ascii_string(32)
|
327
|
327
|
|
|
328
|
328
|
|
# Add token to database
|
329
|
|
- |
database.set_password_reset_token (user['id'], secret_token)
|
|
329
|
+ |
database.set_password_reset_token(user['id'], secret_token)
|
330
|
330
|
|
|
331
|
331
|
|
# Send token via email
|
332
|
|
- |
client_ip = request.environ.get ('HTTP_X_FORWARDED_FOR') or \
|
333
|
|
- |
request.environ.get ('REMOTE_ADDR')
|
334
|
|
- |
email_from = 'freepost <noreply@freepost.peers.community>'
|
|
332
|
+ |
client_ip = request.environ.get('HTTP_X_FORWARDED_FOR') or \
|
|
333
|
+ |
request.environ.get('REMOTE_ADDR')
|
335
|
334
|
|
email_to = user['email']
|
336
|
335
|
|
email_subject = 'freepost password reset'
|
337
|
|
- |
email_body = template (
|
|
336
|
+ |
email_body = template(
|
338
|
337
|
|
'email/password_reset.txt',
|
339
|
338
|
|
ip=client_ip,
|
340
|
339
|
|
secret_token=secret_token)
|
341
|
340
|
|
|
342
|
|
- |
mail.send (email_from, email_to, email_subject, email_body)
|
|
341
|
+ |
mail.send(email_to, email_subject, email_body)
|
343
|
342
|
|
|
344
|
|
- |
redirect (application.get_url ('change_password'))
|
|
343
|
+ |
redirect(application.get_url('change_password'))
|
345
|
344
|
|
|
346
|
345
|
|
@get ('/change_password', name='change_password')
|
347
|
346
|
|
@requires_logout
|
361
|
360
|
|
is OK change the user password.
|
362
|
361
|
|
"""
|
363
|
362
|
|
|
364
|
|
- |
username = request.forms.getunicode ('username')
|
365
|
|
- |
email = request.forms.getunicode ('email')
|
366
|
|
- |
password = request.forms.getunicode ('password')
|
367
|
|
- |
secret_token = request.forms.getunicode ('token')
|
|
363
|
+ |
username = request.forms.getunicode('username')
|
|
364
|
+ |
email = request.forms.getunicode('email')
|
|
365
|
+ |
password = request.forms.getunicode('password')
|
|
366
|
+ |
secret_token = request.forms.getunicode('token')
|
368
|
367
|
|
|
369
|
368
|
|
# We must have all fields
|
370
|
369
|
|
if not username or not email or not password or not secret_token:
|
371
|
|
- |
redirect (application.get_url ('login'))
|
|
370
|
+ |
redirect(application.get_url('login'))
|
372
|
371
|
|
|
373
|
372
|
|
# Password too short?
|
374
|
373
|
|
if len (password) < 8:
|
377
|
376
|
|
flash = 'Password must be at least 8 characters long')
|
378
|
377
|
|
|
379
|
378
|
|
# OK, everything should be fine now. Reset user password.
|
380
|
|
- |
database.reset_password (username, email, password, secret_token)
|
|
379
|
+ |
database.reset_password(username, email, password, secret_token)
|
381
|
380
|
|
|
382
|
381
|
|
# Check if the password was successfully reset
|
383
|
382
|
|
user = database.check_user_credentials (username, password)
|
388
|
387
|
|
|
389
|
388
|
|
# Everything matched!
|
390
|
389
|
|
# Notify user of password change.
|
391
|
|
- |
email_from = 'freepost <noreply@freepost.peers.community>'
|
392
|
390
|
|
email_to = user['email']
|
393
|
391
|
|
email_subject = 'freepost password changed'
|
394
|
392
|
|
email_body = template ('email/password_changed.txt')
|
395
|
393
|
|
|
396
|
|
- |
mail.send (email_from, email_to, email_subject, email_body)
|
|
394
|
+ |
mail.send (email_to, email_subject, email_body)
|
397
|
395
|
|
|
398
|
396
|
|
# Start new session and redirect user
|
399
|
397
|
|
session.start (user['id'])
|