sio-2425/delivery1/server/utils/data_checks.py

28 lines
940 B
Python
Raw Normal View History

import json
from flask import jsonify
from services import SessionService, OrganizationService
from models import Session
def validate_session_file(data) -> tuple | Session:
"""
Check if the session file is valid, and return the session object if it is
:param data: session file data (json)
:return: Session object or error response
"""
if "token" not in data:
return jsonify({"error": "No session token"}), 400
session_token = data["token"]
session = SessionService.get_session(session_token)
if not session:
return jsonify({"error": "Not authenticated"}), 401
org = OrganizationService.get_organization(session.org_id)
if not org:
return jsonify({"error": "Organization not found"}), 404
status = OrganizationService.get_user_status(org, session.user_id)
if status != "active":
return jsonify({"error": "User is not active"}), 403
return session