capsul-flask/capsulflask/hub_api.py

23 lines
689 B
Python
Raw Normal View History

from flask import Blueprint
from flask import current_app
from flask import request
from werkzeug.exceptions import abort
from capsulflask.db import get_model, my_exec_info_message
2021-01-03 01:07:43 +00:00
bp = Blueprint("hub", __name__, url_prefix="/hub")
def authorized_for_host(id):
auth_header_value = request.headers.get('Authorization').replace("Bearer ", "")
return get_model().authorized_for_host(id, auth_header_value)
@bp.route("/heartbeat/<string:id>", methods=("POST"))
def heartbeat(id):
if authorized_for_host(id):
get_model().host_heartbeat(id)
else:
2021-01-03 01:07:43 +00:00
current_app.logger.info(f"/hub/heartbeat/{id} returned 401: invalid token")
return abort(401, "invalid host id or token")