Implement api keys in modules/rest_api.py
This commit is contained in:
parent
096fd05cee
commit
a533228112
1 changed files with 24 additions and 8 deletions
|
@ -1,5 +1,6 @@
|
||||||
import http.server, json, threading, urllib.parse
|
import http.server, json, threading, uuid, urllib.parse
|
||||||
import flask
|
import flask
|
||||||
|
from src import utils
|
||||||
|
|
||||||
_bot = None
|
_bot = None
|
||||||
_events = None
|
_events = None
|
||||||
|
@ -13,14 +14,18 @@ class Handler(http.server.BaseHTTPRequestHandler):
|
||||||
response = ""
|
response = ""
|
||||||
code = 404
|
code = 404
|
||||||
|
|
||||||
if parsed.path.startswith("/api/"):
|
if not "key" in get_params or not _bot.get_setting(
|
||||||
_, _, endpoint = parsed.path[1:].partition("/")
|
"api-key-%s" % get_params["key"][0], False):
|
||||||
response = _events.on("api").on(endpoint).call_for_result(
|
code = 401
|
||||||
params=get_params)
|
else:
|
||||||
|
if parsed.path.startswith("/api/"):
|
||||||
|
_, _, endpoint = parsed.path[1:].partition("/")
|
||||||
|
response = _events.on("api").on(endpoint).call_for_result(
|
||||||
|
params=get_params, path=endpoint.split("/"))
|
||||||
|
|
||||||
if response:
|
if response:
|
||||||
response = json.dumps(response, sort_keys=True, indent=4)
|
response = json.dumps(response, sort_keys=True, indent=4)
|
||||||
code = 200
|
code = 200
|
||||||
|
|
||||||
self.send_response(code)
|
self.send_response(code)
|
||||||
self.send_header("Content-type", "application/json")
|
self.send_header("Content-type", "application/json")
|
||||||
|
@ -49,3 +54,14 @@ class Module(object):
|
||||||
|
|
||||||
def unload(self):
|
def unload(self):
|
||||||
self.httpd.shutdown()
|
self.httpd.shutdown()
|
||||||
|
|
||||||
|
@utils.hook("received.command.apikey", private_only=True)
|
||||||
|
def api_key(self, event):
|
||||||
|
"""
|
||||||
|
:help: Generate a new API key
|
||||||
|
:permission: api-key
|
||||||
|
:prefix: APIKey
|
||||||
|
"""
|
||||||
|
api_key = str(uuid.uuid4())
|
||||||
|
self.bot.set_setting("api-key-%s" % api_key, True)
|
||||||
|
event["stdout"].write(api_key)
|
||||||
|
|
Loading…
Reference in a new issue