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
+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())
|
||||
Reference in new issue
Block a user