# 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


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
GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with ddserver. If not, see <http://www.gnu.org/licenses/>.
'''








@route('/login', method = 'POST')
@validate('/',
          username = validation.ValidUsername(min = 1, max = 255),
          password = validation.String())
@require(users = 'ddserver.interface.user:UserManager')
def post_login(data,
               users):
  ''' Handles user authentication. '''

  users.login(username = data.username,
              password = data.password)

  bottle.redirect('/')



@route('/logout', method = 'GET')

# 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/pages/login.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

GNU Affero General Public License for more details.

You should have received a copy of the GNU Affero General Public License
along with ddserver. If not, see <http://www.gnu.org/licenses/>.
'''








@route('/login', method = 'POST')
@validate('/',
          username = validation.ValidUsername(min = 1, max = 255),
          password = validation.String())
@require(users = 'ddserver.interface.user:UserManager')
def post_login(data,
               users):
  ''' Handles user authentication. '''

  users.login(username = data.username,
              password = data.password)

  bottle.redirect('/')



@route('/logout', method = 'GET')

@require(users = 'ddserver.interface.user:UserManager')