Add docstrings and improve formatting in server.py

This commit is contained in:
√(noham)²
2025-12-13 15:07:12 +01:00
parent 523f5f1576
commit 915a758b17

View File

@@ -1,3 +1,4 @@
"""Flask server for license activation and verification."""
from datetime import datetime from datetime import datetime
import os import os
import uuid import uuid
@@ -12,19 +13,26 @@ app = Flask(__name__)
PENDING_ACTIVATIONS = {} PENDING_ACTIVATIONS = {}
def _b64url_encode(data: bytes) -> str: def _b64url_encode(data: bytes) -> str:
"""Base64 URL-safe encode data."""
return base64.urlsafe_b64encode(data).decode("ascii").rstrip("=") return base64.urlsafe_b64encode(data).decode("ascii").rstrip("=")
def _make_jwt_like(payload: dict) -> str: def _make_jwt_like(payload: dict) -> str:
"""Create a JWT-like token."""
header = {"alg": "HS256", "typ": "JWT"} header = {"alg": "HS256", "typ": "JWT"}
header_b64 = _b64url_encode(json.dumps(header, separators=(",", ":"), ensure_ascii=False).encode("utf-8")) header_json = json.dumps(header, separators=(",", ":"),
payload_b64 = _b64url_encode(json.dumps(payload, separators=(",", ":"), ensure_ascii=False).encode("utf-8")) ensure_ascii=False).encode("utf-8")
header_b64 = _b64url_encode(header_json)
payload_json = json.dumps(payload, separators=(",", ":"),
ensure_ascii=False).encode("utf-8")
payload_b64 = _b64url_encode(payload_json)
signature_b64 = _b64url_encode(b"signature") signature_b64 = _b64url_encode(b"signature")
return f"{header_b64}.{payload_b64}.{signature_b64}" return f"{header_b64}.{payload_b64}.{signature_b64}"
@app.route("/api/v1/license/activate", methods=["POST"]) @app.route("/api/v1/license/activate", methods=["POST"])
def activate_license(): def activate_license():
"""Handle license activation request."""
payload = request.get_json(silent=True) or {} payload = request.get_json(silent=True) or {}
print("Received activation request:", payload) print("Received activation request:", payload)
@@ -61,8 +69,10 @@ def activate_license():
@app.route("/api/v1/license/activate/<activation_id>", methods=["POST"]) @app.route("/api/v1/license/activate/<activation_id>", methods=["POST"])
def verify_activation_otp(activation_id: str): def verify_activation_otp(activation_id: str):
"""Verify activation OTP and return license token."""
payload = request.get_json(silent=True) or {} payload = request.get_json(silent=True) or {}
print("Received OTP verification:", {"activationId": activation_id, **payload}) print("Received OTP verification:",
{"activationId": activation_id, **payload})
pending = PENDING_ACTIVATIONS.get(activation_id) pending = PENDING_ACTIVATIONS.get(activation_id)
if not pending: if not pending:
@@ -75,7 +85,8 @@ def verify_activation_otp(activation_id: str):
"deviceId": pending.get("deviceId"), "deviceId": pending.get("deviceId"),
"deviceName": pending.get("deviceName"), "deviceName": pending.get("deviceName"),
"licenseServerUrl": pending.get("licenseServerUrl"), "licenseServerUrl": pending.get("licenseServerUrl"),
"plan": "ULTIMATE_EDITION", # ["PRO_EDITION","GOLDEN_EDITION","ULTIMATE_EDITION"] # ["PRO_EDITION","GOLDEN_EDITION","ULTIMATE_EDITION"]
"plan": "ULTIMATE_EDITION",
"type": "personal", "type": "personal",
"createdAt": pending.get("activatedAt"), "createdAt": pending.get("activatedAt"),
"updatedAt": now_iso, "updatedAt": now_iso,
@@ -92,6 +103,7 @@ def verify_activation_otp(activation_id: str):
@app.route("/api/v1/license/verify", methods=["POST"]) @app.route("/api/v1/license/verify", methods=["POST"])
def verify_license(): def verify_license():
"""Verify license token."""
payload = request.get_json(silent=True) or {} payload = request.get_json(silent=True) or {}
print("Received license verification:", payload) print("Received license verification:", payload)
@@ -106,6 +118,7 @@ def verify_license():
def create_app(): def create_app():
"""Create and return Flask app."""
return app return app