databar 2.9.0__tar.gz → 2.10.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {databar-2.9.0/src/databar.egg-info → databar-2.10.1}/PKG-INFO +5 -1
- {databar-2.9.0 → databar-2.10.1}/README.md +4 -0
- {databar-2.9.0 → databar-2.10.1}/pyproject.toml +1 -1
- {databar-2.9.0 → databar-2.10.1}/src/databar/__init__.py +1 -1
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/_guide.py +2 -0
- databar-2.10.1/src/databar/cli/_oauth.py +288 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/flows.py +11 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/tasks.py +36 -2
- {databar-2.9.0 → databar-2.10.1}/src/databar/client.py +11 -2
- {databar-2.9.0 → databar-2.10.1}/src/databar/models.py +36 -3
- {databar-2.9.0 → databar-2.10.1/src/databar.egg-info}/PKG-INFO +5 -1
- {databar-2.9.0 → databar-2.10.1}/tests/test_cli.py +26 -2
- {databar-2.9.0 → databar-2.10.1}/tests/test_client.py +54 -0
- databar-2.10.1/tests/test_oauth_login.py +176 -0
- databar-2.9.0/src/databar/cli/_oauth.py +0 -190
- databar-2.9.0/tests/test_oauth_login.py +0 -61
- {databar-2.9.0 → databar-2.10.1}/LICENSE +0 -0
- {databar-2.9.0 → databar-2.10.1}/setup.cfg +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/__init__.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/_auth.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/_onboard.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/_output.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/app.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/enrichments.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/tables.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/waterfalls.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/cli/workbooks.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar/exceptions.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar.egg-info/SOURCES.txt +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar.egg-info/dependency_links.txt +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar.egg-info/entry_points.txt +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar.egg-info/requires.txt +0 -0
- {databar-2.9.0 → databar-2.10.1}/src/databar.egg-info/top_level.txt +0 -0
- {databar-2.9.0 → databar-2.10.1}/tests/test_new_features.py +0 -0
- {databar-2.9.0 → databar-2.10.1}/tests/test_workbooks.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: databar
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.10.1
|
|
4
4
|
Summary: Official Databar.ai Python SDK and CLI — connect to enrichments, waterfalls, and tables via api.databar.ai
|
|
5
5
|
Author-email: "Databar.ai Team" <info@databar.ai>
|
|
6
6
|
License: MIT License
|
|
@@ -409,6 +409,9 @@ status.progress # {"total": 2, "completed": 1, "failed": 0, "processing
|
|
|
409
409
|
# Rows that have already finished, without waiting for the rest
|
|
410
410
|
client.get_task(task.task_id, include_partial=True).data
|
|
411
411
|
|
|
412
|
+
# A flow task: what each step did, and which Output step (branch) answered
|
|
413
|
+
client.get_task(task.task_id, include_trace=True).trace["returned_by"]
|
|
414
|
+
|
|
412
415
|
# Stop it — unfinished requests are refunded, finished rows keep their results
|
|
413
416
|
client.cancel_task(task.task_id)
|
|
414
417
|
```
|
|
@@ -592,6 +595,7 @@ databar task get <task-id> --poll
|
|
|
592
595
|
|
|
593
596
|
# Collect the rows a running bulk task has already finished
|
|
594
597
|
databar task get <task-id> --partial
|
|
598
|
+
databar task get <task-id> --trace # flow task: steps and the Output step that answered
|
|
595
599
|
|
|
596
600
|
# Stop a running task; finished rows keep their results
|
|
597
601
|
databar task cancel <task-id>
|
|
@@ -354,6 +354,9 @@ status.progress # {"total": 2, "completed": 1, "failed": 0, "processing
|
|
|
354
354
|
# Rows that have already finished, without waiting for the rest
|
|
355
355
|
client.get_task(task.task_id, include_partial=True).data
|
|
356
356
|
|
|
357
|
+
# A flow task: what each step did, and which Output step (branch) answered
|
|
358
|
+
client.get_task(task.task_id, include_trace=True).trace["returned_by"]
|
|
359
|
+
|
|
357
360
|
# Stop it — unfinished requests are refunded, finished rows keep their results
|
|
358
361
|
client.cancel_task(task.task_id)
|
|
359
362
|
```
|
|
@@ -537,6 +540,7 @@ databar task get <task-id> --poll
|
|
|
537
540
|
|
|
538
541
|
# Collect the rows a running bulk task has already finished
|
|
539
542
|
databar task get <task-id> --partial
|
|
543
|
+
databar task get <task-id> --trace # flow task: steps and the Output step that answered
|
|
540
544
|
|
|
541
545
|
# Stop a running task; finished rows keep their results
|
|
542
546
|
databar task cancel <task-id>
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "databar"
|
|
7
|
-
version = "2.
|
|
7
|
+
version = "2.10.1"
|
|
8
8
|
description = "Official Databar.ai Python SDK and CLI — connect to enrichments, waterfalls, and tables via api.databar.ai"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = { file = "LICENSE" }
|
|
@@ -173,6 +173,7 @@ are rejected.
|
|
|
173
173
|
databar task get <task-id> --format json # check once; bulk tasks report progress
|
|
174
174
|
databar task get <task-id> --poll # poll until complete
|
|
175
175
|
databar task get <task-id> --partial # rows a running bulk task already finished
|
|
176
|
+
databar task get <task-id> --trace # flow task: what each step did, which Output step answered
|
|
176
177
|
databar task cancel <task-id> # stop it; finished rows keep their results
|
|
177
178
|
```
|
|
178
179
|
|
|
@@ -220,6 +221,7 @@ client.create_rows(table.identifier, [InsertRow(fields={"email": "alice@example.
|
|
|
220
221
|
task = client.get_task(task_id)
|
|
221
222
|
# task.progress → {"total", "completed", "no_data", "failed", "processing"} while a bulk run is going
|
|
222
223
|
# client.get_task(task_id, include_partial=True).data → rows already finished
|
|
224
|
+
# client.get_task(task_id, include_trace=True).trace → flow steps + returned_by (branch taken)
|
|
223
225
|
client.cancel_task(task_id) # stops it; poll_task then raises DatabarTaskCancelledError
|
|
224
226
|
```
|
|
225
227
|
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
"""Browser OAuth 2.1 (PKCE) login for `databar login`.
|
|
2
|
+
|
|
3
|
+
Opens the existing Databar consent screen, catches the authorization code on a
|
|
4
|
+
local loopback redirect, exchanges it for a short-lived bearer token, then
|
|
5
|
+
fetches the workspace API key and writes it to ~/.databar/config.
|
|
6
|
+
|
|
7
|
+
The consent screen keeps its own tab ("Databar CLI has been Authorized!") and
|
|
8
|
+
opens the loopback redirect in a second tab. That second tab is only plumbing:
|
|
9
|
+
it is answered once the login has really finished, and on success it closes
|
|
10
|
+
itself so the person lands back on the consent screen's tab.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import base64
|
|
16
|
+
import hashlib
|
|
17
|
+
import html
|
|
18
|
+
import os
|
|
19
|
+
import secrets
|
|
20
|
+
import threading
|
|
21
|
+
import webbrowser
|
|
22
|
+
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
23
|
+
from typing import Optional
|
|
24
|
+
from urllib.parse import parse_qs, urlencode, urlparse
|
|
25
|
+
|
|
26
|
+
import httpx
|
|
27
|
+
|
|
28
|
+
# Must match oauth.0002_seed_cli_oauth_application / settings.DATABAR_CLI_*.
|
|
29
|
+
# The registered redirect is http://127.0.0.1:53682/callback; the server accepts
|
|
30
|
+
# any port on a loopback IP redirect (RFC 8252 §7.3), so the CLI listens on a
|
|
31
|
+
# free port the OS picks instead of a fixed one another program may hold.
|
|
32
|
+
OAUTH_CLIENT_ID = "databar-cli"
|
|
33
|
+
OAUTH_CALLBACK_HOST = "127.0.0.1"
|
|
34
|
+
OAUTH_CALLBACK_PATH = "/callback"
|
|
35
|
+
OAUTH_SCOPE = "cli:access"
|
|
36
|
+
DEFAULT_OAUTH_ISSUER = "https://databar.ai"
|
|
37
|
+
_LOGIN_TIMEOUT_S = 300
|
|
38
|
+
# How long the browser tab waits for the token exchange before it is answered anyway.
|
|
39
|
+
_FINISH_TIMEOUT_S = 60
|
|
40
|
+
|
|
41
|
+
_ERROR_MESSAGES = {
|
|
42
|
+
"access_denied": "You denied access on the Databar consent screen.",
|
|
43
|
+
"plan_not_eligible": "Your plan doesn’t include API and CLI access. Upgrade, then sign in again.",
|
|
44
|
+
"invalid_state": "The sign-in link didn’t match this login attempt.",
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
def _oauth_issuer() -> str:
|
|
49
|
+
return (os.environ.get("DATABAR_OAUTH_ISSUER") or DEFAULT_OAUTH_ISSUER).rstrip("/")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _pkce_pair() -> tuple[str, str]:
|
|
53
|
+
verifier = secrets.token_urlsafe(64)
|
|
54
|
+
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip("=")
|
|
55
|
+
return verifier, challenge
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
class LoginError(RuntimeError):
|
|
59
|
+
"""A login failure; ``code`` picks the wording on the browser page."""
|
|
60
|
+
|
|
61
|
+
def __init__(self, message: str, code: Optional[str] = None) -> None:
|
|
62
|
+
super().__init__(message)
|
|
63
|
+
self.code = code
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
class _CallbackResult:
|
|
67
|
+
def __init__(self) -> None:
|
|
68
|
+
self.code: Optional[str] = None
|
|
69
|
+
self.error: Optional[str] = None
|
|
70
|
+
self.error_description: Optional[str] = None
|
|
71
|
+
# Set by the handler once the browser delivered the redirect.
|
|
72
|
+
self.received = threading.Event()
|
|
73
|
+
# Set by the login flow once it knows how the login ended.
|
|
74
|
+
self.finished = threading.Event()
|
|
75
|
+
self.failure: Optional[LoginError] = None
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
_PAGE = """<!doctype html>
|
|
79
|
+
<html lang="en">
|
|
80
|
+
<head>
|
|
81
|
+
<meta charset="utf-8">
|
|
82
|
+
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|
83
|
+
<meta name="color-scheme" content="light dark">
|
|
84
|
+
<title>Databar CLI</title>
|
|
85
|
+
<style>
|
|
86
|
+
:root {{ --bg: #fafafa; --card: #fff; --text: #1a1a1a; --muted: #555; --border: #e6e6e6; }}
|
|
87
|
+
@media (prefers-color-scheme: dark) {{
|
|
88
|
+
:root {{ --bg: #141414; --card: #1e1e1e; --text: #f2f2f2; --muted: #b0b0b0; --border: #333; }}
|
|
89
|
+
}}
|
|
90
|
+
body {{ margin: 0; min-height: 100vh; display: flex; align-items: center; justify-content: center;
|
|
91
|
+
background: var(--bg); color: var(--text);
|
|
92
|
+
font: 16px/1.6 Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif; }}
|
|
93
|
+
main {{ max-width: 440px; margin: 16px; padding: 36px 40px; background: var(--card);
|
|
94
|
+
border: 1px solid var(--border); border-radius: 12px; }}
|
|
95
|
+
h1 {{ margin: 0 0 12px; font-size: 20px; font-weight: 600; }}
|
|
96
|
+
p {{ margin: 8px 0 0; color: var(--muted); }}
|
|
97
|
+
code {{ font-family: ui-monospace, Menlo, monospace; font-size: 14px; }}
|
|
98
|
+
</style>
|
|
99
|
+
</head>
|
|
100
|
+
<body>
|
|
101
|
+
<main>
|
|
102
|
+
<h1>{title}</h1>
|
|
103
|
+
{body}
|
|
104
|
+
</main>
|
|
105
|
+
{script}
|
|
106
|
+
</body>
|
|
107
|
+
</html>
|
|
108
|
+
"""
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def _page(title: str, body: str, *, close: bool) -> bytes:
|
|
112
|
+
# The consent screen opened this tab with window.open, so the browser lets it
|
|
113
|
+
# close itself; if it doesn't, the text is still the right thing to show.
|
|
114
|
+
script = "<script>window.close()</script>" if close else ""
|
|
115
|
+
return _PAGE.format(title=html.escape(title), body=body, script=script).encode()
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def _success_page() -> bytes:
|
|
119
|
+
return _page(
|
|
120
|
+
"You’re signed in to Databar CLI",
|
|
121
|
+
"<p>You can close this tab and return to your terminal.</p>",
|
|
122
|
+
close=True,
|
|
123
|
+
)
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _failure_page(failure: LoginError) -> bytes:
|
|
127
|
+
reason = _ERROR_MESSAGES.get(failure.code or "") or str(failure)
|
|
128
|
+
return _page(
|
|
129
|
+
"Sign-in didn’t finish",
|
|
130
|
+
f"<p>{html.escape(reason)}</p>"
|
|
131
|
+
"<p>Run <code>databar login</code> in your terminal to try again.</p>",
|
|
132
|
+
close=False,
|
|
133
|
+
)
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def _make_handler(result: _CallbackResult, expected_state: str):
|
|
137
|
+
class Handler(BaseHTTPRequestHandler):
|
|
138
|
+
def log_message(self, format: str, *args) -> None: # noqa: A003
|
|
139
|
+
return
|
|
140
|
+
|
|
141
|
+
def do_GET(self) -> None: # noqa: N802
|
|
142
|
+
parsed = urlparse(self.path)
|
|
143
|
+
if parsed.path.rstrip("/") != OAUTH_CALLBACK_PATH or result.received.is_set():
|
|
144
|
+
self.send_response(404)
|
|
145
|
+
self.end_headers()
|
|
146
|
+
return
|
|
147
|
+
|
|
148
|
+
qs = parse_qs(parsed.query)
|
|
149
|
+
error = (qs.get("error") or [None])[0]
|
|
150
|
+
state = (qs.get("state") or [None])[0]
|
|
151
|
+
if error:
|
|
152
|
+
result.error = error
|
|
153
|
+
result.error_description = (qs.get("error_description") or [None])[0]
|
|
154
|
+
elif state != expected_state:
|
|
155
|
+
result.error = "invalid_state"
|
|
156
|
+
result.error_description = "OAuth state mismatch"
|
|
157
|
+
else:
|
|
158
|
+
result.code = (qs.get("code") or [None])[0]
|
|
159
|
+
result.received.set()
|
|
160
|
+
|
|
161
|
+
# Answer the tab only once the login is over, so it never says
|
|
162
|
+
# "signed in" for a code the server then refused.
|
|
163
|
+
finished = result.finished.wait(timeout=_FINISH_TIMEOUT_S)
|
|
164
|
+
if finished and result.failure is None:
|
|
165
|
+
status, body = 200, _success_page()
|
|
166
|
+
else:
|
|
167
|
+
failure = result.failure or LoginError("The terminal didn’t finish the login in time.")
|
|
168
|
+
status, body = 400, _failure_page(failure)
|
|
169
|
+
|
|
170
|
+
self.send_response(status)
|
|
171
|
+
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
172
|
+
self.send_header("Content-Length", str(len(body)))
|
|
173
|
+
self.send_header("Cache-Control", "no-store")
|
|
174
|
+
self.end_headers()
|
|
175
|
+
self.wfile.write(body)
|
|
176
|
+
|
|
177
|
+
return Handler
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _exchange_code(issuer: str, code: str, verifier: str, redirect_uri: str) -> str:
|
|
181
|
+
response = httpx.post(
|
|
182
|
+
f"{issuer}/oauth/token/",
|
|
183
|
+
data={
|
|
184
|
+
"grant_type": "authorization_code",
|
|
185
|
+
"code": code,
|
|
186
|
+
"redirect_uri": redirect_uri,
|
|
187
|
+
"client_id": OAUTH_CLIENT_ID,
|
|
188
|
+
"code_verifier": verifier,
|
|
189
|
+
},
|
|
190
|
+
headers={"Accept": "application/json"},
|
|
191
|
+
timeout=30.0,
|
|
192
|
+
)
|
|
193
|
+
if response.status_code != 200:
|
|
194
|
+
detail = response.text[:200]
|
|
195
|
+
raise LoginError(f"Token exchange failed ({response.status_code}): {detail}")
|
|
196
|
+
payload = response.json()
|
|
197
|
+
access_token = payload.get("access_token")
|
|
198
|
+
if not access_token:
|
|
199
|
+
raise LoginError("Token exchange response missing access_token")
|
|
200
|
+
return access_token
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
def _fetch_api_key(issuer: str, access_token: str) -> dict:
|
|
204
|
+
response = httpx.get(
|
|
205
|
+
f"{issuer}/oauth/api/cli-credentials/",
|
|
206
|
+
headers={"Authorization": f"Bearer {access_token}", "Accept": "application/json"},
|
|
207
|
+
timeout=30.0,
|
|
208
|
+
)
|
|
209
|
+
if response.status_code != 200:
|
|
210
|
+
code = None
|
|
211
|
+
try:
|
|
212
|
+
code = response.json().get("error")
|
|
213
|
+
except ValueError:
|
|
214
|
+
pass
|
|
215
|
+
if code in _ERROR_MESSAGES:
|
|
216
|
+
raise LoginError(_ERROR_MESSAGES[code], code)
|
|
217
|
+
detail = response.text[:200]
|
|
218
|
+
raise LoginError(f"Could not fetch API key ({response.status_code}): {detail}", code)
|
|
219
|
+
payload = response.json()
|
|
220
|
+
if not payload.get("api_key"):
|
|
221
|
+
raise LoginError("Credentials response missing api_key")
|
|
222
|
+
return payload
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _finish(result: _CallbackResult, issuer: str, verifier: str, redirect_uri: str) -> dict:
|
|
226
|
+
if result.error:
|
|
227
|
+
code = result.error
|
|
228
|
+
message = _ERROR_MESSAGES.get(code) or result.error_description or code
|
|
229
|
+
raise LoginError(f"Login was denied or failed: {message}", code)
|
|
230
|
+
if not result.code:
|
|
231
|
+
raise LoginError("No authorization code received from the browser callback.")
|
|
232
|
+
access_token = _exchange_code(issuer, result.code, verifier, redirect_uri)
|
|
233
|
+
return _fetch_api_key(issuer, access_token)
|
|
234
|
+
|
|
235
|
+
|
|
236
|
+
def run_browser_login(*, print_url=None) -> dict:
|
|
237
|
+
"""Run the full browser OAuth flow.
|
|
238
|
+
|
|
239
|
+
Returns credentials dict with ``api_key`` and ``email``.
|
|
240
|
+
``print_url`` is called with the authorize URL (so the CLI can show it if
|
|
241
|
+
the browser fails to open).
|
|
242
|
+
"""
|
|
243
|
+
issuer = _oauth_issuer()
|
|
244
|
+
verifier, challenge = _pkce_pair()
|
|
245
|
+
state = secrets.token_urlsafe(24)
|
|
246
|
+
result = _CallbackResult()
|
|
247
|
+
|
|
248
|
+
# Port 0: the OS hands out a free port, so nothing else listening can take the code.
|
|
249
|
+
server = HTTPServer((OAUTH_CALLBACK_HOST, 0), _make_handler(result, state))
|
|
250
|
+
port = server.server_address[1]
|
|
251
|
+
redirect_uri = f"http://{OAUTH_CALLBACK_HOST}:{port}{OAUTH_CALLBACK_PATH}"
|
|
252
|
+
|
|
253
|
+
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
|
254
|
+
thread.start()
|
|
255
|
+
|
|
256
|
+
try:
|
|
257
|
+
params = {
|
|
258
|
+
"client_id": OAUTH_CLIENT_ID,
|
|
259
|
+
"redirect_uri": redirect_uri,
|
|
260
|
+
"response_type": "code",
|
|
261
|
+
"scope": OAUTH_SCOPE,
|
|
262
|
+
"code_challenge": challenge,
|
|
263
|
+
"code_challenge_method": "S256",
|
|
264
|
+
"state": state,
|
|
265
|
+
}
|
|
266
|
+
authorize_url = f"{issuer}/oauth/authorize/?{urlencode(params)}"
|
|
267
|
+
|
|
268
|
+
opened = webbrowser.open(authorize_url)
|
|
269
|
+
if print_url is not None:
|
|
270
|
+
print_url(authorize_url, opened=opened)
|
|
271
|
+
|
|
272
|
+
if not result.received.wait(timeout=_LOGIN_TIMEOUT_S):
|
|
273
|
+
raise LoginError("Timed out waiting for browser login. Run `databar login` again.")
|
|
274
|
+
|
|
275
|
+
try:
|
|
276
|
+
return _finish(result, issuer, verifier, redirect_uri)
|
|
277
|
+
except LoginError as exc:
|
|
278
|
+
result.failure = exc
|
|
279
|
+
raise
|
|
280
|
+
except Exception as exc: # network errors etc. — the tab still gets an answer
|
|
281
|
+
result.failure = LoginError(str(exc))
|
|
282
|
+
raise
|
|
283
|
+
finally:
|
|
284
|
+
# Release the waiting browser tab, then stop the server once it has answered.
|
|
285
|
+
result.finished.set()
|
|
286
|
+
server.shutdown()
|
|
287
|
+
server.server_close()
|
|
288
|
+
thread.join(timeout=2)
|
|
@@ -100,6 +100,17 @@ def get_flow(
|
|
|
100
100
|
]
|
|
101
101
|
output(rows, OutputFormat.TABLE, table_columns=["id", "required", "type", "description"])
|
|
102
102
|
|
|
103
|
+
declared = [out for out in f.outputs if out.type or out.description or out.fields]
|
|
104
|
+
if declared:
|
|
105
|
+
# Fields of a flow that answers through Output steps: the keys of every run's result.
|
|
106
|
+
console.print("[bold]Returns:[/bold]")
|
|
107
|
+
rows = []
|
|
108
|
+
for out in declared:
|
|
109
|
+
rows.append({"field": out.id, "type": out.type or "", "description": out.description or ""})
|
|
110
|
+
for sub in out.fields or []:
|
|
111
|
+
rows.append({"field": f"{out.id}.{sub.id}", "type": sub.type or "", "description": sub.description or ""})
|
|
112
|
+
output(rows, OutputFormat.TABLE, table_columns=["field", "type", "description"])
|
|
113
|
+
|
|
103
114
|
|
|
104
115
|
@app.command("run")
|
|
105
116
|
def run_flow(
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"""
|
|
2
2
|
CLI commands for tasks.
|
|
3
3
|
|
|
4
|
-
databar task get <task-id> [--format] [--poll] [--partial]
|
|
4
|
+
databar task get <task-id> [--format] [--poll] [--partial] [--trace]
|
|
5
5
|
databar task cancel <task-id>
|
|
6
6
|
"""
|
|
7
7
|
|
|
@@ -49,6 +49,27 @@ def _print_status(task) -> None:
|
|
|
49
49
|
)
|
|
50
50
|
|
|
51
51
|
|
|
52
|
+
def _print_trace(trace) -> None:
|
|
53
|
+
"""Which Output step answered and what each step did, for one flow run (or each of a bulk run)."""
|
|
54
|
+
runs = trace if isinstance(trace, list) else [trace]
|
|
55
|
+
for index, run in enumerate(runs):
|
|
56
|
+
if run is None:
|
|
57
|
+
continue
|
|
58
|
+
if len(runs) > 1:
|
|
59
|
+
console.print(f"[bold]Input {index}:[/bold]")
|
|
60
|
+
returned_by = run.get("returned_by")
|
|
61
|
+
if returned_by:
|
|
62
|
+
name = returned_by.get("name") or returned_by.get("id")
|
|
63
|
+
console.print(f"[dim]Returned by:[/dim] {name} ({returned_by.get('id')})")
|
|
64
|
+
rows = [
|
|
65
|
+
{"node_id": step.get("node_id", ""), "type": step.get("type", ""), "cost": step.get("cost", "")}
|
|
66
|
+
for step in run.get("steps") or []
|
|
67
|
+
if isinstance(step, dict)
|
|
68
|
+
]
|
|
69
|
+
if rows:
|
|
70
|
+
output(rows, OutputFormat.TABLE, table_columns=["node_id", "type", "cost"])
|
|
71
|
+
|
|
72
|
+
|
|
52
73
|
@app.command("get")
|
|
53
74
|
def get_task(
|
|
54
75
|
task_id: str = _TASK_ID,
|
|
@@ -63,6 +84,11 @@ def get_task(
|
|
|
63
84
|
"--partial",
|
|
64
85
|
help="For a running bulk task, also show the rows that have already finished.",
|
|
65
86
|
),
|
|
87
|
+
trace: bool = typer.Option(
|
|
88
|
+
False,
|
|
89
|
+
"--trace",
|
|
90
|
+
help="For a flow task, also show what each step did and which Output step answered.",
|
|
91
|
+
),
|
|
66
92
|
) -> None:
|
|
67
93
|
"""Get the status and result of a task."""
|
|
68
94
|
client = get_client()
|
|
@@ -72,9 +98,17 @@ def get_task(
|
|
|
72
98
|
data = client.poll_task(task_id)
|
|
73
99
|
output(data, fmt)
|
|
74
100
|
else:
|
|
75
|
-
task = client.get_task(task_id, include_partial=partial)
|
|
101
|
+
task = client.get_task(task_id, include_partial=partial, include_trace=trace)
|
|
76
102
|
if fmt != OutputFormat.JSON:
|
|
77
103
|
_print_status(task)
|
|
104
|
+
if trace and fmt == OutputFormat.JSON:
|
|
105
|
+
output(
|
|
106
|
+
{"task_id": task.task_id, "status": task.status, "data": task.data, "trace": task.trace},
|
|
107
|
+
fmt,
|
|
108
|
+
)
|
|
109
|
+
return
|
|
110
|
+
if trace and task.trace:
|
|
111
|
+
_print_trace(task.trace)
|
|
78
112
|
if task.data is not None:
|
|
79
113
|
output(task.data, fmt)
|
|
80
114
|
elif task.error:
|
|
@@ -309,14 +309,23 @@ class DatabarClient:
|
|
|
309
309
|
# Task polling
|
|
310
310
|
# -----------------------------------------------------------------------
|
|
311
311
|
|
|
312
|
-
def get_task(self, task_id: str, include_partial: bool = False) -> TaskResponse:
|
|
312
|
+
def get_task(self, task_id: str, include_partial: bool = False, include_trace: bool = False) -> TaskResponse:
|
|
313
313
|
"""Get the current status of a task.
|
|
314
314
|
|
|
315
315
|
While a bulk task is still running, ``progress`` carries per-input counts. Pass
|
|
316
316
|
``include_partial=True`` to also get back the rows that have already finished —
|
|
317
317
|
off by default because it makes the API read the whole result set on every poll.
|
|
318
|
+
|
|
319
|
+
``include_trace=True`` (flow tasks) fills ``trace``: what each step did, and
|
|
320
|
+
``returned_by`` — the Output step that answered, i.e. which branch ran. Ask for it
|
|
321
|
+
when a flow completes with less in ``data`` than you expected.
|
|
318
322
|
"""
|
|
319
|
-
params = {
|
|
323
|
+
params: Optional[Dict[str, str]] = {}
|
|
324
|
+
if include_partial:
|
|
325
|
+
params["include_partial"] = "true"
|
|
326
|
+
if include_trace:
|
|
327
|
+
params["include_trace"] = "true"
|
|
328
|
+
params = params or None
|
|
320
329
|
data = self._request("GET", f"/tasks/{task_id}", params=params)
|
|
321
330
|
return TaskResponse.model_validate(data)
|
|
322
331
|
|
|
@@ -282,6 +282,16 @@ class TaskResponse(BaseModel):
|
|
|
282
282
|
)
|
|
283
283
|
error: Optional[Union[str, List[str]]] = None
|
|
284
284
|
credits_spent: float = 0
|
|
285
|
+
trace: Optional[Union[Dict[str, Any], List[Optional[Dict[str, Any]]]]] = Field(
|
|
286
|
+
default=None,
|
|
287
|
+
description=(
|
|
288
|
+
"Flow tasks fetched with include_trace=True: what each step of the run did — "
|
|
289
|
+
"run_status, termination_reason, steps (one per node, in execution order) and "
|
|
290
|
+
"returned_by, the {id, name} of the Output step that built `data`, so you can tell "
|
|
291
|
+
"which branch the run took (None when no Output step answered). A bulk flow task "
|
|
292
|
+
"gets a list aligned to the inputs."
|
|
293
|
+
),
|
|
294
|
+
)
|
|
285
295
|
|
|
286
296
|
@classmethod
|
|
287
297
|
def model_validate(cls, obj: Any, **kwargs: Any) -> "TaskResponse":
|
|
@@ -354,13 +364,27 @@ class FlowInput(BaseModel):
|
|
|
354
364
|
|
|
355
365
|
|
|
356
366
|
class FlowOutput(BaseModel):
|
|
357
|
-
"""A field
|
|
367
|
+
"""A field the flow returns.
|
|
368
|
+
|
|
369
|
+
Fields: id, key, name, response_field_id, type, description, fields.
|
|
358
370
|
|
|
359
|
-
|
|
371
|
+
For a flow that answers through Output steps, ``id`` is the key in the run
|
|
372
|
+
result and ``type`` / ``description`` are what the flow's author declared
|
|
373
|
+
(``type`` is text, number, boolean, list, object or any). An ``object``
|
|
374
|
+
field lists its sub-fields in ``fields``. The extra fields are None for a
|
|
375
|
+
flow without Output steps.
|
|
360
376
|
"""
|
|
361
377
|
|
|
362
378
|
id: str
|
|
379
|
+
key: Optional[str] = None
|
|
380
|
+
name: Optional[str] = None
|
|
363
381
|
response_field_id: Optional[int] = None
|
|
382
|
+
type: Optional[str] = Field(
|
|
383
|
+
default=None,
|
|
384
|
+
description="Declared type of an Output step field: text, number, boolean, list, object or any.",
|
|
385
|
+
)
|
|
386
|
+
description: Optional[str] = Field(default=None, description="What the field holds, as its author described it.")
|
|
387
|
+
fields: Optional[List["FlowOutput"]] = Field(default=None, description="Sub-fields of an object field.")
|
|
364
388
|
|
|
365
389
|
|
|
366
390
|
class Flow(BaseModel):
|
|
@@ -453,9 +477,13 @@ class FlowConfigOpsResult(BaseModel):
|
|
|
453
477
|
|
|
454
478
|
|
|
455
479
|
class FlowConfigError(BaseModel):
|
|
456
|
-
"""One reason a flow config would be rejected on save.
|
|
480
|
+
"""One reason a flow config would be rejected on save (or, in ``warnings``, a heads-up).
|
|
481
|
+
|
|
482
|
+
``field`` is the path inside the node (or the config) the message is about, when known.
|
|
483
|
+
"""
|
|
457
484
|
|
|
458
485
|
node_id: Optional[str] = None
|
|
486
|
+
field: Optional[str] = None
|
|
459
487
|
message: str
|
|
460
488
|
|
|
461
489
|
|
|
@@ -464,10 +492,15 @@ class FlowValidateResult(BaseModel):
|
|
|
464
492
|
|
|
465
493
|
An invalid config is a valid answer — ``valid`` is False and ``errors``
|
|
466
494
|
names the node (when known) rather than raising.
|
|
495
|
+
|
|
496
|
+
``warnings`` lists what saves but may not do what you meant: a condition
|
|
497
|
+
branch with no Output step (its rows return nothing), a Custom JSON
|
|
498
|
+
response without the declared output fields. They never affect ``valid``.
|
|
467
499
|
"""
|
|
468
500
|
|
|
469
501
|
valid: bool
|
|
470
502
|
errors: List[FlowConfigError] = Field(default_factory=list)
|
|
503
|
+
warnings: List[FlowConfigError] = Field(default_factory=list)
|
|
471
504
|
|
|
472
505
|
|
|
473
506
|
class FlowCostEstimate(BaseModel):
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: databar
|
|
3
|
-
Version: 2.
|
|
3
|
+
Version: 2.10.1
|
|
4
4
|
Summary: Official Databar.ai Python SDK and CLI — connect to enrichments, waterfalls, and tables via api.databar.ai
|
|
5
5
|
Author-email: "Databar.ai Team" <info@databar.ai>
|
|
6
6
|
License: MIT License
|
|
@@ -409,6 +409,9 @@ status.progress # {"total": 2, "completed": 1, "failed": 0, "processing
|
|
|
409
409
|
# Rows that have already finished, without waiting for the rest
|
|
410
410
|
client.get_task(task.task_id, include_partial=True).data
|
|
411
411
|
|
|
412
|
+
# A flow task: what each step did, and which Output step (branch) answered
|
|
413
|
+
client.get_task(task.task_id, include_trace=True).trace["returned_by"]
|
|
414
|
+
|
|
412
415
|
# Stop it — unfinished requests are refunded, finished rows keep their results
|
|
413
416
|
client.cancel_task(task.task_id)
|
|
414
417
|
```
|
|
@@ -592,6 +595,7 @@ databar task get <task-id> --poll
|
|
|
592
595
|
|
|
593
596
|
# Collect the rows a running bulk task has already finished
|
|
594
597
|
databar task get <task-id> --partial
|
|
598
|
+
databar task get <task-id> --trace # flow task: steps and the Output step that answered
|
|
595
599
|
|
|
596
600
|
# Stop a running task; finished rows keep their results
|
|
597
601
|
databar task cancel <task-id>
|
|
@@ -730,7 +730,31 @@ def test_task_get_partial_asks_for_finished_rows(monkeypatch):
|
|
|
730
730
|
monkeypatch.setattr("databar.cli.tasks.get_client", lambda: mock)
|
|
731
731
|
result = invoke(["task", "get", TASK_ID, "--partial"])
|
|
732
732
|
assert result.exit_code == 0
|
|
733
|
-
mock.get_task.assert_called_once_with(TASK_ID, include_partial=True)
|
|
733
|
+
mock.get_task.assert_called_once_with(TASK_ID, include_partial=True, include_trace=False)
|
|
734
|
+
|
|
735
|
+
|
|
736
|
+
def test_task_get_trace_names_the_output_step_that_answered(monkeypatch):
|
|
737
|
+
mock = _client_mock()
|
|
738
|
+
mock.get_task.return_value = TaskResponse(
|
|
739
|
+
task_id=TASK_ID,
|
|
740
|
+
status="completed",
|
|
741
|
+
data={"email": "a@b.co"},
|
|
742
|
+
trace={
|
|
743
|
+
"run_status": "completed",
|
|
744
|
+
"returned_by": {"id": "out_hot", "name": "Hot lead"},
|
|
745
|
+
"steps": [{"node_id": "check", "type": "condition"}, {"node_id": "out_hot", "type": "output"}],
|
|
746
|
+
},
|
|
747
|
+
)
|
|
748
|
+
monkeypatch.setattr("databar.cli.tasks.get_client", lambda: mock)
|
|
749
|
+
result = invoke(["task", "get", TASK_ID, "--trace"])
|
|
750
|
+
assert result.exit_code == 0
|
|
751
|
+
mock.get_task.assert_called_once_with(TASK_ID, include_partial=False, include_trace=True)
|
|
752
|
+
assert "Hot lead" in result.output
|
|
753
|
+
|
|
754
|
+
result = invoke(["task", "get", TASK_ID, "--trace", "--format", "json"])
|
|
755
|
+
payload = json.loads(result.stdout)["data"]
|
|
756
|
+
assert payload["trace"]["returned_by"]["id"] == "out_hot"
|
|
757
|
+
assert payload["data"] == {"email": "a@b.co"}
|
|
734
758
|
|
|
735
759
|
|
|
736
760
|
def test_task_cancel(monkeypatch):
|
|
@@ -759,7 +783,7 @@ def test_task_cancel_reports_a_finished_task(monkeypatch):
|
|
|
759
783
|
def test_version_flag():
|
|
760
784
|
result = runner.invoke(app, ["--version"])
|
|
761
785
|
assert result.exit_code == 0
|
|
762
|
-
assert "2.
|
|
786
|
+
assert "2.10.1" in result.output
|
|
763
787
|
|
|
764
788
|
|
|
765
789
|
def test_output_module_does_not_import_click():
|
|
@@ -453,6 +453,60 @@ def test_validate_flow_config_posts_the_config(client: DatabarClient, httpx_mock
|
|
|
453
453
|
assert body == {"config": {"nodes": []}}
|
|
454
454
|
|
|
455
455
|
|
|
456
|
+
def test_validate_flow_config_returns_warnings_apart_from_errors(client: DatabarClient, httpx_mock: HTTPXMock):
|
|
457
|
+
httpx_mock.add_response(
|
|
458
|
+
url=f"{BASE_URL}/flows/validate-config",
|
|
459
|
+
method="POST",
|
|
460
|
+
json={
|
|
461
|
+
"valid": True,
|
|
462
|
+
"errors": [],
|
|
463
|
+
"warnings": [{"node_id": "cond1", "field": "else_nodes", "message": "this branch returns nothing"}],
|
|
464
|
+
},
|
|
465
|
+
)
|
|
466
|
+
result = client.validate_flow_config({"nodes": []})
|
|
467
|
+
assert result.valid is True
|
|
468
|
+
assert result.warnings[0].node_id == "cond1"
|
|
469
|
+
assert result.warnings[0].field == "else_nodes"
|
|
470
|
+
|
|
471
|
+
|
|
472
|
+
def test_flow_outputs_carry_the_declared_output_fields(client: DatabarClient, httpx_mock: HTTPXMock):
|
|
473
|
+
httpx_mock.add_response(
|
|
474
|
+
url=f"{BASE_URL}/flows/flow-uuid-1",
|
|
475
|
+
method="GET",
|
|
476
|
+
json={
|
|
477
|
+
"id": "flow-uuid-1",
|
|
478
|
+
"name": "Qualify",
|
|
479
|
+
"description": "",
|
|
480
|
+
"inputs": [],
|
|
481
|
+
"outputs": [
|
|
482
|
+
{"id": "email", "type": "text", "description": "Work email", "response_field_id": None},
|
|
483
|
+
{"id": "company", "type": "object", "fields": [{"id": "size", "type": "number"}]},
|
|
484
|
+
],
|
|
485
|
+
"config": {},
|
|
486
|
+
},
|
|
487
|
+
)
|
|
488
|
+
flow = client.get_flow("flow-uuid-1")
|
|
489
|
+
assert flow.outputs[0].type == "text"
|
|
490
|
+
assert flow.outputs[0].description == "Work email"
|
|
491
|
+
assert flow.outputs[1].fields[0].id == "size"
|
|
492
|
+
assert flow.outputs[1].fields[0].type == "number"
|
|
493
|
+
|
|
494
|
+
|
|
495
|
+
def test_get_task_asks_for_the_trace(client: DatabarClient, httpx_mock: HTTPXMock):
|
|
496
|
+
httpx_mock.add_response(
|
|
497
|
+
url=f"{BASE_URL}/tasks/t1?include_trace=true",
|
|
498
|
+
method="GET",
|
|
499
|
+
json={
|
|
500
|
+
"task_id": "t1",
|
|
501
|
+
"status": "completed",
|
|
502
|
+
"data": {"email": "a@b.co"},
|
|
503
|
+
"trace": {"run_status": "completed", "returned_by": {"id": "out1", "name": "Output"}, "steps": []},
|
|
504
|
+
},
|
|
505
|
+
)
|
|
506
|
+
task = client.get_task("t1", include_trace=True)
|
|
507
|
+
assert task.trace["returned_by"]["id"] == "out1"
|
|
508
|
+
|
|
509
|
+
|
|
456
510
|
def test_estimate_flow_cost_posts_the_config(client: DatabarClient, httpx_mock: HTTPXMock):
|
|
457
511
|
httpx_mock.add_response(
|
|
458
512
|
url=f"{BASE_URL}/flows/cost-estimate",
|
|
@@ -0,0 +1,176 @@
|
|
|
1
|
+
"""Unit tests for CLI OAuth helpers (token exchange + credentials fetch)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import httpx
|
|
6
|
+
import pytest
|
|
7
|
+
import respx
|
|
8
|
+
|
|
9
|
+
import threading
|
|
10
|
+
import urllib.error
|
|
11
|
+
import urllib.request
|
|
12
|
+
from urllib.parse import parse_qs, urlencode, urlparse
|
|
13
|
+
|
|
14
|
+
from databar.cli._oauth import (
|
|
15
|
+
OAUTH_CLIENT_ID,
|
|
16
|
+
LoginError,
|
|
17
|
+
_exchange_code,
|
|
18
|
+
_fetch_api_key,
|
|
19
|
+
_pkce_pair,
|
|
20
|
+
run_browser_login,
|
|
21
|
+
)
|
|
22
|
+
|
|
23
|
+
REDIRECT = "http://127.0.0.1:61234/callback"
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def test_pkce_pair_s256_shape():
|
|
27
|
+
verifier, challenge = _pkce_pair()
|
|
28
|
+
assert len(verifier) > 40
|
|
29
|
+
assert len(challenge) >= 43
|
|
30
|
+
assert "=" not in challenge
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
@respx.mock
|
|
34
|
+
def test_exchange_code_posts_pkce_fields():
|
|
35
|
+
route = respx.post("https://databar.ai/oauth/token/").mock(
|
|
36
|
+
return_value=httpx.Response(200, json={"access_token": "tok", "token_type": "Bearer"})
|
|
37
|
+
)
|
|
38
|
+
token = _exchange_code("https://databar.ai", "auth-code", "verifier", REDIRECT)
|
|
39
|
+
assert token == "tok"
|
|
40
|
+
assert route.called
|
|
41
|
+
request = route.calls.last.request
|
|
42
|
+
body = request.content.decode()
|
|
43
|
+
assert "grant_type=authorization_code" in body
|
|
44
|
+
assert "code=auth-code" in body
|
|
45
|
+
assert f"client_id={OAUTH_CLIENT_ID}" in body
|
|
46
|
+
assert "code_verifier=verifier" in body
|
|
47
|
+
assert "redirect_uri=" + REDIRECT.replace(":", "%3A").replace("/", "%2F") in body
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@respx.mock
|
|
51
|
+
def test_fetch_api_key_sends_bearer():
|
|
52
|
+
respx.get("https://databar.ai/oauth/api/cli-credentials/").mock(
|
|
53
|
+
return_value=httpx.Response(
|
|
54
|
+
200,
|
|
55
|
+
json={"api_key": "k", "email": "a@b.com", "workspace_identifier": "w", "workspace_name": "W"},
|
|
56
|
+
)
|
|
57
|
+
)
|
|
58
|
+
payload = _fetch_api_key("https://databar.ai", "tok")
|
|
59
|
+
assert payload["api_key"] == "k"
|
|
60
|
+
assert payload["email"] == "a@b.com"
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
@respx.mock
|
|
64
|
+
def test_exchange_code_raises_on_error():
|
|
65
|
+
respx.post("https://databar.ai/oauth/token/").mock(
|
|
66
|
+
return_value=httpx.Response(400, text="bad")
|
|
67
|
+
)
|
|
68
|
+
with pytest.raises(RuntimeError, match="Token exchange failed"):
|
|
69
|
+
_exchange_code("https://databar.ai", "bad", "v", REDIRECT)
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
class _Browser:
|
|
73
|
+
"""Stands in for the person's browser: follows the consent screen's redirect."""
|
|
74
|
+
|
|
75
|
+
def __init__(self, monkeypatch, query=None):
|
|
76
|
+
self.query = query # callable(state) -> dict of callback params
|
|
77
|
+
self.page_status = None
|
|
78
|
+
self.page = None
|
|
79
|
+
self.redirect_uri = None
|
|
80
|
+
self._thread = None
|
|
81
|
+
monkeypatch.setattr("databar.cli._oauth.webbrowser.open", self.open)
|
|
82
|
+
|
|
83
|
+
def open(self, url):
|
|
84
|
+
params = parse_qs(urlparse(url).query)
|
|
85
|
+
self.redirect_uri = params["redirect_uri"][0]
|
|
86
|
+
state = params["state"][0]
|
|
87
|
+
query = self.query(state) if self.query else {"code": "auth-code", "state": state}
|
|
88
|
+
target = f"{self.redirect_uri}?{urlencode(query)}"
|
|
89
|
+
self._thread = threading.Thread(target=self._visit, args=(target,), daemon=True)
|
|
90
|
+
self._thread.start()
|
|
91
|
+
return True
|
|
92
|
+
|
|
93
|
+
def _visit(self, target):
|
|
94
|
+
try:
|
|
95
|
+
with urllib.request.urlopen(target, timeout=10) as response:
|
|
96
|
+
self.page_status, self.page = response.status, response.read().decode()
|
|
97
|
+
except urllib.error.HTTPError as exc:
|
|
98
|
+
self.page_status, self.page = exc.code, exc.read().decode()
|
|
99
|
+
|
|
100
|
+
def wait(self):
|
|
101
|
+
self._thread.join(timeout=10)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _mock_server(token_status=200, credentials=None):
|
|
105
|
+
token = respx.post("https://databar.ai/oauth/token/").mock(
|
|
106
|
+
return_value=httpx.Response(token_status, json={"access_token": "tok"} if token_status == 200 else None, text=None if token_status == 200 else "invalid_grant")
|
|
107
|
+
)
|
|
108
|
+
respx.get("https://databar.ai/oauth/api/cli-credentials/").mock(
|
|
109
|
+
return_value=credentials or httpx.Response(200, json={"api_key": "k", "email": "a@b.com"})
|
|
110
|
+
)
|
|
111
|
+
return token
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
@respx.mock
|
|
115
|
+
def test_login_listens_on_a_free_port_and_closes_the_tab(monkeypatch):
|
|
116
|
+
token = _mock_server()
|
|
117
|
+
browser = _Browser(monkeypatch)
|
|
118
|
+
|
|
119
|
+
creds = run_browser_login()
|
|
120
|
+
browser.wait()
|
|
121
|
+
|
|
122
|
+
assert creds["api_key"] == "k"
|
|
123
|
+
port = urlparse(browser.redirect_uri).port
|
|
124
|
+
assert browser.redirect_uri == f"http://127.0.0.1:{port}/callback"
|
|
125
|
+
# The code is redeemed for the port it was actually sent to.
|
|
126
|
+
assert f"127.0.0.1%3A{port}%2Fcallback" in token.calls.last.request.content.decode()
|
|
127
|
+
assert browser.page_status == 200
|
|
128
|
+
assert "You’re signed in to Databar CLI" in browser.page
|
|
129
|
+
assert "window.close()" in browser.page
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
@respx.mock
|
|
133
|
+
def test_a_refused_code_is_not_shown_as_signed_in(monkeypatch):
|
|
134
|
+
_mock_server(token_status=400)
|
|
135
|
+
browser = _Browser(monkeypatch)
|
|
136
|
+
|
|
137
|
+
with pytest.raises(LoginError, match="Token exchange failed"):
|
|
138
|
+
run_browser_login()
|
|
139
|
+
browser.wait()
|
|
140
|
+
|
|
141
|
+
assert browser.page_status == 400
|
|
142
|
+
assert "Sign-in didn’t finish" in browser.page
|
|
143
|
+
assert "window.close()" not in browser.page
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
@respx.mock
|
|
147
|
+
def test_a_plan_without_cli_access_says_so_in_the_tab(monkeypatch):
|
|
148
|
+
_mock_server(credentials=httpx.Response(403, json={"error": "plan_not_eligible"}))
|
|
149
|
+
browser = _Browser(monkeypatch)
|
|
150
|
+
|
|
151
|
+
with pytest.raises(LoginError, match="plan doesn’t include"):
|
|
152
|
+
run_browser_login()
|
|
153
|
+
browser.wait()
|
|
154
|
+
|
|
155
|
+
assert browser.page_status == 400
|
|
156
|
+
assert "plan doesn’t include API and CLI access" in browser.page
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
@respx.mock
|
|
160
|
+
def test_deny_and_a_foreign_state_never_reach_the_token_endpoint(monkeypatch):
|
|
161
|
+
token = _mock_server()
|
|
162
|
+
|
|
163
|
+
browser = _Browser(monkeypatch, query=lambda state: {"error": "access_denied", "state": state})
|
|
164
|
+
with pytest.raises(LoginError, match="denied access"):
|
|
165
|
+
run_browser_login()
|
|
166
|
+
browser.wait()
|
|
167
|
+
assert browser.page_status == 400
|
|
168
|
+
assert "You denied access" in browser.page
|
|
169
|
+
|
|
170
|
+
browser = _Browser(monkeypatch, query=lambda state: {"code": "stolen", "state": "someone-else"})
|
|
171
|
+
with pytest.raises(LoginError, match="didn’t match"):
|
|
172
|
+
run_browser_login()
|
|
173
|
+
browser.wait()
|
|
174
|
+
assert browser.page_status == 400
|
|
175
|
+
|
|
176
|
+
assert not token.called
|
|
@@ -1,190 +0,0 @@
|
|
|
1
|
-
"""Browser OAuth 2.1 (PKCE) login for `databar login`.
|
|
2
|
-
|
|
3
|
-
Opens the existing Databar consent screen, catches the authorization code on a
|
|
4
|
-
local loopback redirect, exchanges it for a short-lived bearer token, then
|
|
5
|
-
fetches the workspace API key and writes it to ~/.databar/config.
|
|
6
|
-
"""
|
|
7
|
-
|
|
8
|
-
from __future__ import annotations
|
|
9
|
-
|
|
10
|
-
import base64
|
|
11
|
-
import hashlib
|
|
12
|
-
import os
|
|
13
|
-
import secrets
|
|
14
|
-
import threading
|
|
15
|
-
import webbrowser
|
|
16
|
-
from http.server import BaseHTTPRequestHandler, HTTPServer
|
|
17
|
-
from typing import Optional
|
|
18
|
-
from urllib.parse import parse_qs, urlencode, urlparse
|
|
19
|
-
|
|
20
|
-
import httpx
|
|
21
|
-
|
|
22
|
-
# Must match oauth.0002_seed_cli_oauth_application / settings.DATABAR_CLI_*.
|
|
23
|
-
OAUTH_CLIENT_ID = "databar-cli"
|
|
24
|
-
OAUTH_REDIRECT_URI = "http://127.0.0.1:53682/callback"
|
|
25
|
-
OAUTH_CALLBACK_PORT = 53682
|
|
26
|
-
OAUTH_SCOPE = "cli:access"
|
|
27
|
-
DEFAULT_OAUTH_ISSUER = "https://databar.ai"
|
|
28
|
-
_LOGIN_TIMEOUT_S = 300
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
def _oauth_issuer() -> str:
|
|
32
|
-
return (os.environ.get("DATABAR_OAUTH_ISSUER") or DEFAULT_OAUTH_ISSUER).rstrip("/")
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
def _pkce_pair() -> tuple[str, str]:
|
|
36
|
-
verifier = secrets.token_urlsafe(64)
|
|
37
|
-
challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).decode().rstrip("=")
|
|
38
|
-
return verifier, challenge
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
class _CallbackResult:
|
|
42
|
-
def __init__(self) -> None:
|
|
43
|
-
self.code: Optional[str] = None
|
|
44
|
-
self.state: Optional[str] = None
|
|
45
|
-
self.error: Optional[str] = None
|
|
46
|
-
self.error_description: Optional[str] = None
|
|
47
|
-
self.event = threading.Event()
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
def _make_handler(result: _CallbackResult, expected_state: str):
|
|
51
|
-
class Handler(BaseHTTPRequestHandler):
|
|
52
|
-
def log_message(self, format: str, *args) -> None: # noqa: A003
|
|
53
|
-
return
|
|
54
|
-
|
|
55
|
-
def do_GET(self) -> None: # noqa: N802
|
|
56
|
-
parsed = urlparse(self.path)
|
|
57
|
-
if parsed.path.rstrip("/") != "/callback":
|
|
58
|
-
self.send_response(404)
|
|
59
|
-
self.end_headers()
|
|
60
|
-
return
|
|
61
|
-
|
|
62
|
-
qs = parse_qs(parsed.query)
|
|
63
|
-
result.error = (qs.get("error") or [None])[0]
|
|
64
|
-
result.error_description = (qs.get("error_description") or [None])[0]
|
|
65
|
-
result.code = (qs.get("code") or [None])[0]
|
|
66
|
-
result.state = (qs.get("state") or [None])[0]
|
|
67
|
-
|
|
68
|
-
if result.error:
|
|
69
|
-
body = (
|
|
70
|
-
b"<html><body><h1>Login failed</h1>"
|
|
71
|
-
b"<p>You can close this window and return to the terminal.</p></body></html>"
|
|
72
|
-
)
|
|
73
|
-
self.send_response(400)
|
|
74
|
-
elif result.state != expected_state:
|
|
75
|
-
result.error = "invalid_state"
|
|
76
|
-
result.error_description = "OAuth state mismatch"
|
|
77
|
-
body = (
|
|
78
|
-
b"<html><body><h1>Login failed</h1>"
|
|
79
|
-
b"<p>State mismatch. Close this window and try again.</p></body></html>"
|
|
80
|
-
)
|
|
81
|
-
self.send_response(400)
|
|
82
|
-
else:
|
|
83
|
-
body = (
|
|
84
|
-
b"<html><body><h1>Logged in</h1>"
|
|
85
|
-
b"<p>You can close this window and return to the terminal.</p></body></html>"
|
|
86
|
-
)
|
|
87
|
-
self.send_response(200)
|
|
88
|
-
|
|
89
|
-
self.send_header("Content-Type", "text/html; charset=utf-8")
|
|
90
|
-
self.send_header("Content-Length", str(len(body)))
|
|
91
|
-
self.end_headers()
|
|
92
|
-
self.wfile.write(body)
|
|
93
|
-
result.event.set()
|
|
94
|
-
|
|
95
|
-
return Handler
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
def _exchange_code(issuer: str, code: str, verifier: str) -> str:
|
|
99
|
-
response = httpx.post(
|
|
100
|
-
f"{issuer}/oauth/token/",
|
|
101
|
-
data={
|
|
102
|
-
"grant_type": "authorization_code",
|
|
103
|
-
"code": code,
|
|
104
|
-
"redirect_uri": OAUTH_REDIRECT_URI,
|
|
105
|
-
"client_id": OAUTH_CLIENT_ID,
|
|
106
|
-
"code_verifier": verifier,
|
|
107
|
-
},
|
|
108
|
-
headers={"Accept": "application/json"},
|
|
109
|
-
timeout=30.0,
|
|
110
|
-
)
|
|
111
|
-
if response.status_code != 200:
|
|
112
|
-
detail = response.text[:200]
|
|
113
|
-
raise RuntimeError(f"Token exchange failed ({response.status_code}): {detail}")
|
|
114
|
-
payload = response.json()
|
|
115
|
-
access_token = payload.get("access_token")
|
|
116
|
-
if not access_token:
|
|
117
|
-
raise RuntimeError("Token exchange response missing access_token")
|
|
118
|
-
return access_token
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
def _fetch_api_key(issuer: str, access_token: str) -> dict:
|
|
122
|
-
response = httpx.get(
|
|
123
|
-
f"{issuer}/oauth/api/cli-credentials/",
|
|
124
|
-
headers={"Authorization": f"Bearer {access_token}", "Accept": "application/json"},
|
|
125
|
-
timeout=30.0,
|
|
126
|
-
)
|
|
127
|
-
if response.status_code != 200:
|
|
128
|
-
detail = response.text[:200]
|
|
129
|
-
raise RuntimeError(f"Could not fetch API key ({response.status_code}): {detail}")
|
|
130
|
-
payload = response.json()
|
|
131
|
-
if not payload.get("api_key"):
|
|
132
|
-
raise RuntimeError("Credentials response missing api_key")
|
|
133
|
-
return payload
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
def run_browser_login(*, print_url=None) -> dict:
|
|
137
|
-
"""Run the full browser OAuth flow.
|
|
138
|
-
|
|
139
|
-
Returns credentials dict with ``api_key`` and ``email``.
|
|
140
|
-
``print_url`` is called with the authorize URL (so the CLI can show it if
|
|
141
|
-
the browser fails to open).
|
|
142
|
-
"""
|
|
143
|
-
issuer = _oauth_issuer()
|
|
144
|
-
verifier, challenge = _pkce_pair()
|
|
145
|
-
state = secrets.token_urlsafe(24)
|
|
146
|
-
result = _CallbackResult()
|
|
147
|
-
|
|
148
|
-
try:
|
|
149
|
-
server = HTTPServer(("127.0.0.1", OAUTH_CALLBACK_PORT), _make_handler(result, state))
|
|
150
|
-
except OSError as exc:
|
|
151
|
-
raise RuntimeError(
|
|
152
|
-
f"Port {OAUTH_CALLBACK_PORT} is already in use. "
|
|
153
|
-
f"Close the other process and run `databar login` again."
|
|
154
|
-
) from exc
|
|
155
|
-
|
|
156
|
-
thread = threading.Thread(target=server.serve_forever, daemon=True)
|
|
157
|
-
thread.start()
|
|
158
|
-
|
|
159
|
-
params = {
|
|
160
|
-
"client_id": OAUTH_CLIENT_ID,
|
|
161
|
-
"redirect_uri": OAUTH_REDIRECT_URI,
|
|
162
|
-
"response_type": "code",
|
|
163
|
-
"scope": OAUTH_SCOPE,
|
|
164
|
-
"code_challenge": challenge,
|
|
165
|
-
"code_challenge_method": "S256",
|
|
166
|
-
"state": state,
|
|
167
|
-
}
|
|
168
|
-
authorize_url = f"{issuer}/oauth/authorize/?{urlencode(params)}"
|
|
169
|
-
|
|
170
|
-
opened = webbrowser.open(authorize_url)
|
|
171
|
-
if print_url is not None:
|
|
172
|
-
print_url(authorize_url, opened=opened)
|
|
173
|
-
|
|
174
|
-
if not result.event.wait(timeout=_LOGIN_TIMEOUT_S):
|
|
175
|
-
server.shutdown()
|
|
176
|
-
thread.join(timeout=2)
|
|
177
|
-
raise RuntimeError("Timed out waiting for browser login. Run `databar login` again.")
|
|
178
|
-
|
|
179
|
-
server.shutdown()
|
|
180
|
-
thread.join(timeout=2)
|
|
181
|
-
|
|
182
|
-
if result.error:
|
|
183
|
-
desc = result.error_description or result.error
|
|
184
|
-
raise RuntimeError(f"Login was denied or failed: {desc}")
|
|
185
|
-
|
|
186
|
-
if not result.code:
|
|
187
|
-
raise RuntimeError("No authorization code received from the browser callback.")
|
|
188
|
-
|
|
189
|
-
access_token = _exchange_code(issuer, result.code, verifier)
|
|
190
|
-
return _fetch_api_key(issuer, access_token)
|
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
"""Unit tests for CLI OAuth helpers (token exchange + credentials fetch)."""
|
|
2
|
-
|
|
3
|
-
from __future__ import annotations
|
|
4
|
-
|
|
5
|
-
import httpx
|
|
6
|
-
import pytest
|
|
7
|
-
import respx
|
|
8
|
-
|
|
9
|
-
from databar.cli._oauth import (
|
|
10
|
-
OAUTH_CLIENT_ID,
|
|
11
|
-
OAUTH_REDIRECT_URI,
|
|
12
|
-
_exchange_code,
|
|
13
|
-
_fetch_api_key,
|
|
14
|
-
_pkce_pair,
|
|
15
|
-
)
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
def test_pkce_pair_s256_shape():
|
|
19
|
-
verifier, challenge = _pkce_pair()
|
|
20
|
-
assert len(verifier) > 40
|
|
21
|
-
assert len(challenge) >= 43
|
|
22
|
-
assert "=" not in challenge
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
@respx.mock
|
|
26
|
-
def test_exchange_code_posts_pkce_fields():
|
|
27
|
-
route = respx.post("https://databar.ai/oauth/token/").mock(
|
|
28
|
-
return_value=httpx.Response(200, json={"access_token": "tok", "token_type": "Bearer"})
|
|
29
|
-
)
|
|
30
|
-
token = _exchange_code("https://databar.ai", "auth-code", "verifier")
|
|
31
|
-
assert token == "tok"
|
|
32
|
-
assert route.called
|
|
33
|
-
request = route.calls.last.request
|
|
34
|
-
body = request.content.decode()
|
|
35
|
-
assert "grant_type=authorization_code" in body
|
|
36
|
-
assert "code=auth-code" in body
|
|
37
|
-
assert f"client_id={OAUTH_CLIENT_ID}" in body
|
|
38
|
-
assert "code_verifier=verifier" in body
|
|
39
|
-
assert OAUTH_REDIRECT_URI.replace(":", "%3A").replace("/", "%2F") in body or "127.0.0.1" in body
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
@respx.mock
|
|
43
|
-
def test_fetch_api_key_sends_bearer():
|
|
44
|
-
respx.get("https://databar.ai/oauth/api/cli-credentials/").mock(
|
|
45
|
-
return_value=httpx.Response(
|
|
46
|
-
200,
|
|
47
|
-
json={"api_key": "k", "email": "a@b.com", "workspace_identifier": "w", "workspace_name": "W"},
|
|
48
|
-
)
|
|
49
|
-
)
|
|
50
|
-
payload = _fetch_api_key("https://databar.ai", "tok")
|
|
51
|
-
assert payload["api_key"] == "k"
|
|
52
|
-
assert payload["email"] == "a@b.com"
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
@respx.mock
|
|
56
|
-
def test_exchange_code_raises_on_error():
|
|
57
|
-
respx.post("https://databar.ai/oauth/token/").mock(
|
|
58
|
-
return_value=httpx.Response(400, text="bad")
|
|
59
|
-
)
|
|
60
|
-
with pytest.raises(RuntimeError, match="Token exchange failed"):
|
|
61
|
-
_exchange_code("https://databar.ai", "bad", "v")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|