# Path: ddserver/utils/deps.py
# def require(**requirements):
#   ''' Decorator to inject requirements into a function.
# 
#       The decorated function is called with a named argument for each
#       specified requirement containing the exported instances.
#   '''
# 
#   exports = {name : Export.load(requirement)
#              for name, requirement
#              in requirements.iteritems()}
# 
#   def wrapper(func):
#     def wrapped(*args, **kwargs):
#       # Update the keyword arguments with the required instances
#       kwargs.update({name : export.instance
#                      for name, export
#                      in exports.iteritems()
#                      if name not in kwargs})
# 
#       return func(*args,
#                   **kwargs)
# 
#     wrapped.__name__ = func.__name__
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/user.py
# def authorized_by_code():
#   ''' Checks if a valid username and authcode is passed. '''
# 
#   def wrapper(func):
#     @require(users = 'ddserver.interface.user:UserManager',
#              messages = 'ddserver.interface.message:MessageManager')
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
#       if (bottle.request.query.username == "" or
#          bottle.request.query.authcode == ""):
#         messages.error('You have to provide username and authcode.')
#         bottle.redirect('/')
# 
#       username = bottle.request.query.username
#       authcode = bottle.request.query.authcode
# 
#       user = users[username]
# 
#       if user == None:
#         messages.error('The username does not exist.')
#         bottle.redirect('/')
# 
#       if user.authcode != authcode:
#         messages.error('The auth code you provided was invalid.')
#         bottle.redirect('/')
# 
#       # Inject the user in the wrapped function
#       return func(*args,
#                   user = user,
#                   **kwargs)
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/captcha.py
# def captcha_check(__on_error__):
#   ''' Checks if the captcha challenge and response in the request are matching.
# 
#       The challenge and response values are extracted from the POST data and
#       passed to the recaptcha API.
# 
#       @param __on_error__: The target to redirect if the check failed
#   '''
# 
#   def wrapper(func):
#     @require(config = 'ddserver.config:Config',
#              users = 'ddserver.interface.user:UserManager',
#              messages = 'ddserver.interface.message:MessageManager')
#     def wrapped(config,
#                 users,
#                 messages,
#                 *args,
#                 **kwargs):
#       if config.captcha.enabled:
#         from recaptcha.client import captcha
# 
#         challenge = bottle.request.POST.pop('recaptcha_challenge_field', None)
#         response = bottle.request.POST.pop('recaptcha_response_field', None)
# 
#         if challenge is None or response is None:
#           messages.error('Captcha values are missing')
#           bottle.redirect('/')
# 
#         result = captcha.submit(challenge,
#                                 response,
#                                 config.captcha.recaptcha_private_key,
#                                 bottle.request.remote_addr)
# 
#         if not result.is_valid:
#           messages.error('Captcha invalid')
#           bottle.redirect(__on_error__)
# 
#       # Call the wrapped function
#       return func(*args,
#                   **kwargs)
# 
#     return wrapped
#   return wrapper


import bottle
from ddserver.web import route
from ddserver.utils.deps import require
from ddserver.interface.user import authorized_by_code
from ddserver.interface import validation
from ddserver.interface.validation import validate
from ddserver.interface.captcha import captcha_check
from passlib.apps import custom_app_context as pwd
  users.generate_authcode(data.username)

  # Fetch user info
  user = users[data.username]

  emails.to_user('lostpasswd.mail',
                 user = user)

  messages.success('Password recovery email has been sent.')

  bottle.redirect('/')



@route('/lostpass/recover', method = 'GET')
@require(templates = 'ddserver.interface.template:TemplateManager')
def get_lostpass_setnew(templates):
  username = bottle.request.query.username
  authcode = bottle.request.query.authcode

  return templates['resetpass.html'](username = username,
                                     authcode = authcode)



@route('/lostpass/setnew', method = 'POST')
@authorized_by_code()
@validate(password = validation.SecurePassword(min = 8),
          password_confirm = validation.String(),
          chained_validators = [validation.FieldsMatch('password', 'password_confirm')])

