Implement direct WKD URLs

This commit is contained in:
s3lph 2023-01-30 21:27:30 +01:00
parent 35f38ef188
commit fddbea70d9

View file

@ -8,14 +8,22 @@ from bottle import get, run, response, request, HTTPError
CORS = ('Access-Control-Allow-Origin', '*')
def abort(code, text):
err = HTTPError(code, text)
err.add_header(*CORS)
return err
def get_domain_header():
domain = request.get_header('host')
if len(domain) != 1 or domain[0] is None:
abort(400, 'Bad Request')
return domain[0]
@get('/.well-known/openpgpkey/<domain>/submission-address')
def submission_address(domain: str):
def advanced_submission_address(domain: str):
if domain not in Config.domains:
abort(404, 'Not Found')
response.add_header('Content-Type', 'text/plain')
@ -23,8 +31,13 @@ def submission_address(domain: str):
return make_submission_address_file(domain)
@get('/.well-known/openpgpkey/submission-address')
def direct_submission_address():
return advanced_submission_address(get_domain_header())
@get('/.well-known/openpgpkey/<domain>/policy')
def policy(domain: str):
def advanced_policy(domain: str):
if domain not in Config.domains:
abort(404, 'Not Found')
response.add_header('Content-Type', 'text/plain')
@ -32,8 +45,13 @@ def policy(domain: str):
return make_policy_file(domain)
@get('/.well-known/openpgpkey/policy')
def direct_policy():
return advanced_policy(get_domain_header())
@get('/.well-known/openpgpkey/<domain>/hu/<userhash>')
def hu(domain: str, userhash: str):
def advanced_hu(domain: str, userhash: str):
if domain not in Config.domains:
abort(404, 'Not Found')
if Config.httpd['require_user_urlparam']:
@ -49,6 +67,11 @@ def hu(domain: str, userhash: str):
abort(404, 'Not Found')
@get('/.well-known/openpgpkey/hu/<userhash>')
def direct_hu(userhash: str):
return advanced_hu(get_domain_header(), userhash)
def run_server():
run(host=Config.httpd['host'], port=Config.httpd['port'])