haproxy-wi/app/login.py

96 lines
3.2 KiB
Python
Raw Normal View History

2018-01-15 06:16:04 +00:00
#!/usr/bin/env python3
import cgi
import html
import os
2018-04-23 04:49:23 +00:00
import sys
2018-01-15 06:16:04 +00:00
import funct
import http.cookies
import sql
2018-04-23 04:49:23 +00:00
import create_db
import datetime
import uuid
from configparser import ConfigParser, ExtendedInterpolation
2018-01-15 06:16:04 +00:00
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
form = cgi.FieldStorage()
ref = form.getvalue('ref')
login = form.getvalue('login')
password = form.getvalue('pass')
path_config = "haproxy-webintarface.config"
config = ConfigParser(interpolation=ExtendedInterpolation())
config.read(path_config)
2018-01-15 06:16:04 +00:00
def login_page(error):
if error == "error":
funct.head("Login page")
2018-04-23 04:49:23 +00:00
printError = "<h2>Login page. Enter please</h2><br /><br /><b style='color: red'>Somthing wrong :( I'm sad about this, but try again!</b><br /><br />"
2018-01-15 06:16:04 +00:00
else:
2018-02-10 14:16:33 +00:00
printError = "<h2>Login page. Enter please</h2><br /><br />"
2018-04-23 04:49:23 +00:00
if create_db.check_db():
if create_db.create_table():
print('<div class="alert alert-success">DB was created<br />')
2018-04-23 04:49:23 +00:00
create_db.update_all()
print('<br />Now you can login, default: admin/admin</div>')
create_db.update_all_silent()
ref = form.getvalue('ref')
if ref is None:
ref = "/index.html"
2018-04-23 04:49:23 +00:00
2018-04-13 06:48:21 +00:00
print('<center><form name="auth" action="login.py" class="form-horizontal" method="get">')
2018-01-15 06:16:04 +00:00
print(printError)
2018-02-10 14:16:33 +00:00
print('<label for="login">Login: </label> <input type="text" name="login" required class="form-control"><br /><br />')
print('<label for="pass">Pass: </label> <input type="password" name="pass" required class="form-control"><br /><br />')
2018-01-15 06:16:04 +00:00
print('<input type="hidden" value="%s" name="ref">' % ref)
print('<button type="submit" name="Login" value="Enter">Sign Up</button>')
print('</form></center>')
2018-01-31 09:22:14 +00:00
try:
if config.get('main', 'session_ttl'):
session_ttl = config.getint('main', 'session_ttl')
except:
print('<center><div class="alert alert-danger">Can not find "session_ttl" parametr. Check into config, "main" section</div>')
if form.getvalue('logout') is not None:
cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
user_id = cookie.get('uuid')
try:
sql.delete_uuid(user_id.value)
except:
pass
print("Set-cookie: uuid=; expires=Wed May 18 03:33:20 2003; path=/app/; httponly")
2018-01-31 09:22:14 +00:00
print("Content-type: text/html\n")
print('<meta http-equiv="refresh" content="0; url=/app/login.py">')
2018-01-15 06:16:04 +00:00
if login is None:
2018-04-23 04:49:23 +00:00
funct.head("Login page")
2018-01-15 06:16:04 +00:00
login_page("n")
2018-01-15 06:16:04 +00:00
if login is not None and password is not None:
if form.getvalue('ref') is None:
ref = "/index.html"
USERS = sql.select_users()
session_ttl = config.getint('main', 'session_ttl')
expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl)
user_uuid = str(uuid.uuid4())
for users in USERS:
if login in users[1] and password == users[3]:
2018-01-31 09:22:14 +00:00
c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE"))
c["uuid"] = user_uuid
c["uuid"]["path"] = "/app/"
c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT")
2018-01-31 09:22:14 +00:00
print(c)
sql.write_user_uuid(login, user_uuid)
print("Content-type: text/html\n")
2018-01-15 06:16:04 +00:00
print('<html><head><title>Redirecting</title><meta charset="UTF-8">')
print('<link href="/style.css" rel="stylesheet">')
print('<meta http-equiv="refresh" content="0; url=%s">' % ref)
2018-04-23 04:49:23 +00:00
sys.exit()
login_page("error")
2018-04-23 16:20:19 +00:00
funct.footer()