2020-08-12 00:36:09 +00:00
|
|
|
import logging
|
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.models.users import Users
|
2022-01-15 00:23:50 +00:00
|
|
|
from app.classes.shared.authentication import authentication
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
2021-09-08 22:01:10 +00:00
|
|
|
from app.classes.shared.main_models import fn
|
2022-01-26 01:45:30 +00:00
|
|
|
from app.classes.web.base_handler import BaseHandler
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
try:
|
|
|
|
import bleach
|
|
|
|
|
|
|
|
except ModuleNotFoundError as e:
|
2022-03-08 04:40:44 +00:00
|
|
|
helper.auto_installer_fix(e)
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
class PublicHandler(BaseHandler):
|
2022-01-15 00:23:50 +00:00
|
|
|
def set_current_user(self, user_id: str = None):
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
expire_days = helper.get_setting("cookie_expire")
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
# if helper comes back with false
|
|
|
|
if not expire_days:
|
|
|
|
expire_days = "5"
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
if user_id is not None:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.set_cookie(
|
|
|
|
"token", authentication.generate(user_id), expires_days=int(expire_days)
|
|
|
|
)
|
2020-08-12 00:36:09 +00:00
|
|
|
else:
|
2022-03-14 21:26:09 +00:00
|
|
|
self.clear_cookie("token")
|
2022-03-23 02:50:12 +00:00
|
|
|
# self.clear_cookie("user")
|
|
|
|
# self.clear_cookie("user_data")
|
2020-08-12 00:36:09 +00:00
|
|
|
|
|
|
|
def get(self, page=None):
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
error = bleach.clean(self.get_argument("error", "Invalid Login!"))
|
|
|
|
error_msg = bleach.clean(self.get_argument("error_msg", ""))
|
2020-08-23 22:43:28 +00:00
|
|
|
|
2022-03-05 11:01:36 +00:00
|
|
|
page_data = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"version": helper.get_version_string(),
|
|
|
|
"error": error,
|
|
|
|
"lang": helper.get_setting("language"),
|
|
|
|
"lang_page": helper.getLangPage(helper.get_setting("language")),
|
|
|
|
"query": "",
|
2022-03-05 11:01:36 +00:00
|
|
|
}
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
page_data["query"] = self.request.query
|
2020-08-13 01:33:36 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
# sensible defaults
|
|
|
|
template = "public/404.html"
|
|
|
|
|
2020-08-12 00:36:09 +00:00
|
|
|
if page == "login":
|
|
|
|
template = "public/login.html"
|
2020-08-23 22:43:28 +00:00
|
|
|
|
|
|
|
elif page == 404:
|
|
|
|
template = "public/404.html"
|
|
|
|
|
|
|
|
elif page == "error":
|
|
|
|
template = "public/error.html"
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2021-07-30 16:20:01 +00:00
|
|
|
elif page == "logout":
|
2022-03-14 21:26:09 +00:00
|
|
|
self.clear_cookie("token")
|
2022-03-23 02:50:12 +00:00
|
|
|
# self.clear_cookie("user")
|
|
|
|
# self.clear_cookie("user_data")
|
|
|
|
self.redirect("/public/login")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
|
|
|
|
2020-08-17 02:47:53 +00:00
|
|
|
# if we have no page, let's go to login
|
2020-08-12 00:36:09 +00:00
|
|
|
else:
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/public/login?" + self.request.query)
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/public/login")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2021-03-26 13:57:50 +00:00
|
|
|
self.render(
|
|
|
|
template,
|
|
|
|
data=page_data,
|
|
|
|
translate=self.translator.translate,
|
2022-03-23 02:50:12 +00:00
|
|
|
error_msg=error_msg,
|
2021-03-26 13:57:50 +00:00
|
|
|
)
|
2020-08-12 00:36:09 +00:00
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
def post(self, page=None):
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
error = bleach.clean(self.get_argument("error", "Invalid Login!"))
|
|
|
|
error_msg = bleach.clean(self.get_argument("error_msg", ""))
|
2022-03-14 21:26:09 +00:00
|
|
|
|
|
|
|
page_data = {
|
2022-03-23 02:50:12 +00:00
|
|
|
"version": helper.get_version_string(),
|
|
|
|
"error": error,
|
|
|
|
"lang": helper.get_setting("language"),
|
|
|
|
"lang_page": helper.getLangPage(helper.get_setting("language")),
|
|
|
|
"query": "",
|
2022-03-14 21:26:09 +00:00
|
|
|
}
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
page_data["query"] = self.request.query
|
2022-03-14 21:26:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if page == "login":
|
2022-03-14 21:26:09 +00:00
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
next_page = "/public/login"
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
next_page = "/public/login?" + self.request.query
|
2020-08-13 01:33:36 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
entered_username = bleach.clean(self.get_argument("username"))
|
|
|
|
entered_password = bleach.clean(self.get_argument("password"))
|
2020-08-13 01:33:36 +00:00
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
# pylint: disable=no-member
|
2022-03-23 02:50:12 +00:00
|
|
|
user_data = Users.get_or_none(
|
|
|
|
fn.Lower(Users.username) == entered_username.lower()
|
|
|
|
)
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
# if we don't have a user
|
2020-08-13 01:33:36 +00:00
|
|
|
if not user_data:
|
2022-01-26 01:45:30 +00:00
|
|
|
error_msg = "Incorrect username or password. Please try again."
|
2022-03-23 02:50:12 +00:00
|
|
|
# self.clear_cookie("user")
|
|
|
|
# self.clear_cookie("user_data")
|
2022-03-14 21:26:09 +00:00
|
|
|
self.clear_cookie("token")
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(
|
|
|
|
f"/public/login?error_msg={error_msg}&{self.request.query}"
|
|
|
|
)
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(f"/public/login?error_msg={error_msg}")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2020-08-13 01:33:36 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
# if they are disabled
|
|
|
|
if not user_data.enabled:
|
2022-01-26 01:45:30 +00:00
|
|
|
error_msg = "User account disabled. Please contact your system administrator for more info."
|
2022-03-23 02:50:12 +00:00
|
|
|
# self.clear_cookie("user")
|
|
|
|
# self.clear_cookie("user_data")
|
2022-03-14 21:26:09 +00:00
|
|
|
self.clear_cookie("token")
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(
|
|
|
|
f"/public/login?error_msg={error_msg}&{self.request.query}"
|
|
|
|
)
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(f"/public/login?error_msg={error_msg}")
|
2021-07-30 16:20:01 +00:00
|
|
|
return
|
2020-08-13 14:38:36 +00:00
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
login_result = helper.verify_pass(entered_password, user_data.password)
|
|
|
|
|
|
|
|
# Valid Login
|
|
|
|
if login_result:
|
2022-01-15 00:23:50 +00:00
|
|
|
self.set_current_user(user_data.user_id)
|
2022-03-23 02:50:12 +00:00
|
|
|
logger.info(
|
|
|
|
f"User: {user_data} Logged in from IP: {self.get_remote_ip()}"
|
|
|
|
)
|
2020-08-13 01:33:36 +00:00
|
|
|
|
2020-08-13 14:38:36 +00:00
|
|
|
# record this login
|
2022-03-23 02:50:12 +00:00
|
|
|
q = (
|
|
|
|
Users.select()
|
|
|
|
.where(Users.username == entered_username.lower())
|
|
|
|
.get()
|
|
|
|
)
|
2020-09-23 03:26:23 +00:00
|
|
|
q.last_ip = self.get_remote_ip()
|
|
|
|
q.last_login = helper.get_time_as_string()
|
|
|
|
q.save()
|
|
|
|
|
2020-08-31 20:16:45 +00:00
|
|
|
# log this login
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log(
|
|
|
|
user_data.user_id, "Logged in", 0, self.get_remote_ip()
|
|
|
|
)
|
2022-03-14 21:26:09 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
if self.request.query_arguments.get("next"):
|
|
|
|
next_page = self.request.query_arguments.get("next")[0].decode()
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
|
|
|
next_page = "/panel/dashboard"
|
|
|
|
|
2020-08-13 01:33:36 +00:00
|
|
|
self.redirect(next_page)
|
2021-05-02 15:05:14 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
# self.clear_cookie("user")
|
|
|
|
# self.clear_cookie("user_data")
|
2022-03-14 21:26:09 +00:00
|
|
|
self.clear_cookie("token")
|
2021-11-23 18:22:15 +00:00
|
|
|
error_msg = "Inncorrect username or password. Please try again."
|
2021-05-02 15:05:14 +00:00
|
|
|
# log this failed login attempt
|
2022-03-23 02:50:12 +00:00
|
|
|
self.controller.management.add_to_audit_log(
|
|
|
|
user_data.user_id, "Tried to log in", 0, self.get_remote_ip()
|
|
|
|
)
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(
|
|
|
|
f"/public/login?error_msg={error_msg}&{self.request.query}"
|
|
|
|
)
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect(f"/public/login?error_msg={error_msg}")
|
2021-04-04 17:48:02 +00:00
|
|
|
else:
|
2022-03-19 01:48:24 +00:00
|
|
|
if self.request.query:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/public/login?" + self.request.query)
|
2022-03-14 21:26:09 +00:00
|
|
|
else:
|
2022-03-23 02:50:12 +00:00
|
|
|
self.redirect("/public/login")
|