fix(webui): cancel Copilot sign-in while loading device prompt
This commit is contained in:
1 parent
a44f28e656
commit
804e4c4ea6
3 files changed
+85
-4
No files matched your search
@@ -39,6 +39,7 @@ class GitHubCopilotOAuthFlow:
|
||||
def cancel(self) -> None:
|
||||
with self._commit_lock:
|
||||
self._cancelled.set()
|
||||
self._ready.set()
|
||||
|
||||
def _authorize(self, url: str, code: str, expires_in: int) -> None:
|
||||
self.authorization_url = url
|
||||
@@ -66,6 +67,8 @@ class GitHubCopilotOAuthFlow:
|
||||
if not self._ready.wait(timeout=15):
|
||||
self.cancel()
|
||||
raise RuntimeError("GitHub sign-in timed out. Start again.")
|
||||
if self.expired:
|
||||
raise RuntimeError("GitHub sign-in expired or cancelled. Start again.")
|
||||
if self._error is not None:
|
||||
raise RuntimeError("GitHub sign-in failed. Start again.") from None
|
||||
|
||||
|
||||
@@ -1599,13 +1599,15 @@ def login_oauth_provider(
|
||||
# instead of waiting for approval inside a blocking CLI login request.
|
||||
oauth_flows.clear(spec.name)
|
||||
copilot_flow = GitHubCopilotOAuthFlow()
|
||||
flow_id = secrets.token_urlsafe(24)
|
||||
# Own the flow before network I/O, so logout/replacement also cancels a
|
||||
# login that is still waiting for GitHub to return its device prompt.
|
||||
oauth_flows.register(spec.name, flow_id, copilot_flow)
|
||||
try:
|
||||
copilot_flow.start()
|
||||
except Exception:
|
||||
copilot_flow.cancel()
|
||||
oauth_flows.remove(spec.name, flow_id, copilot_flow)
|
||||
raise WebUISettingsError("GitHub sign-in failed. Start again.", status=502) from None
|
||||
flow_id = secrets.token_urlsafe(24)
|
||||
oauth_flows.register(spec.name, flow_id, copilot_flow)
|
||||
return {
|
||||
"status": "authorization_required",
|
||||
"provider": spec.name,
|
||||
|
||||
@@ -1,11 +1,18 @@
|
||||
"""The WebUI exposes only user-facing device data and owns cancellation."""
|
||||
|
||||
import threading
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
from types import SimpleNamespace
|
||||
|
||||
import pytest
|
||||
|
||||
from nanobot.config.schema import Config
|
||||
from nanobot.webui.settings_models import complete_oauth_provider, login_oauth_provider
|
||||
from nanobot.webui.settings_contracts import WebUISettingsError
|
||||
from nanobot.webui.settings_models import (
|
||||
complete_oauth_provider,
|
||||
login_oauth_provider,
|
||||
logout_oauth_provider,
|
||||
)
|
||||
from nanobot.webui.settings_services import WebUIOAuthFlowRegistry
|
||||
|
||||
|
||||
@@ -44,3 +51,72 @@ def test_copilot_prompt_poll_and_owned_cancellation(monkeypatch):
|
||||
assert stopped.wait(1)
|
||||
finally:
|
||||
registry.clear("github_copilot")
|
||||
|
||||
|
||||
@pytest.mark.parametrize("action", ["logout", "replacement"])
|
||||
def test_cancels_copilot_login_while_device_prompt_is_loading(monkeypatch, tmp_path, action):
|
||||
entered = threading.Event()
|
||||
release_prompt = threading.Event()
|
||||
finished = threading.Event()
|
||||
saved = []
|
||||
|
||||
def login(*, on_authorization, **_kwargs):
|
||||
if not entered.is_set():
|
||||
entered.set()
|
||||
assert release_prompt.wait(3)
|
||||
finished.set()
|
||||
on_authorization("https://github.com/login/device", "ABCD-EFGH", 60)
|
||||
return SimpleNamespace(access="late-account-secret")
|
||||
|
||||
monkeypatch.setattr("nanobot.providers.github_copilot_oauth.login_github_copilot", login)
|
||||
monkeypatch.setattr("nanobot.providers.github_copilot_provider.get_storage", lambda: SimpleNamespace(
|
||||
get_token_path=lambda: tmp_path / "copilot.json", save=saved.append,
|
||||
))
|
||||
registry = WebUIOAuthFlowRegistry()
|
||||
kwargs = dict(oauth_flows=registry, config_path=None, settings_payload=lambda **_: {})
|
||||
query = {"provider": ["github_copilot"]}
|
||||
with ThreadPoolExecutor(max_workers=1) as pool:
|
||||
started = pool.submit(login_oauth_provider, Config(), query, **kwargs)
|
||||
try:
|
||||
assert entered.wait(2)
|
||||
if action == "logout":
|
||||
logout_oauth_provider(query, **kwargs)
|
||||
else:
|
||||
replacement = login_oauth_provider(Config(), query, **kwargs)
|
||||
assert registry.get("github_copilot", replacement["flow_id"]) is not None
|
||||
# Cancellation must end the start request without waiting for the
|
||||
# outstanding GitHub device-code response to return.
|
||||
with pytest.raises(WebUISettingsError, match="GitHub sign-in failed"):
|
||||
started.result(timeout=1)
|
||||
assert saved == []
|
||||
finally:
|
||||
release_prompt.set()
|
||||
assert finished.wait(1)
|
||||
registry.clear("github_copilot")
|
||||
|
||||
|
||||
def test_failed_copilot_start_removes_registered_flow(monkeypatch):
|
||||
from nanobot.providers.github_copilot_oauth import GitHubCopilotOAuthFlow
|
||||
|
||||
registered = []
|
||||
registry = WebUIOAuthFlowRegistry()
|
||||
register = registry.register
|
||||
|
||||
def capture(provider, flow_id, flow):
|
||||
registered.append((provider, flow_id, flow))
|
||||
register(provider, flow_id, flow)
|
||||
|
||||
def fail_start(_self):
|
||||
raise RuntimeError("upstream failure")
|
||||
|
||||
monkeypatch.setattr(registry, "register", capture)
|
||||
monkeypatch.setattr(GitHubCopilotOAuthFlow, "start", fail_start)
|
||||
with pytest.raises(WebUISettingsError, match="GitHub sign-in failed"):
|
||||
login_oauth_provider(
|
||||
Config(), {"provider": ["github_copilot"]}, oauth_flows=registry,
|
||||
config_path=None, settings_payload=lambda **_: {},
|
||||
)
|
||||
assert len(registered) == 1
|
||||
provider, flow_id, flow = registered[0]
|
||||
assert registry.get(provider, flow_id) is None
|
||||
assert flow.expired
|
||||
Reference in new issue
Block a user