def post_to_remote_inbox(self, payload: str, to: str) -> None: if not current_app.config["AP_ENABLED"]: return # not federating if not enabled current_app.logger.debug(f"post_to_remote_inbox {payload}") ap_actor = json.loads(payload)["actor"] actor = Actor.query.filter(Actor.url == ap_actor).first() if not actor: current_app.logger.exception("no actor found") return key = Key(owner=actor.url) key.load(actor.private_key) signature_auth = HTTPSigAuth(key) # current_app.logger.debug(f"key=={key.__dict__}") try: current_app.logger.info("payload=%s", payload) current_app.logger.info("generating sig") signed_payload = json.loads(payload) backend = ap.get_backend() # Don't overwrite the signature if we're forwarding an activity if "signature" not in signed_payload: generate_signature(signed_payload, key) current_app.logger.info("to=%s", to) resp = requests.post( to, data=json.dumps(signed_payload), auth=signature_auth, headers={ "Content-Type": HEADERS[1], "Accept": HEADERS[1], "User-Agent": backend.user_agent() }, ) current_app.logger.info("resp=%s", resp) current_app.logger.info("resp_body=%s", resp.text) resp.raise_for_status() except HTTPError as err: current_app.logger.exception("request failed") if 400 >= err.response.status_code >= 499: current_app.logger.info("client error, no retry") return
def test_key_new_load(): owner = "http://lol.com" k = Key(owner) k.new() assert k.to_dict() == { "id": f"{owner}#main-key", "owner": owner, "publicKeyPem": k.pubkey_pem, } k2 = Key(owner) k2.load(k.privkey_pem) assert k2.to_dict() == k.to_dict()
def get_key(owner: str, _id: str, user: str, domain: str) -> Key: """"Loads or generates an RSA key.""" k = Key(owner, _id) user = user.replace(".", "_") domain = domain.replace(".", "_") key_path = os.path.join(KEY_DIR, f"key_{user}_{domain}.pem") if os.path.isfile(key_path): with open(key_path) as f: privkey_pem = f.read() k.load(privkey_pem) else: k.new() with open(key_path, "w") as f: f.write(k.privkey_pem) return k