Skip to content

Flask Flask-SQLAlchemy python-dotenv Werkzeug pyotp qrcode[pil] cryptography #2

Description

@Coinbeas

import pyotp
from flask import Blueprint, request, jsonify

from backend.models import User

api = Blueprint("api", name)

@api.post("/admin/2fa/verify")
def verify_admin_2fa():
data = request.get_json() or {}

user_id = data.get("user_id")
code = str(data.get("code", "")).strip()

user = User.query.get(user_id)

if not user or not user.is_admin:
    return jsonify({
        "error": "Admin account required"
    }), 403

if not user.two_fa or not user.two_fa.enabled:
    return jsonify({
        "error": "Admin 2FA setup is required"
    }), 403

secret = decrypt_secret(user.two_fa.encrypted_secret)

if not pyotp.TOTP(secret).verify(code, valid_window=1):
    return jsonify({
        "error": "Invalid 2FA code"
    }), 401

return jsonify({
    "status": "verified",
    "message": "Admin 2FA verification successful"
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions