Fix config wizard
This commit is contained in:
parent
d18bf7c7d5
commit
dfa6b6de3c
5 changed files with 20 additions and 25 deletions
|
@ -89,7 +89,7 @@ ME = {
|
||||||
"publicKey": {
|
"publicKey": {
|
||||||
"id": f"{config.ID}#main-key",
|
"id": f"{config.ID}#main-key",
|
||||||
"owner": config.ID,
|
"owner": config.ID,
|
||||||
"publicKeyPem": get_pubkey_as_pem(),
|
"publicKeyPem": get_pubkey_as_pem(config.KEY_PATH),
|
||||||
},
|
},
|
||||||
"alsoKnownAs": [],
|
"alsoKnownAs": [],
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,10 +23,10 @@ from sqlalchemy import select
|
||||||
|
|
||||||
from app import activitypub as ap
|
from app import activitypub as ap
|
||||||
from app import config
|
from app import config
|
||||||
|
from app.config import KEY_PATH
|
||||||
from app.database import AsyncSession
|
from app.database import AsyncSession
|
||||||
from app.database import get_db_session
|
from app.database import get_db_session
|
||||||
from app.key import Key
|
from app.key import Key
|
||||||
from app.key import get_key
|
|
||||||
|
|
||||||
_KEY_CACHE: MutableMapping[str, Key] = LFUCache(256)
|
_KEY_CACHE: MutableMapping[str, Key] = LFUCache(256)
|
||||||
|
|
||||||
|
@ -208,5 +208,5 @@ class HTTPXSigAuth(httpx.Auth):
|
||||||
|
|
||||||
|
|
||||||
k = Key(config.ID, f"{config.ID}#main-key")
|
k = Key(config.ID, f"{config.ID}#main-key")
|
||||||
k.load(get_key())
|
k.load(KEY_PATH.read_text())
|
||||||
auth = HTTPXSigAuth(k)
|
auth = HTTPXSigAuth(k)
|
||||||
|
|
23
app/key.py
23
app/key.py
|
@ -1,33 +1,24 @@
|
||||||
import base64
|
import base64
|
||||||
|
from pathlib import Path
|
||||||
from typing import Any
|
from typing import Any
|
||||||
|
|
||||||
from Crypto.PublicKey import RSA
|
from Crypto.PublicKey import RSA
|
||||||
from Crypto.Util import number
|
from Crypto.Util import number
|
||||||
|
|
||||||
from app.config import KEY_PATH
|
|
||||||
|
|
||||||
|
def generate_key(key_path: Path) -> None:
|
||||||
def key_exists() -> bool:
|
if key_path.exists():
|
||||||
return KEY_PATH.exists()
|
raise ValueError(f"Key at {key_path} already exists")
|
||||||
|
|
||||||
|
|
||||||
def generate_key() -> None:
|
|
||||||
if key_exists():
|
|
||||||
raise ValueError(f"Key at {KEY_PATH} already exists")
|
|
||||||
k = RSA.generate(2048)
|
k = RSA.generate(2048)
|
||||||
privkey_pem = k.exportKey("PEM").decode("utf-8")
|
privkey_pem = k.exportKey("PEM").decode("utf-8")
|
||||||
KEY_PATH.write_text(privkey_pem)
|
key_path.write_text(privkey_pem)
|
||||||
|
|
||||||
|
|
||||||
def get_pubkey_as_pem() -> str:
|
def get_pubkey_as_pem(key_path: Path) -> str:
|
||||||
text = KEY_PATH.read_text()
|
text = key_path.read_text()
|
||||||
return RSA.import_key(text).public_key().export_key("PEM").decode("utf-8")
|
return RSA.import_key(text).public_key().export_key("PEM").decode("utf-8")
|
||||||
|
|
||||||
|
|
||||||
def get_key() -> str:
|
|
||||||
return KEY_PATH.read_text()
|
|
||||||
|
|
||||||
|
|
||||||
class Key(object):
|
class Key(object):
|
||||||
DEFAULT_KEY_SIZE = 2048
|
DEFAULT_KEY_SIZE = 2048
|
||||||
|
|
||||||
|
|
|
@ -14,16 +14,16 @@ from app import activitypub as ap
|
||||||
from app import config
|
from app import config
|
||||||
from app import ldsig
|
from app import ldsig
|
||||||
from app import models
|
from app import models
|
||||||
|
from app.config import KEY_PATH
|
||||||
from app.database import AsyncSession
|
from app.database import AsyncSession
|
||||||
from app.database import SessionLocal
|
from app.database import SessionLocal
|
||||||
from app.database import now
|
from app.database import now
|
||||||
from app.key import Key
|
from app.key import Key
|
||||||
from app.key import get_key
|
|
||||||
|
|
||||||
_MAX_RETRIES = 16
|
_MAX_RETRIES = 16
|
||||||
|
|
||||||
k = Key(config.ID, f"{config.ID}#main-key")
|
k = Key(config.ID, f"{config.ID}#main-key")
|
||||||
k.load(get_key())
|
k.load(KEY_PATH.read_text())
|
||||||
|
|
||||||
|
|
||||||
async def new_outgoing_activity(
|
async def new_outgoing_activity(
|
||||||
|
@ -118,6 +118,8 @@ def process_next_outgoing_activity(db: Session) -> bool:
|
||||||
if retry_after_value := http_error.response.headers.get("Retry-After"):
|
if retry_after_value := http_error.response.headers.get("Retry-After"):
|
||||||
retry_after = _parse_retry_after(retry_after_value)
|
retry_after = _parse_retry_after(retry_after_value)
|
||||||
_set_next_try(next_activity, retry_after)
|
_set_next_try(next_activity, retry_after)
|
||||||
|
elif http_error.response.status_code == 401:
|
||||||
|
_set_next_try(next_activity)
|
||||||
elif 400 <= http_error.response.status_code < 500:
|
elif 400 <= http_error.response.status_code < 500:
|
||||||
logger.info(f"status_code={http_error.response.status_code} not retrying")
|
logger.info(f"status_code={http_error.response.status_code} not retrying")
|
||||||
next_activity.is_errored = True
|
next_activity.is_errored = True
|
||||||
|
|
|
@ -10,22 +10,24 @@ from markdown import markdown # type: ignore
|
||||||
from prompt_toolkit import prompt
|
from prompt_toolkit import prompt
|
||||||
|
|
||||||
from app.key import generate_key
|
from app.key import generate_key
|
||||||
from app.key import key_exists
|
|
||||||
|
_ROOT_DIR = Path().parent.resolve()
|
||||||
|
_KEY_PATH = _ROOT_DIR / "data" / "key.pem"
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
print("Welcome to microblog.pub setup wizard\n")
|
print("Welcome to microblog.pub setup wizard\n")
|
||||||
print("Generating key...")
|
print("Generating key...")
|
||||||
if key_exists():
|
if _KEY_PATH.exists():
|
||||||
yn = ""
|
yn = ""
|
||||||
while yn not in ["y", "n"]:
|
while yn not in ["y", "n"]:
|
||||||
yn = prompt(
|
yn = prompt(
|
||||||
"WARNING, a key already exists, overwrite it? (y/n): ", default="n"
|
"WARNING, a key already exists, overwrite it? (y/n): ", default="n"
|
||||||
).lower()
|
).lower()
|
||||||
if yn == "y":
|
if yn == "y":
|
||||||
generate_key()
|
generate_key(_KEY_PATH)
|
||||||
else:
|
else:
|
||||||
generate_key()
|
generate_key(_KEY_PATH)
|
||||||
|
|
||||||
config_file = Path("data/me.toml")
|
config_file = Path("data/me.toml")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue