File size: 2,244 Bytes
29baa44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
"""
Stateless bearer-token auth (works across the Vercel -> HF Spaces origin boundary, where
third-party cookies are often blocked), with the Flask session cookie as a fallback.
"""
import functools
import threading
import time
from collections import defaultdict, deque

from flask import current_app, g, jsonify, request, session
from itsdangerous import BadSignature, SignatureExpired, URLSafeTimedSerializer


def _serializer():
    return URLSafeTimedSerializer(current_app.config['SECRET_KEY'], salt='auth-token')


def issue_token(user_id):
    return _serializer().dumps({'uid': user_id})


def _user_from_token():
    header = request.headers.get('Authorization', '')
    if not header.startswith('Bearer '):
        return None
    try:
        data = _serializer().loads(header[7:], max_age=current_app.config['TOKEN_MAX_AGE'])
        return int(data['uid'])
    except (BadSignature, SignatureExpired, KeyError, ValueError):
        return None


def current_user_id():
    return _user_from_token() or session.get('user_id')


def login_required(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        uid = current_user_id()
        if not uid:
            return jsonify({'error': 'Not logged in'}), 401
        g.user_id = uid
        return fn(*args, **kwargs)
    return wrapper


class RateLimiter:
    """Small in-process sliding-window limiter for LLM-backed endpoints."""

    def __init__(self, limit, window_s):
        self.limit, self.window = limit, window_s
        self.hits = defaultdict(deque)
        self.lock = threading.Lock()

    def allow(self, key):
        now = time.monotonic()
        with self.lock:
            q = self.hits[key]
            while q and now - q[0] > self.window:
                q.popleft()
            if len(q) >= self.limit:
                return False
            q.append(now)
            return True


LLM_LIMITER = RateLimiter(limit=30, window_s=60)


def rate_limited(fn):
    @functools.wraps(fn)
    def wrapper(*args, **kwargs):
        if not LLM_LIMITER.allow(getattr(g, 'user_id', request.remote_addr)):
            return jsonify({'error': 'Too many requests, please slow down a little.'}), 429
        return fn(*args, **kwargs)
    return wrapper