# Path: ddserver/utils/deps.py
# def require(**requirements):
#   ''' Decorator to inject requirements into a function.
# 
#       The decorated function is called with a named argument for each
#       specified requirement containing the exported instances.
#   '''
# 
#   exports = {name : Export.load(requirement)
#              for name, requirement
#              in requirements.iteritems()}
# 
#   def wrapper(func):
#     def wrapped(*args, **kwargs):
#       # Update the keyword arguments with the required instances
#       kwargs.update({name : export.instance
#                      for name, export
#                      in exports.iteritems()
#                      if name not in kwargs})
# 
#       return func(*args,
#                   **kwargs)
# 
#     wrapped.__name__ = func.__name__
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/user.py
# def authorized_by_code():
#   ''' Checks if a valid username and authcode is passed. '''
# 
#   def wrapper(func):
#     @require(users = 'ddserver.interface.user:UserManager',
#              messages = 'ddserver.interface.message:MessageManager')
#     def wrapped(users,
#                 messages,
#                 *args,
#                 **kwargs):
#       if (bottle.request.query.username == "" or
#          bottle.request.query.authcode == ""):
#         messages.error('You have to provide username and authcode.')
#         bottle.redirect('/')
# 
#       username = bottle.request.query.username
#       authcode = bottle.request.query.authcode
# 
#       user = users[username]
# 
#       if user == None:
#         messages.error('The username does not exist.')
#         bottle.redirect('/')
# 
#       if user.authcode != authcode:
#         messages.error('The auth code you provided was invalid.')
#         bottle.redirect('/')
# 
#       # Inject the user in the wrapped function
#       return func(*args,
#                   user = user,
#                   **kwargs)
# 
#     return wrapped
#   return wrapper
# 
# Path: ddserver/interface/captcha.py
# def captcha_check(__on_error__):
#   ''' Checks if the captcha challenge and response in the request are matching.
# 
#       The challenge and response values are extracted from the POST data and
#       passed to the recaptcha API.
# 
#       @param __on_error__: The target to redirect if the check failed
#   '''
# 
#   def wrapper(func):
#     @require(config = 'ddserver.config:Config',
#              users = 'ddserver.interface.user:UserManager',
#              messages = 'ddserver.interface.message:MessageManager')
#     def wrapped(config,
#                 users,
#                 messages,
#                 *args,
#                 **kwargs):
#       if config.captcha.enabled:
#         from recaptcha.client import captcha
# 
#         challenge = bottle.request.POST.pop('recaptcha_challenge_field', None)
#         response = bottle.request.POST.pop('recaptcha_response_field', None)
# 
#         if challenge is None or response is None:
#           messages.error('Captcha values are missing')
#           bottle.redirect('/')
# 
#         result = captcha.submit(challenge,
#                                 response,
#                                 config.captcha.recaptcha_private_key,
#                                 bottle.request.remote_addr)
# 
#         if not result.is_valid:
#           messages.error('Captcha invalid')
#           bottle.redirect(__on_error__)
# 
#       # Call the wrapped function
#       return func(*args,
#                   **kwargs)
# 
#     return wrapped
#   return wrapper


# Path: ddserver/interface/pages/lostpasswd.py
import bottle
from ddserver.web import route
from ddserver.utils.deps import require
from ddserver.interface.user import authorized_by_code
from ddserver.interface import validation
from ddserver.interface.validation import validate
from ddserver.interface.captcha import captcha_check
from passlib.apps import custom_app_context as pwd

  users.generate_authcode(data.username)

  # Fetch user info
  user = users[data.username]

  emails.to_user('lostpasswd.mail',
                 user = user)

  messages.success('Password recovery email has been sent.')

  bottle.redirect('/')



@route('/lostpass/recover', method = 'GET')
@require(templates = 'ddserver.interface.template:TemplateManager')
def get_lostpass_setnew(templates):
  username = bottle.request.query.username
  authcode = bottle.request.query.authcode

  return templates['resetpass.html'](username = username,
                                     authcode = authcode)



@route('/lostpass/setnew', method = 'POST')
@authorized_by_code()
@validate(password = validation.SecurePassword(min = 8),
          password_confirm = validation.String(),
          chained_validators = [validation.FieldsMatch('password', 'password_confirm')])

@require(db = 'ddserver.db:Database',