feat: add simple license activation server
This commit is contained in:
1 parent
b2db5ed499
commit
05be83d289
17 files changed
+1053
-6
No files matched your search
@@ -168,6 +168,44 @@ def test_install_license_rejects_invalid_text(tmp_path):
|
||||
assert not target_path.exists()
|
||||
|
||||
|
||||
def test_activate_license_code_installs_server_license(tmp_path, monkeypatch):
|
||||
from douyin.license import license_manager
|
||||
from tools.license_dev import create_dev_license
|
||||
|
||||
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
||||
source_path = tmp_path / "server_license.dat"
|
||||
target_path = tmp_path / "license.dat"
|
||||
create_dev_license(
|
||||
output_path=source_path,
|
||||
machine_fingerprint="machine-a",
|
||||
expires_at=datetime.now(timezone.utc) + timedelta(days=30),
|
||||
customer="online-user",
|
||||
)
|
||||
server_doc = json.loads(source_path.read_text(encoding="utf-8"))
|
||||
calls = {}
|
||||
|
||||
def fake_post(url, payload, timeout_seconds=10):
|
||||
calls["url"] = url
|
||||
calls["payload"] = payload
|
||||
calls["timeout_seconds"] = timeout_seconds
|
||||
return {"ok": True, "license": server_doc, "reason": "ok", "message": "授权有效"}
|
||||
|
||||
result = license_manager.install_license_or_activate(
|
||||
"DG-AAAA-BBBB-CCCC",
|
||||
target_path,
|
||||
activation_url="https://license.example.test/api/v1/activate",
|
||||
http_post=fake_post,
|
||||
)
|
||||
|
||||
assert result.ok is True
|
||||
assert target_path.exists()
|
||||
assert calls["payload"] == {
|
||||
"card_key": "DG-AAAA-BBBB-CCCC",
|
||||
"machine_fingerprint": "machine-a",
|
||||
}
|
||||
assert license_manager.check_license(target_path).payload["customer"] == "online-user"
|
||||
|
||||
|
||||
def test_license_status_public_payload_hides_signature(tmp_path, monkeypatch):
|
||||
from douyin.license import license_manager
|
||||
from tools.license_dev import create_dev_license
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def test_license_server_api_activates_card(tmp_path):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from license_server import api
|
||||
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
|
||||
|
||||
db_path = tmp_path / "license.db"
|
||||
service = LicenseService(db_path=db_path, private_key_pem=DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=5, count=1, customer="api-user")[0]
|
||||
client = TestClient(api.create_app(service=service))
|
||||
|
||||
health = client.get("/api/v1/health")
|
||||
resp = client.post(
|
||||
"/api/v1/activate",
|
||||
json={"card_key": card.code, "machine_fingerprint": "machine-api"},
|
||||
)
|
||||
|
||||
assert health.status_code == 200
|
||||
assert health.json()["ok"] is True
|
||||
assert resp.status_code == 200
|
||||
body = resp.json()
|
||||
assert body["ok"] is True
|
||||
assert body["license"]["payload"]["customer"] == "api-user"
|
||||
assert body["license"]["payload"]["machine_fingerprint"] == "machine-api"
|
||||
|
||||
|
||||
def test_license_server_api_rejects_unknown_card(tmp_path):
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from license_server import api
|
||||
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
|
||||
|
||||
service = LicenseService(db_path=tmp_path / "license.db", private_key_pem=DEV_PRIVATE_KEY_PEM)
|
||||
client = TestClient(api.create_app(service=service))
|
||||
|
||||
resp = client.post(
|
||||
"/api/v1/activate",
|
||||
json={"card_key": "DG-NOPE-NOPE-NOPE", "machine_fingerprint": "machine-api"},
|
||||
)
|
||||
|
||||
assert resp.status_code == 404
|
||||
assert resp.json()["reason"] == "not_found"
|
||||
@@ -0,0 +1,63 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from datetime import datetime, timezone
|
||||
|
||||
|
||||
def test_license_server_creates_and_activates_card(tmp_path, monkeypatch):
|
||||
from douyin.license import license_manager
|
||||
from license_server import core
|
||||
|
||||
monkeypatch.setattr(license_manager, "get_machine_fingerprint", lambda: "machine-a")
|
||||
db_path = tmp_path / "license.db"
|
||||
service = core.LicenseService(db_path=db_path, private_key_pem=core.DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=30, count=1, customer="alice")[0]
|
||||
|
||||
result = service.activate(card.code, machine_fingerprint="machine-a")
|
||||
|
||||
assert result.ok is True
|
||||
assert result.license_doc is not None
|
||||
status = license_manager.check_license(result.write_license(tmp_path / "license.dat"))
|
||||
assert status.ok is True
|
||||
assert status.payload["customer"] == "alice"
|
||||
assert status.payload["machine_fingerprint"] == "machine-a"
|
||||
|
||||
|
||||
def test_license_server_rejects_revoked_card(tmp_path):
|
||||
from license_server import core
|
||||
|
||||
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=7, count=1)[0]
|
||||
service.revoke(card.code)
|
||||
|
||||
result = service.activate(card.code, machine_fingerprint="machine-a")
|
||||
|
||||
assert result.ok is False
|
||||
assert result.reason == "revoked"
|
||||
|
||||
|
||||
def test_license_server_verify_reports_bound_activation(tmp_path):
|
||||
from license_server import core
|
||||
|
||||
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=3, count=1, customer="bob")[0]
|
||||
service.activate(card.code, machine_fingerprint="machine-b")
|
||||
|
||||
result = service.verify(card.code, machine_fingerprint="machine-b")
|
||||
|
||||
assert result.ok is True
|
||||
assert result.reason == "ok"
|
||||
assert result.expires_at is not None
|
||||
assert result.expires_at > datetime.now(timezone.utc)
|
||||
|
||||
|
||||
def test_license_server_verify_rejects_other_machine(tmp_path):
|
||||
from license_server import core
|
||||
|
||||
service = core.LicenseService(db_path=tmp_path / "license.db", private_key_pem=core.DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=3, count=1)[0]
|
||||
service.activate(card.code, machine_fingerprint="machine-a")
|
||||
|
||||
result = service.verify(card.code, machine_fingerprint="machine-b")
|
||||
|
||||
assert result.ok is False
|
||||
assert result.reason == "machine_mismatch"
|
||||
@@ -0,0 +1,40 @@
|
||||
from __future__ import annotations
|
||||
|
||||
|
||||
def test_manage_create_outputs_cards(tmp_path, capsys):
|
||||
from license_server import manage
|
||||
|
||||
rc = manage.main(
|
||||
[
|
||||
"--db",
|
||||
str(tmp_path / "license.db"),
|
||||
"create",
|
||||
"--days",
|
||||
"9",
|
||||
"--count",
|
||||
"2",
|
||||
"--customer",
|
||||
"tester",
|
||||
]
|
||||
)
|
||||
|
||||
out = capsys.readouterr().out
|
||||
assert rc == 0
|
||||
assert out.count("DG-") == 2
|
||||
assert "days=9" in out
|
||||
assert "customer=tester" in out
|
||||
|
||||
|
||||
def test_manage_revoke_disables_card(tmp_path, capsys):
|
||||
from license_server import manage
|
||||
from license_server.core import DEV_PRIVATE_KEY_PEM, LicenseService
|
||||
|
||||
db_path = tmp_path / "license.db"
|
||||
service = LicenseService(db_path=db_path, private_key_pem=DEV_PRIVATE_KEY_PEM)
|
||||
card = service.create_cards(days=1, count=1)[0]
|
||||
|
||||
rc = manage.main(["--db", str(db_path), "revoke", card.code])
|
||||
|
||||
assert rc == 0
|
||||
assert "revoked" in capsys.readouterr().out
|
||||
assert service.activate(card.code, machine_fingerprint="machine-a").reason == "revoked"
|
||||
@@ -139,11 +139,11 @@ def test_license_install_route_accepts_code(monkeypatch):
|
||||
message = "授权有效"
|
||||
payload = {"customer": "demo"}
|
||||
|
||||
def fake_install(text):
|
||||
def fake_install(text, **_kwargs):
|
||||
calls["text"] = text
|
||||
return _Status()
|
||||
|
||||
monkeypatch.setattr(routes, "install_license_text", fake_install)
|
||||
monkeypatch.setattr(routes, "install_license_or_activate", fake_install)
|
||||
|
||||
status, obj = _post("/api/license", {"license_text": "DYGJ-demo"}, monkeypatch)
|
||||
|
||||
@@ -153,6 +153,39 @@ def test_license_install_route_accepts_code(monkeypatch):
|
||||
assert calls["text"] == "DYGJ-demo"
|
||||
|
||||
|
||||
def test_license_install_route_uses_activation_url(monkeypatch):
|
||||
calls = {}
|
||||
|
||||
class _Status:
|
||||
ok = True
|
||||
reason = "ok"
|
||||
message = "授权有效"
|
||||
payload = {}
|
||||
|
||||
def fake_install(text, *, activation_url="", timeout_seconds=10):
|
||||
calls["text"] = text
|
||||
calls["activation_url"] = activation_url
|
||||
calls["timeout_seconds"] = timeout_seconds
|
||||
return _Status()
|
||||
|
||||
monkeypatch.setattr(
|
||||
routes,
|
||||
"read_config",
|
||||
lambda: {"license": {"activation_url": "https://license.example/api/v1/activate", "timeout_seconds": 7}},
|
||||
)
|
||||
monkeypatch.setattr(routes, "install_license_or_activate", fake_install)
|
||||
|
||||
status, obj = _post("/api/license", {"license_text": "DG-DEMO"}, monkeypatch)
|
||||
|
||||
assert status == 200
|
||||
assert obj["ok"] is True
|
||||
assert calls == {
|
||||
"text": "DG-DEMO",
|
||||
"activation_url": "https://license.example/api/v1/activate",
|
||||
"timeout_seconds": 7,
|
||||
}
|
||||
|
||||
|
||||
def test_license_install_route_rejects_empty_code(monkeypatch):
|
||||
status, obj = _post("/api/license", {"license_text": " "}, monkeypatch)
|
||||
|
||||
|
||||
Reference in new issue
Block a user