File size: 10,101 Bytes
ea1fa95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2920fe6
 
 
 
 
 
ea1fa95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04eeb91
ea1fa95
 
 
 
 
 
 
 
 
 
 
 
 
04eeb91
ea1fa95
 
 
 
 
 
 
 
04eeb91
 
 
 
 
 
 
ea1fa95
 
 
 
04eeb91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea1fa95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04eeb91
ea1fa95
 
 
 
 
 
 
 
 
 
 
04eeb91
ea1fa95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04eeb91
 
 
 
 
ea1fa95
 
04eeb91
 
ea1fa95
 
 
 
 
 
 
 
 
04eeb91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ea1fa95
 
 
04eeb91
ea1fa95
 
 
04eeb91
 
ea1fa95
 
 
 
 
04eeb91
 
 
 
 
 
ea1fa95
 
 
 
 
 
 
04eeb91
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
"""Per-visitor OAuth for the Space UI (HF/5).

PKCE lives here, not on Hub MCP tools. Tokens stay in process memory keyed by
the Gradio session. They are never written to MCP responses or Space Secrets.
"""

from __future__ import annotations

import base64
import hashlib
import json
import os
import secrets
import time
from dataclasses import dataclass
from typing import Any
from urllib.parse import urlencode

import httpx

OAUTH_ISSUER = "https://re-port-flow.com/api/v1"
# Same AS the product MCP uses. Tokens issued here are what content-service
# already accepts from reportflow-mcp.
MCP_AS = "https://mcp.re-port-flow.com"
MCP_RESOURCE = "https://mcp.re-port-flow.com"
API_BASE = "https://api.re-port-flow.com"
# The Space registers itself through DCR (register_public_client), so the
# authorization server caps it at the scopes a dynamically registered client
# may hold. Asking for anything outside that set (designs:read, templates:*)
# makes the authorize step fail with scope_not_allowed. Keep this in step with
# SUPPORTED_SCOPES in ../src/config.ts.
SCOPE = "openid profile designs:write pdf:generate"
CLIENT_NAME = "reportflow-readme-space"
APP_KEY = "reportflow-mcp"


class OAuthError(Exception):
    """User-visible OAuth failure. Must not include token material."""


@dataclass
class TokenSet:
    access_token: str
    refresh_token: str | None
    expires_at: float
    workspace_id: str | None


@dataclass
class PendingAuth:
    verifier: str
    created_at: float


_pending: dict[str, PendingAuth] = {}
_sessions: dict[str, TokenSet] = {}
_aliases: dict[str, str] = {}
_client_id: str | None = None


def _b64url(data: bytes) -> str:
    return base64.urlsafe_b64encode(data).decode("ascii").rstrip("=")


def pkce_pair() -> tuple[str, str]:
    verifier = secrets.token_urlsafe(64)[:64]
    challenge = _b64url(hashlib.sha256(verifier.encode("ascii")).digest())
    return verifier, challenge


def _jwt_payload(access_token: str) -> dict[str, Any] | None:
    parts = access_token.split(".")
    if len(parts) < 2:
        return None
    pad = "=" * (-len(parts[1]) % 4)
    try:
        payload = json.loads(base64.urlsafe_b64decode(parts[1] + pad))
    except (ValueError, json.JSONDecodeError):
        return None
    return payload if isinstance(payload, dict) else None


def decode_workspace_id(access_token: str) -> str | None:
    payload = _jwt_payload(access_token)
    if not payload:
        return None
    ws = payload.get("workspace_id")
    return ws if isinstance(ws, str) and ws else None


def _token_expires_at(access_token: str, expires_in: Any) -> float:
    payload = _jwt_payload(access_token)
    exp = payload.get("exp") if payload else None
    if isinstance(exp, (int, float)):
        return max(float(exp) - 60.0, time.time() + 30.0)
    ttl = float(expires_in) if isinstance(expires_in, (int, float)) else 3600.0
    return time.time() + max(ttl - 60.0, 30.0)


def resolve_session(session_id: str, visitor_id: str | None = None) -> str:
    """Prefer the browser-stored visitor id so a new Gradio hash still finds tokens."""
    if visitor_id and visitor_id in _sessions:
        if session_id:
            _aliases[session_id] = visitor_id
        return visitor_id
    if session_id in _sessions:
        return session_id
    alias = _aliases.get(session_id)
    if alias and alias in _sessions:
        return alias
    return visitor_id or session_id


def redirect_uri() -> str:
    host = (os.environ.get("SPACE_HOST") or "").strip()
    if host:
        return f"https://{host}/"
    return "http://127.0.0.1:7860/"


def register_public_client(client: httpx.Client | None = None) -> str:
    """DCR public client. Scope is explicit (omit → openid profile only)."""
    global _client_id
    if _client_id:
        return _client_id
    own = client is None
    http = client or httpx.Client(timeout=15.0)
    body = {
        "client_name": CLIENT_NAME,
        "redirect_uris": [redirect_uri()],
        "grant_types": ["authorization_code", "refresh_token"],
        "token_endpoint_auth_method": "none",
        "scope": SCOPE,
    }
    try:
        response = http.post(
            f"{MCP_AS}/register",
            json=body,
            headers={"Accept": "application/json"},
        )
        response.raise_for_status()
        payload = response.json()
    except httpx.HTTPError as err:
        raise OAuthError("Could not register the Space as an OAuth client.") from err
    finally:
        if own:
            http.close()
    client_id = payload.get("client_id")
    if not isinstance(client_id, str) or not client_id:
        raise OAuthError("OAuth client registration returned no client_id.")
    _client_id = client_id
    return client_id


