Download auth/middleware.py from QBANK/OFTQUEST: direct link, hf CLI and curl.
- Browser
- Download file 1.32 kB
-
https://huggingface.co/spaces/QBANK/OFTQUEST/resolve/main/auth/middleware.py
- Command line
-
hf download hf://spaces/QBANK/OFTQUEST/auth/middleware.py
-
curl -L -o middleware.py https://huggingface.co/spaces/QBANK/OFTQUEST/resolve/main/auth/middleware.py
1.32 kB
| from functools import wraps | |
| from flask import request, jsonify, g | |
| from auth.session_manager import validate_session | |
| def require_auth(f): | |
| """Decorator that requires a valid session token.""" | |
| def decorated(*args, **kwargs): | |
| token = None | |
| auth_header = request.headers.get('Authorization', '') | |
| if auth_header.startswith('Bearer '): | |
| token = auth_header[7:] | |
| if not token: | |
| token = request.args.get('token') | |
| if not token: | |
| token = request.cookies.get('mm_token') | |
| if not token: | |
| return jsonify({'error': 'Token de autenticação não fornecido'}), 401 | |
| user, session = validate_session(token) | |
| if not user: | |
| return jsonify({'error': 'Sessão inválida ou expirada. Faça login novamente.'}), 401 | |
| g.current_user = dict(user) | |
| g.current_session = dict(session) | |
| g.token = token | |
| return f(*args, **kwargs) | |
| return decorated | |
| def require_admin(f): | |
| """Decorator that requires admin role.""" | |
| def decorated(*args, **kwargs): | |
| if g.current_user.get('role') != 'admin': | |
| return jsonify({'error': 'Acesso restrito a administradores'}), 403 | |
| return f(*args, **kwargs) | |
| return decorated | |