gns3-server/gns3server/handlers/auth_handler.py

98 lines
2.9 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
#
# Copyright (C) 2014 GNS3 Technologies Inc.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
Simple file upload & listing handler.
"""
import os
import tornado.web
import tornado.websocket
import logging
log = logging.getLogger(__name__)
2015-01-20 12:24:00 +00:00
class GNS3BaseHandler(tornado.web.RequestHandler):
2015-01-20 12:24:00 +00:00
def get_current_user(self):
if 'required_user' not in self.settings:
return "FakeUser"
user = self.get_secure_cookie("user")
if not user:
2015-01-20 12:24:00 +00:00
return None
if self.settings['required_user'] == user.decode("utf-8"):
2015-01-20 12:24:00 +00:00
return user
class GNS3WebSocketBaseHandler(tornado.websocket.WebSocketHandler):
2015-01-20 12:24:00 +00:00
def get_current_user(self):
if 'required_user' not in self.settings:
return "FakeUser"
user = self.get_secure_cookie("user")
if not user:
2015-01-20 12:24:00 +00:00
return None
if self.settings['required_user'] == user.decode("utf-8"):
2015-01-20 12:24:00 +00:00
return user
class LoginHandler(tornado.web.RequestHandler):
2015-01-20 12:24:00 +00:00
def get(self):
self.write('<html><body><form action="/login" method="post">'
'Name: <input type="text" name="name">'
'Password: <input type="text" name="password">'
'<input type="submit" value="Sign in">'
'</form></body></html>')
try:
2015-01-20 12:24:00 +00:00
redirect_to = self.get_argument("next")
self.set_secure_cookie("login_success_redirect_to", redirect_to)
except tornado.web.MissingArgumentError:
2015-01-20 12:24:00 +00:00
pass
def post(self):
user = self.get_argument("name")
password = self.get_argument("password")
if self.settings['required_user'] == user and self.settings['required_pass'] == password:
2015-01-20 12:24:00 +00:00
self.set_secure_cookie("user", user)
auth_status = "successful"
else:
2015-01-20 12:24:00 +00:00
self.set_secure_cookie("user", "None")
auth_status = "failure"
log.info("Authentication attempt {}: {}, {}".format(auth_status, user, password))
try:
2015-01-20 12:24:00 +00:00
redirect_to = self.get_secure_cookie("login_success_redirect_to")
except tornado.web.MissingArgumentError:
2015-01-20 12:24:00 +00:00
redirect_to = "/"
if redirect_to is None:
self.write({'result': auth_status})
else:
log.info('Redirecting to {}'.format(redirect_to))
2015-01-20 12:24:00 +00:00
self.redirect(redirect_to)