feat: allow debugging with pdb

allows user to debug the webapp when admin API key is passed as a param.
also - an anniversary, 100th commit!
This commit is contained in:
2025-11-13 11:28:40 +01:00
parent 11c94c757e
commit c979c97077
3 changed files with 27 additions and 16 deletions

View File

@@ -8,19 +8,20 @@ import ythdd_globals, ythdd_extractor
#from flask_sqlalchemy import SQLAlchemy
#import ythdd_api_v1_stats, ythdd_api_v1_user, ythdd_api_v1_info, ythdd_api_v1_query, ythdd_api_v1_meta, ythdd_api_v1_admin
def requireAuthentication(func):
@wraps(func)
def wrapper(*args, **kwargs):
token = kwargs["r"].args.get('token')
if token == lewy_globals.config['api']['api_key']:
try:
status, received, data = func(*args, **kwargs)
return status, received, data
except:
raise AssertionError(f"Function \"{func.__name__}\" does not return status, code, and data as it should!")
else:
return 401, "error", {'error_msg': "Unauthorized"}
return wrapper
def requireAuthentication(admin: bool = True):
def functionWrapper(func):
def wrapper(*args, **kwargs):
token = kwargs["r"].args.get('token')
if token == ythdd_globals.config['api']['api_key' + admin * '_admin']:
try:
status, received, data = func(*args, **kwargs)
return status, received, data
except:
raise AssertionError(f"Function \"{func.__name__}\" does not return status, code, and data as it should!")
else:
return 401, "error", {'error_msg': "Unauthorized"}
return wrapper
return functionWrapper
def incrementBadRequests():
ythdd_globals.apiFailedRequests += 1
@@ -143,6 +144,13 @@ def hot(data):
incrementBadRequests()
return notImplemented([data[1]]) # workaround before notImplemented is reworked
@requireAuthentication(admin=True)
def debugger_halt(r):
if not ythdd_globals.config["api"]["enable_debugger_halt"]:
return 403, "Administrator has disabled access for this endpoint.", []
breakpoint()
return 200, "Pdb triggered and ended successfully.", []
def lookup(data, request):
match data[0]:
case 'stats':
@@ -163,6 +171,8 @@ def lookup(data, request):
case 'admin':
# REQUIRE CREDENTIALS!
return stub_hello()
case 'halt':
return debugger_halt(r=request)
case _:
incrementBadRequests()
return notImplemented(data)