55from werkzeug .exceptions import NotFound , Unauthorized
66from werkzeug .utils import redirect
77
8+ from .._common import log_info_with_request
89from .._config import ADMIN_PASSWORD , API_KEY_REGISTRATION_FORM_LINK , API_KEY_REMOVAL_REQUEST_LINK , REGISTER_WEBHOOK_TOKEN
910from .._security import resolve_auth_token
1011from ..admin .models import User , UserRole
@@ -44,6 +45,24 @@ def user_exists(user_email: str = None, api_key: str = None):
4445 return True if user else False
4546
4647
48+ # ~~~~ PUBLIC ROUTES ~~~~
49+
50+
51+ @bp .route ("/registration_form" , methods = ["GET" ])
52+ def registration_form_redirect ():
53+ # TODO: replace this with our own hosted registration form instead of external
54+ return redirect (API_KEY_REGISTRATION_FORM_LINK , code = 302 )
55+
56+
57+ @bp .route ("/removal_request" , methods = ["GET" ])
58+ def removal_request_redirect ():
59+ # TODO: replace this with our own hosted form instead of external
60+ return redirect (API_KEY_REMOVAL_REQUEST_LINK , code = 302 )
61+
62+
63+ # ~~~~ PRIVLEGED ROUTES ~~~~
64+
65+
4766@bp .route ("/" , methods = ["GET" , "POST" ])
4867def _index ():
4968 token = _require_admin ()
@@ -88,21 +107,6 @@ def _detail(user_id: int):
88107 return _render ("detail" , token , flags , user = user .as_dict )
89108
90109
91- def register_new_key (api_key : str , email : str ) -> str :
92- User .create_user (api_key = api_key , email = email )
93- return api_key
94-
95-
96- @bp .route ("/registration_form" , methods = ["GET" ])
97- def registration_form_redirect ():
98- # TODO: replace this with our own hosted registration form instead of external
99- return redirect (API_KEY_REGISTRATION_FORM_LINK , code = 302 )
100-
101- @bp .route ("/removal_request" , methods = ["GET" ])
102- def removal_request_redirect ():
103- # TODO: replace this with our own hosted form instead of external
104- return redirect (API_KEY_REMOVAL_REQUEST_LINK , code = 302 )
105-
106110@bp .route ("/register" , methods = ["POST" ])
107111def _register ():
108112 body = request .get_json ()
@@ -117,5 +121,16 @@ def _register():
117121 "User with email and/or API Key already exists, use different parameters or contact us for help" ,
118122 409 ,
119123 )
120- api_key = register_new_key (user_api_key , user_email )
121- return make_response (f"Successfully registered API key '{ api_key } '" , 200 )
124+ User .create_user (api_key = user_api_key , email = user_email )
125+ return make_response (f"Successfully registered API key '{ user_api_key } '" , 200 )
126+
127+
128+ @bp .route ("/diagnostics" , methods = ["GET" , "PUT" , "POST" , "DELETE" ])
129+ def diags ():
130+ # allows us to get useful diagnostic information written into server logs,
131+ # such as a full current "X-Forwarded-For" path as inserted into headers by intermediate proxies...
132+ # (but only when initiated purposefully by us to keep junk out of the logs)
133+ _require_admin ()
134+ log_info_with_request ("diagnostics" , headers = request .headers )
135+ response_text = f"request path: { request .headers .get ('X-Forwarded-For' , 'idk' )} "
136+ return make_response (response_text , 200 , {'content-type' : 'text/plain' })
0 commit comments