|
1 | 1 | """muapi auth — configure API key and inspect identity.""" |
2 | | -import typer |
| 2 | +import os |
| 3 | +import re |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import webbrowser |
| 7 | + |
3 | 8 | import httpx |
4 | | -from rich.prompt import Prompt |
| 9 | +import typer |
| 10 | +from rich.prompt import Confirm, Prompt |
5 | 11 |
|
6 | | -from ..config import delete_api_key, get_api_key, save_api_key, BASE_URL |
| 12 | +from ..config import BASE_URL, _CONFIG_FILE, delete_api_key, get_api_key, get_key_info, save_api_key |
7 | 13 | from .. import exitcodes |
8 | 14 | from ..utils import console, error_exit, out |
9 | 15 |
|
10 | 16 | app = typer.Typer(help="Manage authentication and API key.") |
11 | 17 |
|
12 | | -# Auth endpoints live at the root host, not under /api/v1 |
13 | 18 | _AUTH_BASE = BASE_URL.replace("/api/v1", "") |
| 19 | +_ACCESS_KEYS_URL = "https://muapi.ai/access-keys" |
| 20 | + |
| 21 | +LINKS = { |
| 22 | + "dashboard": "https://muapi.ai/dashboard", |
| 23 | + "access-keys": _ACCESS_KEYS_URL, |
| 24 | + "models": "https://muapi.ai/models", |
| 25 | + "docs": "https://muapi.ai/docs", |
| 26 | + "pricing": "https://muapi.ai/pricing", |
| 27 | +} |
| 28 | + |
| 29 | + |
| 30 | +def _mask(key: str) -> str: |
| 31 | + if len(key) < 12: |
| 32 | + return "••••" |
| 33 | + return key[:8] + "…" + key[-4:] |
| 34 | + |
| 35 | + |
| 36 | +def _looks_like_key(s: str) -> bool: |
| 37 | + s = s.strip() |
| 38 | + return bool(re.match(r'^[A-Za-z0-9_\-]{20,}$', s) and '\n' not in s) |
| 39 | + |
| 40 | + |
| 41 | +def _read_clipboard() -> str | None: |
| 42 | + try: |
| 43 | + if sys.platform == "darwin": |
| 44 | + r = subprocess.run(["pbpaste"], capture_output=True, text=True, timeout=2) |
| 45 | + return r.stdout.strip() or None |
| 46 | + if sys.platform.startswith("linux"): |
| 47 | + for cmd in (["xclip", "-o"], ["xsel", "--clipboard", "--output"]): |
| 48 | + try: |
| 49 | + r = subprocess.run(cmd, capture_output=True, text=True, timeout=2) |
| 50 | + if r.returncode == 0: |
| 51 | + return r.stdout.strip() or None |
| 52 | + except FileNotFoundError: |
| 53 | + continue |
| 54 | + if sys.platform == "win32": |
| 55 | + r = subprocess.run( |
| 56 | + ["powershell", "-command", "Get-Clipboard"], |
| 57 | + capture_output=True, text=True, timeout=2, |
| 58 | + ) |
| 59 | + return r.stdout.strip() or None |
| 60 | + except Exception: |
| 61 | + pass |
| 62 | + return None |
| 63 | + |
| 64 | + |
| 65 | +def _validate_key(api_key: str) -> tuple[bool, str]: |
| 66 | + """Validate key against the live API. Returns (ok, error_msg).""" |
| 67 | + try: |
| 68 | + resp = httpx.get( |
| 69 | + f"{BASE_URL}/account/balance", |
| 70 | + headers={"x-api-key": api_key}, |
| 71 | + timeout=15.0, |
| 72 | + ) |
| 73 | + if resp.status_code in (401, 403): |
| 74 | + return False, "API rejected the key (401/403)." |
| 75 | + if resp.status_code >= 400: |
| 76 | + return False, f"API returned {resp.status_code}." |
| 77 | + return True, "" |
| 78 | + except httpx.RequestError as e: |
| 79 | + return False, f"Could not reach {BASE_URL}: {e}" |
| 80 | + |
| 81 | + |
| 82 | +def _find_project_config() -> str | None: |
| 83 | + """Walk up from CWD looking for muapi.json.""" |
| 84 | + from pathlib import Path |
| 85 | + d = Path.cwd() |
| 86 | + while True: |
| 87 | + candidate = d / "muapi.json" |
| 88 | + if candidate.exists(): |
| 89 | + return str(candidate) |
| 90 | + parent = d.parent |
| 91 | + if parent == d: |
| 92 | + return None |
| 93 | + d = parent |
| 94 | + |
| 95 | + |
| 96 | +def _do_save(api_key: str) -> None: |
| 97 | + with console.status("[dim]Validating with api.muapi.ai…[/dim]"): |
| 98 | + ok, reason = _validate_key(api_key) |
| 99 | + |
| 100 | + if not ok: |
| 101 | + console.print(f"[bold red]✖[/bold red] {reason}") |
| 102 | + console.print() |
| 103 | + console.print(f"[dim] Double-check at [/dim][cyan]{_ACCESS_KEYS_URL}[/cyan]") |
| 104 | + console.print("[dim] Or set [/dim][cyan]MUAPI_API_KEY[/cyan][dim] in your shell and skip this step.[/dim]\n") |
| 105 | + raise typer.Exit(exitcodes.AUTH_ERROR) |
| 106 | + |
| 107 | + location = save_api_key(api_key) |
| 108 | + location_display = "OS keychain" if location == "keychain" else str(_CONFIG_FILE) |
| 109 | + console.print("[bold green]✔[/bold green] Signed in.") |
| 110 | + console.print() |
| 111 | + console.print(f" [dim]Key: [/dim][green]{_mask(api_key)}[/green]") |
| 112 | + console.print(f" [dim]Stored: [/dim][cyan]{location_display}[/cyan]") |
| 113 | + console.print() |
| 114 | + console.print("[bold]Try it:[/bold]") |
| 115 | + console.print(" [cyan]muapi account balance[/cyan]") |
| 116 | + console.print(" [cyan]muapi image generate -p \"a cyberpunk skyline at golden hour\"[/cyan]") |
| 117 | + console.print(" [cyan]muapi video generate -p \"drone shot over snowy peaks\" --model kling-master[/cyan]") |
| 118 | + console.print() |
14 | 119 |
|
15 | 120 |
|
16 | 121 | @app.command("login") |
@@ -165,25 +270,85 @@ def reset_password( |
165 | 270 |
|
166 | 271 | @app.command("configure") |
167 | 272 | def configure( |
168 | | - api_key: str = typer.Option(None, "--api-key", "-k", help="API key (will prompt if omitted)"), |
| 273 | + api_key: str = typer.Option(None, "--api-key", "-k", help="API key (skips all prompts)"), |
| 274 | + no_browser: bool = typer.Option(False, "--no-browser", help="Skip opening the access-keys page"), |
169 | 275 | ): |
170 | | - """Save your muapi API key to the OS keychain (or config file).""" |
| 276 | + """Save your muapi API key — opens browser, detects clipboard, validates before saving.""" |
| 277 | + if api_key: |
| 278 | + _do_save(api_key.strip()) |
| 279 | + return |
| 280 | + |
| 281 | + console.print() |
| 282 | + console.print("[bold magenta] Welcome to muapi.[/bold magenta]") |
| 283 | + console.print("[dim] Sign in once and you're set on this machine.[/dim]\n") |
| 284 | + |
| 285 | + if not no_browser: |
| 286 | + console.print(f"[bold] 1.[/bold] Opening [cyan]{_ACCESS_KEYS_URL}[/cyan]") |
| 287 | + try: |
| 288 | + webbrowser.open(_ACCESS_KEYS_URL) |
| 289 | + except Exception: |
| 290 | + console.print("[dim] (browser launch failed — open the link manually)[/dim]") |
| 291 | + console.print("[bold] 2.[/bold] Copy your API key") |
| 292 | + console.print("[bold] 3.[/bold] Paste below — we'll validate it automatically\n") |
| 293 | + |
| 294 | + # Clipboard detection |
| 295 | + detected: str | None = None |
| 296 | + clip = _read_clipboard() |
| 297 | + if clip and _looks_like_key(clip): |
| 298 | + detected = clip |
| 299 | + |
| 300 | + if detected: |
| 301 | + use_it = Confirm.ask( |
| 302 | + f" Detected a key on your clipboard ({_mask(detected)}). Use it?", |
| 303 | + default=True, |
| 304 | + console=console, |
| 305 | + ) |
| 306 | + if use_it: |
| 307 | + api_key = detected |
| 308 | + |
171 | 309 | if not api_key: |
172 | | - api_key = Prompt.ask("[bold]Enter your muapi API key[/bold]", password=True, console=console) |
| 310 | + api_key = Prompt.ask("[bold] Paste your API key[/bold]", password=True, console=console) |
| 311 | + api_key = api_key.strip() |
| 312 | + |
173 | 313 | if not api_key: |
174 | | - error_exit("No API key provided.") |
175 | | - location = save_api_key(api_key.strip()) |
176 | | - console.print(f"[green]API key saved to {location}.[/green]") |
| 314 | + error_exit("No API key provided.", exitcodes.AUTH_ERROR) |
| 315 | + |
| 316 | + _do_save(api_key) |
| 317 | + |
| 318 | + |
| 319 | +@app.command("status") |
| 320 | +def status(): |
| 321 | + """Show the active API key, config location, base URL, and quick links.""" |
| 322 | + key, source = get_key_info() |
| 323 | + |
| 324 | + console.print() |
| 325 | + console.print("[bold]muapi CLI status[/bold]") |
| 326 | + if key: |
| 327 | + console.print(f" [dim]API key: [/dim][green]{_mask(key)}[/green]") |
| 328 | + else: |
| 329 | + console.print(f" [dim]API key: [/dim][red]not set — run [bold]muapi auth configure[/bold][/red]") |
| 330 | + console.print(f" [dim]Source: [/dim][cyan]{source}[/cyan]") |
| 331 | + console.print(f" [dim]Base URL: [/dim][cyan]{BASE_URL}[/cyan]") |
| 332 | + console.print(f" [dim]Config: [/dim][cyan]{_CONFIG_FILE}[/cyan]") |
| 333 | + |
| 334 | + project_file = _find_project_config() |
| 335 | + if project_file: |
| 336 | + console.print(f" [dim]Project: [/dim][cyan]{project_file}[/cyan] [dim](muapi.json detected)[/dim]") |
| 337 | + |
| 338 | + console.print() |
| 339 | + console.print("[bold]Useful links[/bold]") |
| 340 | + width = max(len(k) for k in LINKS) |
| 341 | + for name, url in LINKS.items(): |
| 342 | + console.print(f" [dim]{name.ljust(width)}[/dim] [cyan]{url}[/cyan]") |
| 343 | + console.print() |
| 344 | + console.print("[dim]Jump in your browser: [/dim][cyan]muapi open <target>[/cyan]") |
| 345 | + console.print() |
177 | 346 |
|
178 | 347 |
|
179 | 348 | @app.command("whoami") |
180 | 349 | def whoami(): |
181 | | - """Show the currently configured API key (masked).""" |
182 | | - key = get_api_key() |
183 | | - if not key: |
184 | | - error_exit("No API key configured. Run: muapi auth configure", exitcodes.AUTH_ERROR) |
185 | | - masked = key[:8] + "..." + key[-4:] |
186 | | - out.print(f"API key: [bold]{masked}[/bold]") |
| 350 | + """Alias for [bold]muapi auth status[/bold].""" |
| 351 | + status() |
187 | 352 |
|
188 | 353 |
|
189 | 354 | @app.command("logout") |
|
0 commit comments