# Path: rhizi/crypt_util.py
# def hash_pw(pw_str, salt_str):
#     salt = hashlib.sha512(salt_str.encode('utf-8')).hexdigest()
#     ret = hashlib.sha512((pw_str + salt).encode('utf-8')).hexdigest()
#     return ret
# 
# Path: rhizi/rz_mail.py
# def send_email__flask_ctx(recipients, subject, body, attachments=[]):
#     """
#     Flask context dependent email sending utility
#     """
# 
#     send_from = current_app.rz_config.mail_default_sender
#     mta_host = current_app.rz_config.mta_host
#     mta_port = current_app.rz_config.mta_port
# 
#     send_email(mta_host,
#                         mta_port,
#                         send_from=send_from,
#                         recipients=recipients,
#                         subject=subject,
#                         attachments=attachments,
#                         body=body)
# 
#     log.info('email sent: recipients: %s: subject: %s, attachment-count: %d' % (recipients, subject, len(attachments)))
# 
# Path: rhizi/rz_req_handling.py
# def make_response__json(status=200, data={}):
#     """
#     Construct a json response with proper content-type header
# 
#     @param data: must be serializable via json.dumps
#     """
#     data_str = json.dumps(data)
#     resp = make_response(data_str)
#     resp.headers['Content-Type'] = "application/json"
#     resp.status = str(status)
# 
#     return resp
# 
# def make_response__json__html(status=200, html_str=''):
#     """
#     Construct a json response with HTML payload
#     """
#     return make_response__json(status=status, data={'response__html': html_str })
# 
# HTTP_STATUS__400_BAD_REQUEST = 400
# 
# HTTP_STATUS__500_INTERNAL_SERVER_ERROR = 500
# 
# HTTP_STATUS__200_OK = 200
# 
# HTTP_STATUS__401_UNAUTORIZED = 401


from datetime import datetime
from datetime import timedelta
from flask import current_app
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
from .crypt_util import hash_pw
from .rz_api_common import API_Exception__bad_request
from .rz_mail import send_email__flask_ctx
from .rz_req_handling import (make_response__json, make_response__json__html,
    HTTP_STATUS__400_BAD_REQUEST, HTTP_STATUS__500_INTERNAL_SERVER_ERROR,
    HTTP_STATUS__200_OK, HTTP_STATUS__401_UNAUTORIZED)
import logging
import re
import uuid

            if None == pw_rst_req:
                # request expired & already removed OR bad token
                log.warning('pw reset: request not found or bad token: remote-address: %s' % (request.remote_addr))
                return render_template('pw_reset.html', pw_rst_step='general_error')

            if pw_rst_req.has_expired():  # check again whether request has expired
                log.warning('pw reset: attempt to activate expired reset request: email: %s' % (pw_rst_req.email_address))
                return render_template('pw_reset.html', pw_rst_step='general_error')

            return render_template('pw_reset.html', pw_rst_step='step_1__collect_new_password')

    if request.method == 'POST':

        req_data = sanitize_input(request)
        user_db = current_app.user_db

        pw_rst_tok = req_data.get('pw_rst_tok')
        new_user_pw = req_data.get('new_user_pw')
        email_address = req_data.get('email_address')

        # handle according to passed req arguments
        if pw_rst_tok and new_user_pw and None == email_address:  # validate token & collect new pw

            uid = None

            pw_rst_req = pw_rst_req_map.get(pw_rst_tok)
            if None == pw_rst_req:
                # request expired & already removed OR bad token
                log.warning('pw reset: request not found or bad token: remote-address: %s' % (request.remote_addr))

# Path: rhizi/crypt_util.py
# def hash_pw(pw_str, salt_str):
#     salt = hashlib.sha512(salt_str.encode('utf-8')).hexdigest()
#     ret = hashlib.sha512((pw_str + salt).encode('utf-8')).hexdigest()
#     return ret
# 
# Path: rhizi/rz_mail.py
# def send_email__flask_ctx(recipients, subject, body, attachments=[]):
#     """
#     Flask context dependent email sending utility
#     """
# 
#     send_from = current_app.rz_config.mail_default_sender
#     mta_host = current_app.rz_config.mta_host
#     mta_port = current_app.rz_config.mta_port
# 
#     send_email(mta_host,
#                         mta_port,
#                         send_from=send_from,
#                         recipients=recipients,
#                         subject=subject,
#                         attachments=attachments,
#                         body=body)
# 
#     log.info('email sent: recipients: %s: subject: %s, attachment-count: %d' % (recipients, subject, len(attachments)))
# 
# Path: rhizi/rz_req_handling.py
# def make_response__json(status=200, data={}):
#     """
#     Construct a json response with proper content-type header
# 
#     @param data: must be serializable via json.dumps
#     """
#     data_str = json.dumps(data)
#     resp = make_response(data_str)
#     resp.headers['Content-Type'] = "application/json"
#     resp.status = str(status)
# 
#     return resp
# 
# def make_response__json__html(status=200, html_str=''):
#     """
#     Construct a json response with HTML payload
#     """
#     return make_response__json(status=status, data={'response__html': html_str })
# 
# HTTP_STATUS__400_BAD_REQUEST = 400
# 
# HTTP_STATUS__500_INTERNAL_SERVER_ERROR = 500
# 
# HTTP_STATUS__200_OK = 200
# 
# HTTP_STATUS__401_UNAUTORIZED = 401


# Path: rhizi/rz_user.py
from datetime import datetime
from datetime import timedelta
from flask import current_app
from flask import make_response
from flask import redirect
from flask import render_template
from flask import request
from flask import session
from flask import url_for
from .crypt_util import hash_pw
from .rz_api_common import API_Exception__bad_request
from .rz_mail import send_email__flask_ctx
from .rz_req_handling import (make_response__json, make_response__json__html,
    HTTP_STATUS__400_BAD_REQUEST, HTTP_STATUS__500_INTERNAL_SERVER_ERROR,
    HTTP_STATUS__200_OK, HTTP_STATUS__401_UNAUTORIZED)
import logging
import re
import uuid


            if None == pw_rst_req:
                # request expired & already removed OR bad token
                log.warning('pw reset: request not found or bad token: remote-address: %s' % (request.remote_addr))
                return render_template('pw_reset.html', pw_rst_step='general_error')

            if pw_rst_req.has_expired():  # check again whether request has expired
                log.warning('pw reset: attempt to activate expired reset request: email: %s' % (pw_rst_req.email_address))
                return render_template('pw_reset.html', pw_rst_step='general_error')

            return render_template('pw_reset.html', pw_rst_step='step_1__collect_new_password')

    if request.method == 'POST':

        req_data = sanitize_input(request)
        user_db = current_app.user_db

        pw_rst_tok = req_data.get('pw_rst_tok')
        new_user_pw = req_data.get('new_user_pw')
        email_address = req_data.get('email_address')

        # handle according to passed req arguments
        if pw_rst_tok and new_user_pw and None == email_address:  # validate token & collect new pw

            uid = None

            pw_rst_req = pw_rst_req_map.get(pw_rst_tok)
            if None == pw_rst_req:
                # request expired & already removed OR bad token
                log.warning('pw reset: request not found or bad token: remote-address: %s' % (request.remote_addr))

                return make_response__json__html(status=HTTP_STATUS__500_INTERNAL_SERVER_ERROR, html_str=html_err__tech_difficulty)