2025-06-02 12:04:01 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
|
|
|
from flask import Blueprint, render_template, abort, request, Flask, current_app, session
|
2025-05-29 16:23:20 +08:00
|
|
|
import os
|
2025-06-02 12:04:01 +08:00
|
|
|
|
|
|
|
from modules.WireguardConfiguration import WireguardConfiguration
|
|
|
|
from modules.DashboardConfig import DashboardConfig
|
2025-05-29 16:23:20 +08:00
|
|
|
|
2025-05-31 22:59:46 +08:00
|
|
|
def ResponseObject(status=True, message=None, data=None, status_code = 200) -> Flask.response_class:
|
|
|
|
response = Flask.make_response(current_app, {
|
|
|
|
"status": status,
|
|
|
|
"message": message,
|
|
|
|
"data": data
|
|
|
|
})
|
|
|
|
response.status_code = status_code
|
|
|
|
response.content_type = "application/json"
|
|
|
|
return response
|
|
|
|
|
2025-06-02 12:04:01 +08:00
|
|
|
def login_required(f):
|
|
|
|
@wraps(f)
|
|
|
|
def func(*args, **kwargs):
|
2025-06-03 03:02:06 +08:00
|
|
|
if session.get("username") is None or session.get("totpVerified") is None or not session.get("totpVerified") or session.get("role") != "client":
|
2025-06-02 12:04:01 +08:00
|
|
|
return ResponseObject(False, "Unauthorized access.", data=None, status_code=401)
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return func
|
2025-05-31 22:59:46 +08:00
|
|
|
|
2025-06-02 12:04:01 +08:00
|
|
|
def createClientBlueprint(wireguardConfigurations: dict[WireguardConfiguration], dashboardConfig: DashboardConfig):
|
|
|
|
from modules.DashboardClients import DashboardClients
|
|
|
|
DashboardClients = DashboardClients()
|
|
|
|
client = Blueprint('client', __name__, template_folder=os.path.abspath("./static/client/dist"))
|
|
|
|
prefix = f'{dashboardConfig.GetConfig("Server", "app_prefix")[1]}/client'
|
|
|
|
|
|
|
|
|
|
|
|
@client.before_request
|
|
|
|
def clientBeforeRequest():
|
|
|
|
if request.method.lower() == 'options':
|
|
|
|
return ResponseObject(True)
|
|
|
|
|
|
|
|
|
|
|
|
@client.post(f'{prefix}/api/signup')
|
|
|
|
def ClientAPI_SignUp():
|
|
|
|
data = request.json
|
|
|
|
status, msg = DashboardClients.SignUp(**data)
|
2025-06-02 19:23:04 +08:00
|
|
|
return ResponseObject(status, msg)
|
2025-06-02 12:04:01 +08:00
|
|
|
|
2025-06-02 19:23:04 +08:00
|
|
|
@client.post(f'{prefix}/api/signin')
|
|
|
|
def ClientAPI_SignIn():
|
|
|
|
data = request.json
|
|
|
|
status, msg = DashboardClients.SignIn(**data)
|
2025-06-03 03:02:06 +08:00
|
|
|
if status:
|
|
|
|
session['username'] = data.get('Email')
|
|
|
|
session['role'] = 'client'
|
|
|
|
session['totpVerified'] = False
|
|
|
|
return ResponseObject(status, msg)
|
2025-06-03 17:18:18 +08:00
|
|
|
|
|
|
|
@client.get(f'{prefix}/api/signout')
|
|
|
|
def ClientAPI_SignOut():
|
|
|
|
session.pop('username')
|
|
|
|
session.pop('role')
|
|
|
|
session.pop('totpVerified')
|
|
|
|
return ResponseObject(True)
|
2025-06-03 03:02:06 +08:00
|
|
|
|
|
|
|
@client.get(f'{prefix}/api/signin/totp')
|
|
|
|
def ClientAPI_SignIn_TOTP():
|
|
|
|
token = request.args.get('Token', None)
|
|
|
|
if not token:
|
|
|
|
return ResponseObject(False, "Please provide TOTP token")
|
|
|
|
|
|
|
|
status, msg = DashboardClients.SignIn_GetTotp(token)
|
2025-06-02 12:04:01 +08:00
|
|
|
return ResponseObject(status, msg)
|
2025-06-02 19:23:04 +08:00
|
|
|
|
2025-06-03 03:02:06 +08:00
|
|
|
@client.post(f'{prefix}/api/signin/totp')
|
|
|
|
def ClientAPI_SignIn_ValidateTOTP():
|
|
|
|
data = request.json
|
|
|
|
token = data.get('Token', None)
|
|
|
|
userProvidedTotp = data.get('UserProvidedTOTP', None)
|
|
|
|
if not all([token, userProvidedTotp]):
|
|
|
|
return ResponseObject(False, "Please fill in all fields")
|
|
|
|
status, msg = DashboardClients.SignIn_GetTotp(token, userProvidedTotp)
|
|
|
|
if status:
|
|
|
|
if session.get('username') is None:
|
|
|
|
return ResponseObject(False, "Sign in status is invalid", status_code=401)
|
|
|
|
session['totpVerified'] = True
|
2025-06-03 23:37:43 +08:00
|
|
|
# return ResponseObject(True, data=)
|
2025-06-03 03:02:06 +08:00
|
|
|
return ResponseObject(status, msg)
|
|
|
|
|
2025-06-02 19:23:04 +08:00
|
|
|
@client.get(prefix)
|
|
|
|
def ClientIndex():
|
|
|
|
return render_template('client.html')
|
2025-06-02 12:04:01 +08:00
|
|
|
|
2025-06-03 03:02:06 +08:00
|
|
|
@client.get(f'{prefix}/api/validateAuthentication')
|
|
|
|
@login_required
|
|
|
|
def ClientAPI_ValidateAuthentication():
|
|
|
|
return ResponseObject(True)
|
|
|
|
|
|
|
|
@client.get(f'{prefix}/api/configurations')
|
|
|
|
@login_required
|
|
|
|
def ClientAPI_Configurations():
|
|
|
|
return ResponseObject(True, "Ping Pong!")
|
|
|
|
|
2025-06-02 12:04:01 +08:00
|
|
|
return client
|