COSCUP-ap-demo/demo/utils/checker.py

49 lines
1.2 KiB
Python
Raw Normal View History

2023-06-13 08:14:28 +02:00
"""Request checker"""
2023-06-19 08:34:03 +02:00
import json
2023-06-13 08:11:29 +02:00
from flask import Request, abort
2023-06-19 08:34:03 +02:00
from demo.httpsig import HttpSignature, SignedData
from demo.actor import fetch_actor
2023-06-13 08:11:29 +02:00
def inbox_prechecker(
request: Request,
) -> bool:
2023-06-13 08:14:28 +02:00
"""Inbox request prechecker"""
2023-06-19 08:34:03 +02:00
payload = request.headers
ap_body = request.data
2023-06-13 08:11:29 +02:00
try:
parsec_signature = HttpSignature.parse_signature(
payload["signature"]
)
except KeyError:
abort(401, "Missing signature key!")
2023-06-19 08:34:03 +02:00
actor_id = request.get_json()["actor"]
actor = fetch_actor(actor_id)
try:
pub_key = actor["publicKey"]["publicKeyPem"]
2023-06-19 08:55:56 +02:00
except json.JSONDecodeError as exc:
raise ValueError from exc
except KeyError as exc:
2023-06-19 08:34:03 +02:00
print("actore gone?")
2023-06-19 08:55:56 +02:00
raise KeyError from exc
2023-06-19 08:34:03 +02:00
sigdate = SignedData(
method = request.method,
path = request.path,
signed_list = parsec_signature["headers"],
body_digest = HttpSignature.calculation_digest(ap_body),
headers = request.headers,
)
is_verify = HttpSignature.verify_signature(
HttpSignature.build_signature_string(sigdate),
parsec_signature["signature"],
pub_key,
)
print(is_verify)
2023-06-13 08:11:29 +02:00
return True