fetch key from WKD

This commit is contained in:
missytake 2025-04-05 12:24:32 +02:00
parent 6899223ad3
commit 3a8f2228bd
Signed by: missytake
GPG key ID: 04CC6658320518DF
5 changed files with 70 additions and 6 deletions

View file

@ -18,6 +18,8 @@ python_requires = >=3.11
install_requires =
deltachat-rpc-client
deltachat-rpc-server
wkdhash
requests
[options.packages.find]
where = src
@ -26,6 +28,10 @@ where = src
console_scripts =
keyserver-bot = keyserver_bot.hooks:main
[options.extras_require]
dev =
pytest
[tox:tox]
envlist = lint, py311, py312
isolated_build = True

View file

@ -1,11 +1,10 @@
#!/usr/bin/env python3
"""Minimal echo bot example.
it will echo back any text send to it, it also will print to console all Delta Chat core events.
Pass --help to the CLI to see available options.
"""
from deltachat_rpc_client import events, run_bot_cli
import wkd
import koo
import vcard
hooks = events.HookCollection()
@ -17,7 +16,19 @@ def log_event(event):
@hooks.on(events.NewMessage)
def echo(event):
snapshot = event.message_snapshot
snapshot.chat.send_text(snapshot.text)
email = snapshot
domain = email.split("@")[1]
public_key = wkd.request_by_email(email, domain)
if not public_key:
public_key = wkd.request_by_email(email, f"openpgpkey.{domain}")
if not public_key:
public_key = wkd.request_by_email(email, "wkd.keys.openpgp.org")
if not public_key:
snapshot.chat.send_text("Sorry, I could not find a key for this user.")
return
vcard_path = vcard.save_vcard(public_key)
snapshot.chat.send_file(vcard_path)
def main():

View file

@ -0,0 +1,16 @@
def construct_vcard(email: str, public_key: str) -> str:
"""
:param email:
:param public_key:
:return:
"""
def save_vcard(content: str) -> str:
"""
:param content: the VCard content
:return: the file path in the blob dir
"""

18
src/keyserver_bot/wkd.py Normal file
View file

@ -0,0 +1,18 @@
import wkdhash
import requests
import base64
def request_by_email(email: str, server: str) -> str:
"""Request the public key from WKD by email
:param email: an RFC 5322 email address
:return: the public key of the user, or "" if it isn't found
"""
localpart, domain = email.split("@")
wkd_hash = wkdhash.userid_to_wkd_hash(email)
wkd_url = f"https://{server}/.well-known/openpgpkey/{domain}/hu/{wkd_hash}?l={localpart}"
r = requests.get(wkd_url)
if b"The requested URL was not found on this server." in r.content:
return ""
return base64.b64encode(r.content).decode().strip()

13
tests/test_wkd.py Normal file
View file

@ -0,0 +1,13 @@
import pytest
import keyserver_bot.wkd
@pytest.mark.parametrize(
("email", "server", "public_key"),
[
("sdjif2mlij@protonmail.com", "openpgpkey.protonmail.com", "xjMEZ+/HahYJKwYBBAHaRw8BAQdAzRZOcQEkaPGwUYBVHsKTVX+4nP05ZwLQ3wYUod1IQyjNNXNkamlmMm1saWpAcHJvdG9ubWFpbC5jb20gPHNkamlmMm1saWpAcHJvdG9ubWFpbC5jb20+wsARBBMWCgCDBYJn78dqAwsJBwmQ9od3MCAJzKNFFAAAAAAAHAAgc2FsdEBub3RhdGlvbnMub3BlbnBncGpzLm9yZ3e7fGtn2OkXwNFoBOAEgwoRygRlwr41fcO4LsJ/1HcyAxUKCAQWAAIBAhkBApsDAh4BFiEERU5l1enHSVCTOUnc9od3MCAJzKMAAAuPAQCQ0PFlbngJRtJ36yloWfHwTY168TDlMGeL+nAxJ1GUQAEA31H+NukIwEzCwQWh7aoFeY9eJHXVoaeh0swfvJA5yQ/CwB4EEBYIAJAFgmfvx6cFgwDtTgAJENgGwa9ZeOjHNRQAAAAAABwAEHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmfU5CAfgrGEZMQfk/lTyXT2LBxvcGVucGdwLWNhQHByb3Rvbi5tZSA8b3BlbnBncC1jYUBwcm90b24ubWU+FiEECoZS/l1TOGBXiZ/p2AbBr1l46McAAJCrAP4tMWMSLKAykI/JnR+94aE2+E4dCTslkW/svb1o7zGLbwEAg/fo6sOxa3wHr9xnCbUUrxZ0Dnh/21zJ3atL6zMpYQHOOARn78dqEgorBgEEAZdVAQUBAQdATm5zMhEn2/S+UB3qKfeV424+iyvIr2p3FEzxuzUQKQEDAQgHwr4EGBYKAHAFgmfvx2oJkPaHdzAgCcyjRRQAAAAAABwAIHNhbHRAbm90YXRpb25zLm9wZW5wZ3Bqcy5vcmf94HNvbCT5aX/ifcaDK2bIjzmS69JdQo1AcOt3X8AwFQKbDBYhBEVOZdXpx0lQkzlJ3PaHdzAgCcyjAACVUgEA8Agw7R9+a72WsKOq691JIYPGDfNYCKNHFllsQAfHHHYA/3x+rG9YQTbdAuuFi1ciGvgo9DAWVv8Bt7ibBqEVAuQN"),
("missytake@systemli.org", "openpgpkey.systemli.org", "")
],
)
def test_request_by_email(email, server, public_key):
assert public_key == keyserver_bot.wkd.request_by_email(email, server)