mirror of
https://github.com/PR0M3TH3AN/SeedPass.git
synced 2025-09-09 15:58:48 +00:00
Allow selecting fingerprint for API server
This commit is contained in:
@@ -25,10 +25,18 @@ def _check_token(auth: str | None) -> None:
|
|||||||
raise HTTPException(status_code=401, detail="Unauthorized")
|
raise HTTPException(status_code=401, detail="Unauthorized")
|
||||||
|
|
||||||
|
|
||||||
def start_server() -> str:
|
def start_server(fingerprint: str | None = None) -> str:
|
||||||
"""Initialize global state and return the API token."""
|
"""Initialize global state and return the API token.
|
||||||
|
|
||||||
|
Parameters
|
||||||
|
----------
|
||||||
|
fingerprint:
|
||||||
|
Optional seed profile fingerprint to select before starting the server.
|
||||||
|
"""
|
||||||
global _pm, _token
|
global _pm, _token
|
||||||
_pm = PasswordManager()
|
_pm = PasswordManager()
|
||||||
|
if fingerprint:
|
||||||
|
_pm.select_fingerprint(fingerprint)
|
||||||
_token = secrets.token_urlsafe(16)
|
_token = secrets.token_urlsafe(16)
|
||||||
print(f"API token: {_token}")
|
print(f"API token: {_token}")
|
||||||
origins = [
|
origins = [
|
||||||
|
@@ -183,15 +183,15 @@ def generate_password(ctx: typer.Context, length: int = 24) -> None:
|
|||||||
|
|
||||||
|
|
||||||
@api_app.command("start")
|
@api_app.command("start")
|
||||||
def api_start(host: str = "127.0.0.1", port: int = 8000) -> None:
|
def api_start(ctx: typer.Context, host: str = "127.0.0.1", port: int = 8000) -> None:
|
||||||
"""Start the SeedPass API server."""
|
"""Start the SeedPass API server."""
|
||||||
token = api_module.start_server()
|
token = api_module.start_server(ctx.obj.get("fingerprint"))
|
||||||
typer.echo(f"API token: {token}")
|
typer.echo(f"API token: {token}")
|
||||||
uvicorn.run(api_module.app, host=host, port=port)
|
uvicorn.run(api_module.app, host=host, port=port)
|
||||||
|
|
||||||
|
|
||||||
@api_app.command("stop")
|
@api_app.command("stop")
|
||||||
def api_stop(host: str = "127.0.0.1", port: int = 8000) -> None:
|
def api_stop(ctx: typer.Context, host: str = "127.0.0.1", port: int = 8000) -> None:
|
||||||
"""Stop the SeedPass API server."""
|
"""Stop the SeedPass API server."""
|
||||||
import requests
|
import requests
|
||||||
|
|
||||||
|
@@ -149,3 +149,19 @@ def test_generate_password(monkeypatch):
|
|||||||
assert result.exit_code == 0
|
assert result.exit_code == 0
|
||||||
assert called.get("length") == 12
|
assert called.get("length") == 12
|
||||||
assert "secretpw" in result.stdout
|
assert "secretpw" in result.stdout
|
||||||
|
|
||||||
|
|
||||||
|
def test_api_start_passes_fingerprint(monkeypatch):
|
||||||
|
"""Ensure the API start command forwards the selected fingerprint."""
|
||||||
|
called = {}
|
||||||
|
|
||||||
|
def fake_start(fp=None):
|
||||||
|
called["fp"] = fp
|
||||||
|
return "tok"
|
||||||
|
|
||||||
|
monkeypatch.setattr(cli.api_module, "start_server", fake_start)
|
||||||
|
monkeypatch.setattr(cli, "uvicorn", SimpleNamespace(run=lambda *a, **k: None))
|
||||||
|
|
||||||
|
result = runner.invoke(app, ["--fingerprint", "abc", "api", "start"])
|
||||||
|
assert result.exit_code == 0
|
||||||
|
assert called.get("fp") == "abc"
|
||||||
|
Reference in New Issue
Block a user