chore: import MCTF services
- Added all seven issued service trees without omissions. - Preserved the supplied runtime configuration, binaries, and assets.
This commit is contained in:
commit
13f936bee9
1222 files changed
+21280
No files matched your search
Executable
+5
@@ -0,0 +1,5 @@
|
||||
from flask import current_app
|
||||
|
||||
|
||||
def ledger():
|
||||
return current_app.extensions["ledger"]
|
||||
Executable
+8
@@ -0,0 +1,8 @@
|
||||
from flask import abort, request
|
||||
|
||||
|
||||
def json_object():
|
||||
payload = request.get_json(silent=True)
|
||||
if not isinstance(payload, dict):
|
||||
abort(400, "Expected a JSON object")
|
||||
return payload
|
||||
Executable
+68
@@ -0,0 +1,68 @@
|
||||
import re
|
||||
import sqlite3
|
||||
|
||||
from uuid import uuid4
|
||||
from flask import Blueprint, abort, current_app, g
|
||||
from werkzeug.security import check_password_hash, generate_password_hash
|
||||
from backend.app.common.utils import account_namespace, utc_now, valid_text
|
||||
from backend.app.infrastructure.persistence import repositories
|
||||
from backend.app.infrastructure.persistence.connection import database
|
||||
from backend.app.api.request import json_object
|
||||
from backend.app.api.session import owner_session, sign_in
|
||||
|
||||
|
||||
USERNAME = re.compile(r"[A-Za-z0-9_-]{3,32}")
|
||||
BAD_CREDENTIALS = "Invalid username or password"
|
||||
|
||||
|
||||
bp = Blueprint("auth", __name__)
|
||||
|
||||
|
||||
@bp.post("/register")
|
||||
def register():
|
||||
payload = json_object()
|
||||
username = payload.get("username")
|
||||
password = payload.get("password")
|
||||
if not isinstance(username, str) or not USERNAME.fullmatch(username):
|
||||
abort(400, "Username must contain 3-32 ASCII letters, digits, _ or -")
|
||||
if not valid_text(password, 8, 128):
|
||||
abort(400, "Password must contain 8-128 ASCII characters")
|
||||
user_id = str(uuid4())
|
||||
namespace = account_namespace(username)
|
||||
password_hash = generate_password_hash(password, method="scrypt")
|
||||
connection = database()
|
||||
try:
|
||||
with connection:
|
||||
repositories.create_user(connection, user_id, username, namespace, password_hash, utc_now().isoformat())
|
||||
except sqlite3.IntegrityError:
|
||||
abort(409, "Username is already taken")
|
||||
return sign_in(user_id, username), 201
|
||||
|
||||
@bp.post("/login")
|
||||
def login():
|
||||
payload = json_object()
|
||||
username = payload.get("username")
|
||||
password = payload.get("password")
|
||||
valid_username = isinstance(username, str) and USERNAME.fullmatch(username)
|
||||
valid_password = valid_text(password, 8, 128)
|
||||
row = repositories.get_user_by_username(database(), username if valid_username else "")
|
||||
correct = check_password_hash(
|
||||
row["password_hash"] if row else current_app.extensions["ledger"].dummy_hash,
|
||||
password if valid_password else "",
|
||||
)
|
||||
if not correct or row is None or not valid_password:
|
||||
abort(401, BAD_CREDENTIALS)
|
||||
return sign_in(row["id"], row["username"])
|
||||
|
||||
@bp.get("/home/api/me")
|
||||
@owner_session
|
||||
def me():
|
||||
return g.user
|
||||
|
||||
@bp.delete("/home/api/logout")
|
||||
@owner_session
|
||||
def logout():
|
||||
connection = database()
|
||||
with connection:
|
||||
repositories.delete_auth_session(connection, g.token_id)
|
||||
return "", 204
|
||||
+49
@@ -0,0 +1,49 @@
|
||||
from flask import Blueprint, g, request, abort
|
||||
from backend.app.infrastructure.persistence import repositories
|
||||
from backend.app.infrastructure.persistence.connection import database
|
||||
from backend.app.application import implants
|
||||
from backend.app.api.request import json_object
|
||||
from backend.app.api.context import ledger
|
||||
from backend.app.api.session import owner_session, claimed_username
|
||||
|
||||
|
||||
bp = Blueprint("implants", __name__, url_prefix="/home/api")
|
||||
|
||||
|
||||
@bp.post("/register_implant")
|
||||
@owner_session
|
||||
def register_implant():
|
||||
return implants.register(database(), ledger(), g.user["id"], json_object()), 201
|
||||
|
||||
@bp.get("/implants")
|
||||
@owner_session
|
||||
def list_implants():
|
||||
return implants.list_owned(database(), g.user["id"])
|
||||
|
||||
@bp.get("/telemetry")
|
||||
@owner_session
|
||||
def telemetry():
|
||||
username = request.args.get("username") or g.user["username"]
|
||||
if claimed_username(g.token) != username:
|
||||
return {"error": "access denied"}, 403
|
||||
user = repositories.get_user_by_username(database(), username)
|
||||
if user is None:
|
||||
abort(404)
|
||||
return implants.telemetry(database(), user["id"])
|
||||
|
||||
@bp.get("/implants/<implant_id>")
|
||||
@owner_session
|
||||
def implant_passport(implant_id):
|
||||
return implants.passport(database(), ledger(), implant_id, g.user["id"])
|
||||
|
||||
@bp.route("/check_certificate/<implant_id>/check", methods=["GET", "POST"])
|
||||
@owner_session
|
||||
def check_certificate(implant_id):
|
||||
payload = json_object() if request.method == "POST" else None
|
||||
return implants.check_certificate(database(), ledger(), implant_id, g.user["id"], payload)
|
||||
|
||||
|
||||
@bp.post("/reissue/<implant_id>")
|
||||
@owner_session
|
||||
def reissue_certificate(implant_id):
|
||||
return implants.reissue(database(), ledger(), implant_id, json_object()), 200
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
from flask import Blueprint
|
||||
from backend.app.infrastructure.persistence.connection import database
|
||||
from backend.app.application import implants
|
||||
from backend.app.api.session import owner_session
|
||||
from backend.app.api.context import ledger
|
||||
|
||||
|
||||
bp = Blueprint("registry", __name__, url_prefix="/home/api")
|
||||
|
||||
|
||||
@bp.get("/registry")
|
||||
@owner_session
|
||||
def registry():
|
||||
return implants.registry(database(), ledger())
|
||||
Executable
+66
@@ -0,0 +1,66 @@
|
||||
from functools import wraps
|
||||
from flask import abort, g, request
|
||||
from obsigil import Obsigil, ObsigilError, generate_uuid7
|
||||
|
||||
from backend.app.api.context import ledger
|
||||
from backend.app.common.constants import AUTH_AUDIENCE, AUTH_LIFETIME
|
||||
from backend.app.common.utils import utc_now
|
||||
from backend.app.infrastructure.persistence import repositories
|
||||
from backend.app.infrastructure.persistence.connection import database
|
||||
|
||||
|
||||
def sign_in(user_id, username):
|
||||
now = int(utc_now().timestamp())
|
||||
expires_at = now + int(AUTH_LIFETIME.total_seconds())
|
||||
tid = generate_uuid7()
|
||||
token = Obsigil.mint(
|
||||
clauses={}, mandate_key=ledger().obsigil_key, tid=tid,
|
||||
sub=user_id, aud=[AUTH_AUDIENCE], exp=expires_at,
|
||||
manifest={"iss": AUTH_AUDIENCE, "claims": {"username": username}},
|
||||
).token()
|
||||
connection = database()
|
||||
with connection:
|
||||
repositories.delete_auth_session(connection, g.token_id)
|
||||
repositories.create_auth_session(connection, tid, user_id, expires_at)
|
||||
return {"id": user_id, "username": username, "access_token": token}
|
||||
|
||||
def claimed_username(token):
|
||||
claims = Obsigil(token, max_decoded_len=4096).claims() or {}
|
||||
username = claims.get("username")
|
||||
return username if isinstance(username, str) else None
|
||||
|
||||
def load_identity():
|
||||
g.user = None
|
||||
g.token = None
|
||||
g.token_id = None
|
||||
authorization = request.headers.get("Authorization", "").split()
|
||||
if len(authorization) != 2 or authorization[0].lower() != "bearer":
|
||||
return
|
||||
token = authorization[1]
|
||||
if len(token) > 4096:
|
||||
return
|
||||
now = int(utc_now().timestamp())
|
||||
try:
|
||||
clauses = Obsigil(
|
||||
token, keys=ledger().obsigil_key, audience=AUTH_AUDIENCE,
|
||||
now=now, max_decoded_len=4096,
|
||||
).clauses()
|
||||
except ObsigilError:
|
||||
return
|
||||
user_id = clauses.get("sub")
|
||||
if not isinstance(user_id, str) or clauses.get("aud") != [AUTH_AUDIENCE]:
|
||||
return
|
||||
row = repositories.get_auth_user(database(), clauses["tid"], user_id, clauses["exp"], now)
|
||||
if row is not None:
|
||||
g.user = dict(row)
|
||||
g.token = token
|
||||
g.token_id = clauses["tid"]
|
||||
|
||||
|
||||
def owner_session(route):
|
||||
@wraps(route)
|
||||
def guarded(*args, **kwargs):
|
||||
if g.user is None:
|
||||
abort(401, "Authentication required")
|
||||
return route(*args, **kwargs)
|
||||
return guarded
|
||||
Executable
+36
@@ -0,0 +1,36 @@
|
||||
from pathlib import Path
|
||||
from flask import Blueprint, abort, make_response, send_from_directory
|
||||
from werkzeug.security import safe_join
|
||||
from backend.app.api.context import ledger
|
||||
|
||||
|
||||
bp = Blueprint("console", __name__)
|
||||
|
||||
|
||||
@bp.get("/", defaults={"path": ""})
|
||||
@bp.get("/<path:path>")
|
||||
def console(path):
|
||||
static_root = ledger().static_root
|
||||
public_root = static_root / "public"
|
||||
if path.startswith("src/"):
|
||||
source_path = path.removeprefix("src/")
|
||||
source_root = static_root / "src"
|
||||
target = safe_join(str(source_root), source_path)
|
||||
if target and Path(target).is_file():
|
||||
return send_from_directory(source_root, source_path)
|
||||
abort(404)
|
||||
target = safe_join(str(public_root), path)
|
||||
if path and path != "index.html" and target and Path(target).is_file():
|
||||
return send_from_directory(public_root, path)
|
||||
pages = {"", "login", "home", "home/register_implant", "home/check_certificate", "home/reissue", "home/implants", "home/registry"}
|
||||
if path not in pages and not (
|
||||
(path.startswith("home/implants/") and len(path.split("/")) == 3)
|
||||
or (path.startswith("home/check_certificate/") and len(path.split("/")) == 4 and path.endswith("/check"))
|
||||
):
|
||||
abort(404)
|
||||
index = public_root / "index.html"
|
||||
if not index.is_file():
|
||||
abort(503, "Console not found")
|
||||
response = make_response(index.read_text(encoding="utf-8"))
|
||||
response.headers["Cache-Control"] = "no-store"
|
||||
return response
|
||||
Reference in new issue
Block a user