2021-10-15 14:08:27 +00:00
|
|
|
from .get import *
|
|
|
|
from .alerts import send_notification
|
|
|
|
from files.helpers.const import *
|
|
|
|
|
|
|
|
|
|
|
|
def get_logged_in_user():
|
2021-12-17 03:25:05 +00:00
|
|
|
token = request.headers.get("Authorization")
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-17 03:25:05 +00:00
|
|
|
if token:
|
|
|
|
client = g.db.query(ClientAuth).filter(ClientAuth.access_token == token).first()
|
|
|
|
if not client: return None
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-17 03:25:05 +00:00
|
|
|
v = client.user
|
|
|
|
v.client = client
|
|
|
|
return v
|
2021-10-15 14:08:27 +00:00
|
|
|
else:
|
|
|
|
uid = session.get("user_id")
|
|
|
|
nonce = session.get("login_nonce", 0)
|
2021-12-17 03:25:05 +00:00
|
|
|
logged_in = session.get("logged_in")
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-17 04:44:25 +00:00
|
|
|
if not uid: return None
|
|
|
|
# if not uid or not logged_in or uid != logged_in: return None
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-17 17:55:11 +00:00
|
|
|
try:
|
|
|
|
if g.db: v = g.db.query(User).filter_by(id=uid).first()
|
|
|
|
else: return None
|
|
|
|
except: return None
|
2021-12-17 05:21:55 +00:00
|
|
|
|
2021-12-17 03:25:05 +00:00
|
|
|
if not v or nonce < v.login_nonce: return None
|
2021-10-15 14:08:27 +00:00
|
|
|
|
2021-12-17 04:47:38 +00:00
|
|
|
v.client = None
|
2021-12-17 03:25:05 +00:00
|
|
|
return v
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
def check_ban_evade(v):
|
2021-11-16 00:05:54 +00:00
|
|
|
if v and v.ban_evade and v.admin_level == 0 and not v.is_suspended:
|
|
|
|
if random.randint(0,30) < v.ban_evade: v.shadowbanned = "AutoJanny"
|
|
|
|
else: v.ban_evade +=1
|
2021-10-15 14:08:27 +00:00
|
|
|
g.db.add(v)
|
2021-11-16 00:05:54 +00:00
|
|
|
g.db.commit()
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
def auth_desired(f):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
v = get_logged_in_user()
|
2021-12-13 21:37:45 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
check_ban_evade(v)
|
|
|
|
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def auth_required(f):
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
v = get_logged_in_user()
|
|
|
|
|
|
|
|
if not v: abort(401)
|
2021-12-13 21:37:45 +00:00
|
|
|
|
2021-10-15 14:08:27 +00:00
|
|
|
check_ban_evade(v)
|
|
|
|
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def is_not_banned(f):
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
v = get_logged_in_user()
|
|
|
|
|
|
|
|
if not v: abort(401)
|
|
|
|
|
|
|
|
check_ban_evade(v)
|
|
|
|
|
2021-11-30 17:19:55 +00:00
|
|
|
if v.is_suspended: return {"error": "You can't perform this action while being banned."}, 403
|
2021-10-15 14:08:27 +00:00
|
|
|
|
|
|
|
resp = make_response(f(*args, v=v, **kwargs))
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
|
|
|
def admin_level_required(x):
|
|
|
|
|
|
|
|
def wrapper_maker(f):
|
|
|
|
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
|
|
|
|
v = get_logged_in_user()
|
|
|
|
|
|
|
|
if not v: abort(401)
|
|
|
|
|
|
|
|
if v.admin_level < x: abort(403)
|
|
|
|
|
|
|
|
response = f(*args, v=v, **kwargs)
|
|
|
|
|
|
|
|
if isinstance(response, tuple): resp = make_response(response[0])
|
|
|
|
else: resp = make_response(response)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return wrapper_maker
|
|
|
|
|
|
|
|
|
|
|
|
def validate_formkey(f):
|
|
|
|
def wrapper(*args, v, **kwargs):
|
|
|
|
|
|
|
|
if not request.headers.get("Authorization"):
|
|
|
|
|
|
|
|
submitted_key = request.values.get("formkey", None)
|
|
|
|
|
|
|
|
if not submitted_key: abort(401)
|
|
|
|
|
|
|
|
elif not v.validate_formkey(submitted_key): abort(401)
|
|
|
|
|
|
|
|
return f(*args, v=v, **kwargs)
|
|
|
|
|
|
|
|
wrapper.__name__ = f.__name__
|
2021-09-17 11:14:45 +00:00
|
|
|
return wrapper
|