def start_authorization() -> str:
    verifier, challenge = pkce_pair()
    state = secrets.token_urlsafe(24)
    _pending[state] = PendingAuth(verifier=verifier, created_at=time.time())
    client_id = register_public_client()
    query = urlencode(
        {
            "response_type": "code",
            "client_id": client_id,
            "redirect_uri": redirect_uri(),
            "scope": SCOPE,
            "state": state,
            "code_challenge": challenge,
            "code_challenge_method": "S256",
            "resource": MCP_RESOURCE,
        }
    )
    return f"{MCP_AS}/authorize?{query}"


def _store_tokens(session_id: str, payload: dict[str, Any]) -> TokenSet:
    access = payload.get("access_token")
    if not isinstance(access, str) or not access:
        raise OAuthError("Token response had no access_token.")
    refresh = payload.get("refresh_token")
    expires_in = payload.get("expires_in")
    stored = TokenSet(
        access_token=access,
        refresh_token=refresh if isinstance(refresh, str) else None,
        expires_at=_token_expires_at(access, expires_in),
        workspace_id=decode_workspace_id(access),
    )
    _sessions[session_id] = stored
    return stored


def finish_authorization(
    session_id: str,
    code: str,
    state: str,
    client: httpx.Client | None = None,
    visitor_id: str | None = None,
) -> TokenSet:
    if not session_id:
        raise OAuthError("No Gradio session — cannot attach tokens to a visitor.")
    pending = _pending.pop(state, None)
    if pending is None or time.time() - pending.created_at > 600:
        raise OAuthError("Sign-in expired or state did not match. Start again.")
    own = client is None
    http = client or httpx.Client(timeout=15.0)
    try:
        response = http.post(
            f"{MCP_AS}/token",
            data={
                "grant_type": "authorization_code",
                "code": code,
                "redirect_uri": redirect_uri(),
                "client_id": register_public_client(http),
                "code_verifier": pending.verifier,
                "resource": MCP_RESOURCE,
            },
            headers={"Accept": "application/json"},
        )
        response.raise_for_status()
        payload = response.json()
    except httpx.HTTPError as err:
        raise OAuthError("Could not exchange the authorization code.") from err
    finally:
        if own:
            http.close()
    if not isinstance(payload, dict):
        raise OAuthError("Token response was not JSON.")
    vid = visitor_id.strip() if isinstance(visitor_id, str) and visitor_id.strip() else secrets.token_urlsafe(24)
    stored = _store_tokens(vid, payload)
    if session_id:
        _aliases[session_id] = vid
    return stored


def session_status(session_id: str, visitor_id: str | None = None) -> dict[str, Any]:
    token = _sessions.get(resolve_session(session_id, visitor_id))
    if token is None:
        return {"signedIn": False}
    return {
        "signedIn": True,
        "workspaceId": token.workspace_id,
        "expiresSoon": time.time() >= token.expires_at,
    }


def _refresh(key: str, token: TokenSet, client: httpx.Client | None = None) -> TokenSet:
    if not token.refresh_token:
        _sessions.pop(key, None)
        raise OAuthError("Session expired. Sign in again.")
    own = client is None
    http = client or httpx.Client(timeout=15.0)
    try:
        response = http.post(
            f"{MCP_AS}/token",
            data={
                "grant_type": "refresh_token",
                "refresh_token": token.refresh_token,
                "client_id": register_public_client(http),
                "resource": MCP_RESOURCE,
            },
            headers={"Accept": "application/json"},
        )
        response.raise_for_status()
        payload = response.json()
    except httpx.HTTPError as err:
        raise OAuthError("Session expired. Sign in again.") from err
    finally:
        if own:
            http.close()
    if not isinstance(payload, dict):
        raise OAuthError("Session expired. Sign in again.")
    if not payload.get("refresh_token"):
        payload = {**payload, "refresh_token": token.refresh_token}
    return _store_tokens(key, payload)


def access_token_for(
    session_id: str,
    visitor_id: str | None = None,
    client: httpx.Client | None = None,
) -> str:
    key = resolve_session(session_id, visitor_id)
    token = _sessions.get(key)
    if token is None:
        raise OAuthError("Sign in with Re:port Flow first.")
    if time.time() >= token.expires_at:
        token = _refresh(key, token, client=client)
    return token.access_token


def workspace_id_for(session_id: str, visitor_id: str | None = None) -> str:
    token = _sessions.get(resolve_session(session_id, visitor_id))
    if token is None or not token.workspace_id:
        raise OAuthError("Sign in and pick a workspace on the consent screen.")
    return token.workspace_id


def sign_out(session_id: str, visitor_id: str | None = None) -> None:
    keys = {session_id, visitor_id, _aliases.get(session_id)}
    for key in keys:
        if key:
            _sessions.pop(key, None)
    _aliases.pop(session_id, None)


def reset_client_cache() -> None:
    global _client_id
    _client_id = None
    _pending.clear()
    _sessions.clear()
    _aliases.clear()