2016-03-07 18:18:52 +00:00
|
|
|
import json
|
|
|
|
|
|
|
|
|
|
from flask import (render_template, url_for, redirect, flash, session, current_app, abort)
|
|
|
|
|
from itsdangerous import SignatureExpired
|
2016-01-05 17:52:09 +00:00
|
|
|
|
|
|
|
|
from app.main import main
|
2016-01-06 17:37:07 +00:00
|
|
|
from app.main.forms import NewPasswordForm
|
2016-03-07 18:18:52 +00:00
|
|
|
from datetime import datetime
|
2016-03-30 09:58:10 +01:00
|
|
|
from app import user_api_client
|
2016-01-06 17:37:07 +00:00
|
|
|
|
2016-01-05 17:52:09 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
@main.route('/new-password/<path:token>', methods=['GET', 'POST'])
|
|
|
|
|
def new_password(token):
|
2016-03-07 18:18:52 +00:00
|
|
|
from utils.url_safe_token import check_token
|
|
|
|
|
try:
|
|
|
|
|
token_data = check_token(token, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT'],
|
|
|
|
|
current_app.config['TOKEN_MAX_AGE_SECONDS'])
|
|
|
|
|
except SignatureExpired:
|
2016-01-08 16:47:34 +00:00
|
|
|
flash('The link in the email we sent you has expired. Enter your email address to resend.')
|
2016-01-08 15:12:14 +00:00
|
|
|
return redirect(url_for('.forgot_password'))
|
|
|
|
|
|
2016-03-07 18:18:52 +00:00
|
|
|
email_address = json.loads(token_data)['email']
|
2016-03-30 09:58:10 +01:00
|
|
|
user = user_api_client.get_user_by_email(email_address)
|
2016-03-07 18:18:52 +00:00
|
|
|
if user.password_changed_at and datetime.strptime(user.password_changed_at, '%Y-%m-%d %H:%M:%S.%f') > \
|
|
|
|
|
datetime.strptime(json.loads(token_data)['created_at'], '%Y-%m-%d %H:%M:%S.%f'):
|
|
|
|
|
flash('The link in the email has already been used')
|
|
|
|
|
return redirect(url_for('main.index'))
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
form = NewPasswordForm()
|
2016-01-08 15:12:14 +00:00
|
|
|
|
2016-01-06 17:37:07 +00:00
|
|
|
if form.validate_on_submit():
|
2016-03-30 09:58:10 +01:00
|
|
|
user_api_client.send_verify_code(user.id, 'sms', user.mobile_number)
|
2016-01-27 16:30:33 +00:00
|
|
|
session['user_details'] = {
|
|
|
|
|
'id': user.id,
|
|
|
|
|
'email': user.email_address,
|
|
|
|
|
'password': form.new_password.data}
|
2016-01-08 15:12:14 +00:00
|
|
|
return redirect(url_for('main.two_factor'))
|
2016-01-06 17:37:07 +00:00
|
|
|
else:
|
2016-01-08 15:12:14 +00:00
|
|
|
return render_template('views/new-password.html', token=token, form=form, user=user)
|