mirror of
https://github.com/HKUDS/DeepTutor.git
synced 2026-12-02 11:30:26 +08:00
For a remote or containerized deployment the browser's localhost is the user's workstation while the OAuth listener runs on the server, so a reverse proxy cannot deliver http://localhost:1455/auth/callback to that private listener and Codex sign-in could never complete. Keep the listener loopback-only and forward just the exact browser callback path through the Web origin to a state-validating broker. The settings card and CLI now show the real callback address, the SSH tunnel command, an explicit authorize action and a countdown, and survive a page reload; the deployment docs cover the Docker host-port and reverse-proxy caveats. The listener stays bound to loopback and ports 1455/1457 are never published; the Web bridge rewrites only /auth/callback; the broker requires an active operation and a constant-time state match; callback responses are no-store HTML that never echo the code or state; PKCE and the token exchange are unchanged. Binding the listener with start_serving=False also closes a window where a connection could be accepted before the callback object existed.
104 lines
3.8 KiB
Python
104 lines
3.8 KiB
Python
"""CLI commands for provider auth and access validation."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import webbrowser
|
|
|
|
import typer
|
|
|
|
from deeptutor.services.codex_auth import CodexAuthError, get_codex_oauth_service
|
|
|
|
from .common import maybe_run
|
|
|
|
|
|
def register(app: typer.Typer) -> None:
|
|
@app.command("login")
|
|
def provider_login(
|
|
provider: str = typer.Argument(
|
|
...,
|
|
help="Provider: openai-codex (OAuth login) | github-copilot (validate existing Copilot auth)",
|
|
),
|
|
) -> None:
|
|
"""Authenticate or validate provider access."""
|
|
key = provider.strip().lower().replace("-", "_")
|
|
if key == "openai_codex":
|
|
maybe_run(_login_openai_codex())
|
|
return
|
|
if key == "github_copilot":
|
|
maybe_run(_login_github_copilot())
|
|
return
|
|
raise typer.BadParameter(
|
|
f"Unknown provider `{provider}`. Supported: openai-codex, github-copilot"
|
|
)
|
|
|
|
|
|
async def _login_openai_codex() -> None:
|
|
service = get_codex_oauth_service()
|
|
try:
|
|
started = await service.start_login()
|
|
authorize_url = str(started["authorize_url"])
|
|
typer.echo(f"Callback: {started['redirect_uri']}")
|
|
typer.echo(f"Authorization URL: {authorize_url}")
|
|
typer.echo(f"Remote server tunnel command: {started['ssh_forward_command']}")
|
|
typer.echo(
|
|
"Opening the OpenAI Codex sign-in in your browser; "
|
|
"credentials are written only to DeepTutor's private directory."
|
|
)
|
|
if not webbrowser.open(authorize_url):
|
|
typer.echo(f"The browser did not open automatically. Visit: {authorize_url}")
|
|
|
|
while True:
|
|
status = service.public_status()
|
|
operation_state = status.get("operation_state")
|
|
if operation_state == "completed":
|
|
typer.echo(
|
|
f"OpenAI Codex sign-in succeeded. Models available: "
|
|
f"{status.get('model_count', 0)}."
|
|
)
|
|
active_model = status.get("active_model")
|
|
if active_model:
|
|
typer.echo(f"Codex is the active model: {active_model}")
|
|
else:
|
|
typer.echo("Select a Codex model in Settings to start using it.")
|
|
return
|
|
if operation_state in {"failed", "expired", "cancelled"}:
|
|
error_code = status.get("error_code") or operation_state
|
|
typer.echo(f"OpenAI Codex sign-in did not complete: {error_code}")
|
|
raise typer.Exit(code=1)
|
|
await asyncio.sleep(0.5)
|
|
except asyncio.CancelledError:
|
|
await service.cancel_login()
|
|
typer.echo("Cancelled the OpenAI Codex sign-in.")
|
|
raise typer.Exit(code=130) from None
|
|
except CodexAuthError as exc:
|
|
typer.echo(f"OpenAI Codex sign-in failed: {exc.public_message}")
|
|
raise typer.Exit(code=1)
|
|
|
|
|
|
async def _login_github_copilot() -> None:
|
|
"""Validate an existing GitHub Copilot auth session via a lightweight request."""
|
|
try:
|
|
from openai import AsyncOpenAI
|
|
except ImportError:
|
|
typer.echo(
|
|
"openai is not installed. Install CLI deps from a local checkout: "
|
|
"python -m pip install -e ./packaging/deeptutor-cli"
|
|
)
|
|
raise typer.Exit(code=1)
|
|
try:
|
|
client = AsyncOpenAI(
|
|
api_key="copilot",
|
|
base_url="https://api.githubcopilot.com",
|
|
max_retries=0,
|
|
)
|
|
await client.chat.completions.create(
|
|
model="gpt-4o",
|
|
messages=[{"role": "user", "content": "ping"}],
|
|
max_tokens=1,
|
|
)
|
|
except Exception as exc:
|
|
typer.echo(f"GitHub Copilot auth validation failed: {exc}")
|
|
raise typer.Exit(code=1) from exc
|
|
typer.echo("GitHub Copilot auth validation succeeded.")
|