Allow API endpoints to not request authentication
This commit is contained in:
parent
f31fdc48b3
commit
e5f475cdeb
1 changed files with 22 additions and 15 deletions
|
@ -13,25 +13,32 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
query = parsed.query
|
query = parsed.query
|
||||||
get_params = urllib.parse.parse_qs(query)
|
get_params = urllib.parse.parse_qs(query)
|
||||||
|
|
||||||
|
_, _, endpoint = parsed.path[1:].partition("/")
|
||||||
|
endpoint, _, args = endpoint.partition("/")
|
||||||
|
args = list(filter(None, args.split("/")))
|
||||||
|
|
||||||
response = ""
|
response = ""
|
||||||
code = 404
|
code = 404
|
||||||
|
|
||||||
if not "key" in get_params or not _bot.get_setting(
|
hooks = _events.on("api").on(endpoint).get_hooks()
|
||||||
"api-key-%s" % get_params["key"][0], False):
|
if hooks:
|
||||||
code = 401
|
hook = hooks[0]
|
||||||
else:
|
authenticated = hook.get_kwarg("authenticated", True)
|
||||||
if parsed.path.startswith("/api/"):
|
key = get_params.get("key", None)
|
||||||
_, _, endpoint = parsed.path[1:].partition("/")
|
print(key)
|
||||||
endpoint, _, args = endpoint.partition("/")
|
if authenticated and (
|
||||||
args = list(filter(None, args.split("/")))
|
not key or
|
||||||
|
not _bot.get_setting("api-key-%s" % key[0], False)):
|
||||||
|
code = 401
|
||||||
|
else:
|
||||||
|
if parsed.path.startswith("/api/"):
|
||||||
|
response = _events.on("api").on(endpoint
|
||||||
|
).call_for_result(params=get_params, path=args)
|
||||||
|
|
||||||
response = _events.on("api").on(endpoint).call_for_result(
|
if response:
|
||||||
params=get_params, path=args)
|
response = json.dumps(response, sort_keys=True,
|
||||||
|
indent=4)
|
||||||
if response:
|
code = 200
|
||||||
response = json.dumps(response, sort_keys=True,
|
|
||||||
indent=4)
|
|
||||||
code = 200
|
|
||||||
|
|
||||||
self.send_response(code)
|
self.send_response(code)
|
||||||
self.send_header("Content-type", "application/json")
|
self.send_header("Content-type", "application/json")
|
||||||
|
|
Loading…
Reference in a new issue