def send_cookie(login): session_ttl = sql.get_setting('session_ttl') session_ttl = int(session_ttl) expires = datetime.datetime.utcnow() + datetime.timedelta(days=session_ttl) user_uuid = str(uuid.uuid4()) user_token = str(uuid.uuid4()) sql.write_user_uuid(login, user_uuid) sql.write_user_token(login, user_token) id = sql.get_user_id_by_uuid(user_uuid) try: cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_group_id = cookie.get('group') user_group_id = user_group_id.value if sql.check_user_group(id, user_group_id): user_groups = user_group_id else: user_groups = sql.select_user_groups(id, limit=1) except: user_groups = sql.select_user_groups(id, limit=1) c = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) c["uuid"] = user_uuid c["uuid"]["path"] = "/app" # c["uuid"]["samesite"] = "Strict" c["uuid"]["Secure"] = "True" c["uuid"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") c["group"] = user_groups c["group"]["path"] = "/app" # c["group"]["samesite"] = "Strict" c["group"]["Secure"] = "True" c["group"]["expires"] = expires.strftime("%a, %d %b %Y %H:%M:%S GMT") print(c.output()) try: groups = sql.select_groups(id=user_groups) for g in groups: if g[0] == int(user_groups): user_group = g[1] except: user_group = '' try: user_name = sql.get_user_name_by_uuid(user_uuid) funct.logging('localhost', ' user: '******', group: ' + user_group + ' log in', haproxywi=1) except: pass print("Content-type: text/html\n") print('ok') sys.exit()
def check_user_group(): import http.cookies import os import sql cookie = http.cookies.SimpleCookie(os.environ.get("HTTP_COOKIE")) user_uuid = cookie.get('uuid') group = cookie.get('group') group_id = group.value user_id = sql.get_user_id_by_uuid(user_uuid.value) if sql.check_user_group(user_id, group_id): return True else: logging('localhost', ' has tried to actions in not his group ', haproxywi=1, login=1) print('Atata!') sys.exit()