mirror of
https://git.sr.ht/~tsileo/microblog.pub
synced 2024-11-14 02:34:27 +00:00
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": {
|
||||
"id": f"{config.ID}#main-key",
|
||||
"owner": config.ID,
|
||||
"publicKeyPem": get_pubkey_as_pem(),
|
||||
"publicKeyPem": get_pubkey_as_pem(config.KEY_PATH),
|
||||
},
|
||||
"alsoKnownAs": [],
|
||||
}
|
||||
|
|
|
@ -23,10 +23,10 @@ from sqlalchemy import select
|
|||
|
||||
from app import activitypub as ap
|
||||
from app import config
|
||||
from app.config import KEY_PATH
|
||||
from app.database import AsyncSession
|
||||
from app.database import get_db_session
|
||||
from app.key import Key
|
||||
from app.key import get_key
|
||||
|
||||
_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.load(get_key())
|
||||
k.load(KEY_PATH.read_text())
|
||||
auth = HTTPXSigAuth(k)
|
||||
|
|
23
app/key.py
23
app/key.py
|
@ -1,33 +1,24 @@
|
|||
import base64
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
from Crypto.PublicKey import RSA
|
||||
from Crypto.Util import number
|
||||
|
||||
from app.config import KEY_PATH
|
||||
|
||||
|
||||
def key_exists() -> bool:
|
||||
return KEY_PATH.exists()
|
||||
|
||||
|
||||
def generate_key() -> None:
|
||||
if key_exists():
|
||||
raise ValueError(f"Key at {KEY_PATH} already exists")
|
||||
def generate_key(key_path: Path) -> None:
|
||||
if key_path.exists():
|
||||
raise ValueError(f"Key at {key_path} already exists")
|
||||
k = RSA.generate(2048)
|
||||
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:
|
||||
text = KEY_PATH.read_text()
|
||||
def get_pubkey_as_pem(key_path: Path) -> str:
|
||||
text = key_path.read_text()
|
||||
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):
|
||||
DEFAULT_KEY_SIZE = 2048
|
||||
|
||||
|
|
|
@ -14,16 +14,16 @@ from app import activitypub as ap
|
|||
from app import config
|
||||
from app import ldsig
|
||||
from app import models
|
||||
from app.config import KEY_PATH
|
||||
from app.database import AsyncSession
|
||||
from app.database import SessionLocal
|
||||
from app.database import now
|
||||
from app.key import Key
|
||||
from app.key import get_key
|
||||
|
||||
_MAX_RETRIES = 16
|
||||
|
||||
k = Key(config.ID, f"{config.ID}#main-key")
|
||||
k.load(get_key())
|
||||
k.load(KEY_PATH.read_text())
|
||||
|
||||
|
||||
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"):
|
||||
retry_after = _parse_retry_after(retry_after_value)
|
||||
_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:
|
||||
logger.info(f"status_code={http_error.response.status_code} not retrying")
|
||||
next_activity.is_errored = True
|
||||
|
|
|
@ -10,22 +10,24 @@ from markdown import markdown # type: ignore
|
|||
from prompt_toolkit import prompt
|
||||
|
||||
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:
|
||||
print("Welcome to microblog.pub setup wizard\n")
|
||||
print("Generating key...")
|
||||
if key_exists():
|
||||
if _KEY_PATH.exists():
|
||||
yn = ""
|
||||
while yn not in ["y", "n"]:
|
||||
yn = prompt(
|
||||
"WARNING, a key already exists, overwrite it? (y/n): ", default="n"
|
||||
).lower()
|
||||
if yn == "y":
|
||||
generate_key()
|
||||
generate_key(_KEY_PATH)
|
||||
else:
|
||||
generate_key()
|
||||
generate_key(_KEY_PATH)
|
||||
|
||||
config_file = Path("data/me.toml")
|
||||
|
||||
|
|
Loading…
Reference in a new issue