2026-08-23 22:14:37 +08:00
|
|
|
"""WebUI OAuth connection flow for Linear workspace installations."""
|
|
|
|
|
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
import asyncio
|
|
|
|
|
import time
|
|
|
|
|
from dataclasses import dataclass, field
|
|
|
|
|
from typing import Any, cast
|
|
|
|
|
|
|
|
|
|
from nanobot.channels.connect import ChannelConnectError, QueryParams, query_first
|
2026-09-25 15:19:49 +08:00
|
|
|
from nanobot.channels.linear.access import member_allowed
|
2026-08-23 22:14:37 +08:00
|
|
|
from nanobot.channels.linear.client import LinearApiError, LinearClient
|
|
|
|
|
from nanobot.channels.linear.config import LinearConfig
|
|
|
|
|
from nanobot.channels.linear.oauth import (
|
|
|
|
|
FLOW_TTL_SECONDS,
|
2026-09-23 01:32:19 +08:00
|
|
|
LINEAR_SCOPES,
|
2026-08-23 22:14:37 +08:00
|
|
|
OAUTH_FLOWS,
|
|
|
|
|
LinearOAuthFlow,
|
|
|
|
|
authorization_url,
|
|
|
|
|
)
|
|
|
|
|
from nanobot.channels.linear.server import LinearServerLease, acquire_http_server
|
|
|
|
|
from nanobot.channels.linear.state import LinearStateStore
|
|
|
|
|
from nanobot.config.loader import load_config
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclass(slots=True)
|
|
|
|
|
class LinearConnectSession:
|
|
|
|
|
flow: LinearOAuthFlow
|
|
|
|
|
config: LinearConfig
|
|
|
|
|
state: LinearStateStore
|
2026-10-21 19:16:56 +08:00
|
|
|
server: LinearServerLease | None
|
2026-08-23 22:14:37 +08:00
|
|
|
completion_lock: asyncio.Lock = field(default_factory=asyncio.Lock, repr=False)
|
2026-09-23 01:32:19 +08:00
|
|
|
result: dict[str, Any] | None = None
|
2026-10-21 19:16:56 +08:00
|
|
|
expiry_task: asyncio.Task[None] | None = None
|
2026-08-23 22:14:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class LinearConnectStore:
|
|
|
|
|
"""Keep short-lived OAuth browser sessions in the gateway process."""
|
|
|
|
|
|
|
|
|
|
def __init__(self) -> None:
|
|
|
|
|
self._sessions: dict[str, LinearConnectSession] = {}
|
|
|
|
|
|
2026-10-21 19:16:56 +08:00
|
|
|
@property
|
|
|
|
|
def has_pending_work(self) -> bool:
|
|
|
|
|
# A deadline alone does not close the callback/webhook listener. Cached
|
|
|
|
|
# success, however, is retained only after releasing that listener.
|
|
|
|
|
return any(session.server is not None for session in self._sessions.values())
|
|
|
|
|
|
|
|
|
|
async def close(self) -> None:
|
|
|
|
|
for session_id in tuple(self._sessions):
|
|
|
|
|
await self.cancel(session_id)
|
|
|
|
|
|
2026-08-23 22:14:37 +08:00
|
|
|
async def handle(self, action: str, query: QueryParams) -> dict[str, Any]:
|
|
|
|
|
await self._cleanup()
|
|
|
|
|
if action == "start":
|
2026-09-23 01:32:19 +08:00
|
|
|
operation = (query_first(query, "operation") or "connect").strip().lower()
|
|
|
|
|
if operation == "inspect":
|
|
|
|
|
return self.inspect()
|
2026-09-25 23:10:49 +08:00
|
|
|
if operation == "workspace_profile":
|
|
|
|
|
organization_id = (query_first(query, "organization_id") or "").strip()
|
|
|
|
|
if not organization_id:
|
|
|
|
|
raise ChannelConnectError("missing Linear workspace")
|
|
|
|
|
return await self.workspace_profile(organization_id)
|
2026-09-23 01:32:19 +08:00
|
|
|
if operation == "disconnect":
|
|
|
|
|
organization_id = (query_first(query, "organization_id") or "").strip()
|
|
|
|
|
if not organization_id:
|
|
|
|
|
raise ChannelConnectError("missing Linear workspace")
|
|
|
|
|
return await self.disconnect(organization_id)
|
2026-09-25 15:19:49 +08:00
|
|
|
if operation in {"members", "member_access"}:
|
|
|
|
|
organization_id = (query_first(query, "organization_id") or "").strip()
|
|
|
|
|
if not organization_id:
|
|
|
|
|
raise ChannelConnectError("missing Linear workspace")
|
|
|
|
|
user_id: str | None = None
|
|
|
|
|
allowed: bool | None = None
|
|
|
|
|
if operation == "member_access":
|
|
|
|
|
user_id = (query_first(query, "user_id") or "").strip()
|
|
|
|
|
raw_allowed = query_first(query, "allowed")
|
|
|
|
|
if not user_id or raw_allowed not in {"true", "false"}:
|
|
|
|
|
raise ChannelConnectError("member access requires a user ID and true/false")
|
|
|
|
|
allowed = raw_allowed == "true"
|
|
|
|
|
return await self.members(organization_id, user_id=user_id, allowed=allowed)
|
2026-09-23 01:32:19 +08:00
|
|
|
if operation != "connect":
|
|
|
|
|
raise ChannelConnectError(f"unsupported Linear connect operation: {operation}")
|
2026-08-23 22:14:37 +08:00
|
|
|
return await self.start(force=_query_bool(query, "force"))
|
|
|
|
|
session_id = (query_first(query, "session_id") or "").strip()
|
|
|
|
|
if not session_id:
|
|
|
|
|
raise ChannelConnectError("missing Linear connect session")
|
|
|
|
|
if action == "poll":
|
|
|
|
|
return await self.poll(session_id)
|
|
|
|
|
if action == "cancel":
|
|
|
|
|
return await self.cancel(session_id)
|
|
|
|
|
raise ChannelConnectError(f"unsupported Linear connect action: {action}", status=404)
|
|
|
|
|
|
|
|
|
|
async def start(self, *, force: bool = False) -> dict[str, Any]:
|
|
|
|
|
config = _load_linear_config()
|
|
|
|
|
try:
|
|
|
|
|
config.validate_runtime()
|
|
|
|
|
except ValueError as exc:
|
|
|
|
|
raise ChannelConnectError(str(exc)) from exc
|
|
|
|
|
state = LinearStateStore()
|
|
|
|
|
if state.has_installations(config.client_id) and not force:
|
|
|
|
|
return {
|
|
|
|
|
"session_id": "",
|
|
|
|
|
"status": "succeeded",
|
|
|
|
|
"message": "Linear is already connected. Use reconnect to add or replace a workspace.",
|
2026-09-23 01:32:19 +08:00
|
|
|
"installations": _installation_payloads(state, config.client_id),
|
2026-08-23 22:14:37 +08:00
|
|
|
}
|
|
|
|
|
flow = OAUTH_FLOWS.create()
|
|
|
|
|
try:
|
|
|
|
|
lease = await asyncio.to_thread(acquire_http_server, config, state)
|
|
|
|
|
except (OSError, RuntimeError) as exc:
|
|
|
|
|
OAUTH_FLOWS.remove(flow)
|
|
|
|
|
raise ChannelConnectError(
|
|
|
|
|
f"Unable to start the Linear callback listener on {config.host}:{config.port}: {exc}",
|
|
|
|
|
status=502,
|
|
|
|
|
) from exc
|
2026-10-21 19:16:56 +08:00
|
|
|
session = LinearConnectSession(
|
2026-08-23 22:14:37 +08:00
|
|
|
flow=flow,
|
|
|
|
|
config=config,
|
|
|
|
|
state=state,
|
|
|
|
|
server=lease,
|
|
|
|
|
)
|
2026-10-21 19:16:56 +08:00
|
|
|
self._sessions[flow.session_id] = session
|
|
|
|
|
session.expiry_task = asyncio.create_task(
|
|
|
|
|
self._expire_session(session), name=f"linear-connect-expire-{flow.session_id}",
|
|
|
|
|
)
|
|
|
|
|
session.expiry_task.add_done_callback(
|
|
|
|
|
lambda task: task.exception() if not task.cancelled() else None,
|
|
|
|
|
)
|
2026-08-23 22:14:37 +08:00
|
|
|
return {
|
|
|
|
|
"session_id": flow.session_id,
|
|
|
|
|
"status": "pending",
|
|
|
|
|
"authorization_url": authorization_url(config, flow),
|
|
|
|
|
"qr_url": authorization_url(config, flow),
|
|
|
|
|
"redirect_uri": config.redirect_uri,
|
|
|
|
|
"webhook_url": config.webhook_url,
|
|
|
|
|
"interval_ms": 1500,
|
|
|
|
|
"expires_at_ms": int((flow.created_at + FLOW_TTL_SECONDS) * 1000),
|
|
|
|
|
"message": "Authorize the nanobot app in Linear.",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async def poll(self, session_id: str) -> dict[str, Any]:
|
|
|
|
|
session = self._sessions.get(session_id)
|
|
|
|
|
if session is None:
|
|
|
|
|
return _terminal(session_id, "expired", "This Linear authorization has expired.")
|
|
|
|
|
async with session.completion_lock:
|
|
|
|
|
if self._sessions.get(session_id) is not session:
|
|
|
|
|
return _terminal(session_id, "cancelled", "Linear authorization cancelled.")
|
2026-09-19 00:08:37 +08:00
|
|
|
if session.result is not None:
|
2026-10-21 19:16:56 +08:00
|
|
|
await self._release_listener(session)
|
2026-09-19 00:08:37 +08:00
|
|
|
return dict(session.result)
|
2026-08-23 22:14:37 +08:00
|
|
|
flow = session.flow
|
|
|
|
|
if time.monotonic() >= flow.deadline:
|
|
|
|
|
await self._close_session(session_id)
|
|
|
|
|
return _terminal(session_id, "expired", "This Linear authorization has expired.")
|
|
|
|
|
if flow.error:
|
|
|
|
|
message = "Linear authorization was cancelled."
|
|
|
|
|
await self._close_session(session_id)
|
|
|
|
|
return _terminal(session_id, "failed", message)
|
|
|
|
|
if not flow.code:
|
|
|
|
|
return {
|
|
|
|
|
"session_id": session_id,
|
|
|
|
|
"status": "pending",
|
|
|
|
|
"interval_ms": 1500,
|
|
|
|
|
"expires_at_ms": int((flow.created_at + FLOW_TTL_SECONDS) * 1000),
|
|
|
|
|
"message": "Waiting for Linear authorization.",
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client = LinearClient(session.config, session.state)
|
|
|
|
|
try:
|
|
|
|
|
installation = await client.exchange_code(flow.code, flow.verifier)
|
|
|
|
|
except LinearApiError as exc:
|
|
|
|
|
await self._close_session(session_id)
|
|
|
|
|
return _terminal(
|
|
|
|
|
session_id,
|
|
|
|
|
"failed",
|
|
|
|
|
f"Linear authorization could not be completed: {exc}",
|
|
|
|
|
)
|
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
2026-09-19 00:08:37 +08:00
|
|
|
session.result = {
|
2026-08-23 22:14:37 +08:00
|
|
|
"session_id": session_id,
|
|
|
|
|
"status": "succeeded",
|
|
|
|
|
"message": "Linear is connected.",
|
|
|
|
|
"organization_id": installation.organization_id,
|
|
|
|
|
"organization_name": installation.organization_name,
|
2026-09-23 01:32:19 +08:00
|
|
|
"installations": _installation_payloads(session.state, session.config.client_id),
|
2026-08-23 22:14:37 +08:00
|
|
|
}
|
2026-09-19 00:08:37 +08:00
|
|
|
# A browser may miss the first successful poll while switching tabs.
|
|
|
|
|
# Retain its result until the session expires, but release the callback listener.
|
2026-10-21 19:16:56 +08:00
|
|
|
await self._release_listener(session)
|
2026-09-19 00:08:37 +08:00
|
|
|
return dict(session.result)
|
2026-08-23 22:14:37 +08:00
|
|
|
|
|
|
|
|
async def cancel(self, session_id: str) -> dict[str, Any]:
|
|
|
|
|
session = self._sessions.get(session_id)
|
|
|
|
|
if session is not None:
|
|
|
|
|
async with session.completion_lock:
|
|
|
|
|
await self._close_session(session_id)
|
|
|
|
|
return _terminal(session_id, "cancelled", "Linear authorization cancelled.")
|
|
|
|
|
|
2026-09-23 01:32:19 +08:00
|
|
|
def inspect(self) -> dict[str, Any]:
|
|
|
|
|
config = _load_linear_config()
|
|
|
|
|
state = LinearStateStore()
|
|
|
|
|
installations = (
|
|
|
|
|
_installation_payloads(state, config.client_id) if config.client_id else []
|
|
|
|
|
)
|
|
|
|
|
return {
|
|
|
|
|
"session_id": "",
|
|
|
|
|
"status": "inspected",
|
|
|
|
|
"message": (
|
|
|
|
|
f"{len(installations)} Linear workspace(s) authorized."
|
|
|
|
|
if installations
|
|
|
|
|
else "No Linear workspaces are authorized."
|
|
|
|
|
),
|
|
|
|
|
"installations": installations,
|
|
|
|
|
"webhook_url": config.webhook_url if config.public_base_url else "",
|
|
|
|
|
"redirect_uri": config.redirect_uri if config.public_base_url else "",
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-25 23:10:49 +08:00
|
|
|
async def workspace_profile(self, organization_id: str) -> dict[str, Any]:
|
|
|
|
|
config = _load_linear_config()
|
|
|
|
|
state = LinearStateStore()
|
|
|
|
|
installation = state.installation(organization_id)
|
|
|
|
|
if installation is None or installation.oauth_client_id != config.client_id:
|
|
|
|
|
raise ChannelConnectError("Linear workspace is not connected", status=404)
|
|
|
|
|
client = LinearClient(config, state)
|
|
|
|
|
try:
|
|
|
|
|
profile = await client.workspace_profile(organization_id)
|
|
|
|
|
except LinearApiError as exc:
|
|
|
|
|
raise ChannelConnectError("Unable to read Linear workspace profile", status=502) from exc
|
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
|
|
|
|
return {"session_id": "", "status": "workspace_profile", **profile}
|
|
|
|
|
|
2026-09-25 15:19:49 +08:00
|
|
|
async def members(
|
|
|
|
|
self, organization_id: str, *, user_id: str | None = None, allowed: bool | None = None,
|
|
|
|
|
) -> dict[str, Any]:
|
|
|
|
|
config = _load_linear_config()
|
|
|
|
|
state = LinearStateStore()
|
|
|
|
|
installation = state.installation(organization_id)
|
|
|
|
|
if installation is None or installation.oauth_client_id != config.client_id:
|
|
|
|
|
raise ChannelConnectError("Linear workspace is not connected", status=404)
|
|
|
|
|
client = LinearClient(config, state)
|
|
|
|
|
try:
|
|
|
|
|
members = await client.list_members(organization_id, user_id=user_id)
|
|
|
|
|
except LinearApiError as exc:
|
|
|
|
|
raise ChannelConnectError(f"Unable to read Linear members: {exc}", status=502) from exc
|
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
|
|
|
|
if user_id is not None and allowed is not None:
|
|
|
|
|
if not any(member["id"] == user_id for member in members):
|
|
|
|
|
raise ChannelConnectError("Member is not active in an accessible Linear team", status=404)
|
|
|
|
|
try:
|
2026-10-01 00:27:11 +08:00
|
|
|
state.set_member_access(
|
|
|
|
|
config.client_id, organization_id, user_id, allowed=allowed,
|
|
|
|
|
expected_authorized_at=installation.authorized_at,
|
|
|
|
|
)
|
2026-09-25 15:19:49 +08:00
|
|
|
except ValueError as exc:
|
|
|
|
|
raise ChannelConnectError(str(exc), status=409) from exc
|
|
|
|
|
return {
|
|
|
|
|
"session_id": "",
|
|
|
|
|
"status": "members" if allowed is None else "member_access_saved",
|
|
|
|
|
"organization_id": organization_id,
|
|
|
|
|
"legacy_allow_all": "*" in config.allow_from,
|
|
|
|
|
"members": [
|
|
|
|
|
{**member, "allowed": member_allowed(config, state, organization_id, member["id"])}
|
|
|
|
|
for member in members
|
|
|
|
|
],
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-23 01:32:19 +08:00
|
|
|
async def disconnect(self, organization_id: str) -> dict[str, Any]:
|
|
|
|
|
config = _load_linear_config()
|
|
|
|
|
state = LinearStateStore()
|
|
|
|
|
installation = state.installation(organization_id)
|
|
|
|
|
if installation is None or installation.oauth_client_id != config.client_id:
|
|
|
|
|
raise ChannelConnectError("Linear workspace is not connected", status=404)
|
|
|
|
|
client = LinearClient(config, state)
|
|
|
|
|
try:
|
|
|
|
|
await client.revoke_installation(installation)
|
|
|
|
|
except LinearApiError as exc:
|
|
|
|
|
raise ChannelConnectError(
|
|
|
|
|
f"Unable to revoke the Linear workspace authorization: {exc}",
|
|
|
|
|
status=502,
|
|
|
|
|
) from exc
|
|
|
|
|
finally:
|
|
|
|
|
await client.close()
|
2026-09-26 17:31:20 +08:00
|
|
|
removed = state.delete_installation(organization_id, expected=installation)
|
|
|
|
|
if not removed and state.installation(organization_id) is not None:
|
|
|
|
|
raise ChannelConnectError(
|
|
|
|
|
"Linear workspace authorization changed while disconnecting. Refresh and try again.",
|
|
|
|
|
status=409,
|
|
|
|
|
)
|
2026-09-23 01:32:19 +08:00
|
|
|
installations = _installation_payloads(state, config.client_id)
|
|
|
|
|
return {
|
|
|
|
|
"session_id": "",
|
|
|
|
|
"status": "disconnected",
|
|
|
|
|
"message": f"Disconnected {installation.organization_name or organization_id}.",
|
|
|
|
|
"organization_id": organization_id,
|
|
|
|
|
"installations": installations,
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-23 22:14:37 +08:00
|
|
|
async def _cleanup(self) -> None:
|
|
|
|
|
expired = [
|
|
|
|
|
session_id
|
|
|
|
|
for session_id, session in self._sessions.items()
|
|
|
|
|
if time.monotonic() >= session.flow.deadline
|
|
|
|
|
]
|
|
|
|
|
for session_id in expired:
|
|
|
|
|
await self.cancel(session_id)
|
|
|
|
|
|
|
|
|
|
async def _close_session(self, session_id: str) -> None:
|
2026-10-21 19:16:56 +08:00
|
|
|
session = self._sessions.get(session_id)
|
2026-08-23 22:14:37 +08:00
|
|
|
if session is None:
|
|
|
|
|
return
|
2026-10-21 19:16:56 +08:00
|
|
|
expiry_task = session.expiry_task
|
|
|
|
|
if expiry_task is not None and expiry_task is not asyncio.current_task():
|
|
|
|
|
expiry_task.cancel()
|
|
|
|
|
await asyncio.gather(expiry_task, return_exceptions=True)
|
|
|
|
|
await self._release_listener(session)
|
|
|
|
|
self._sessions.pop(session_id, None)
|
|
|
|
|
|
|
|
|
|
async def _expire_session(self, session: LinearConnectSession) -> None:
|
|
|
|
|
await asyncio.sleep(max(0, session.flow.deadline - time.monotonic()))
|
|
|
|
|
await self.cancel(session.flow.session_id)
|
|
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
|
async def _release_listener(session: LinearConnectSession) -> None:
|
2026-08-23 22:14:37 +08:00
|
|
|
OAUTH_FLOWS.remove(session.flow)
|
2026-10-21 19:16:56 +08:00
|
|
|
if session.server is not None:
|
|
|
|
|
await asyncio.to_thread(session.server.close)
|
|
|
|
|
session.server = None
|
2026-08-23 22:14:37 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _load_linear_config() -> LinearConfig:
|
|
|
|
|
config = load_config()
|
|
|
|
|
extras: dict[str, Any] = config.channels.model_extra or {}
|
|
|
|
|
raw: object = extras.get("linear", {})
|
|
|
|
|
if not isinstance(raw, dict):
|
|
|
|
|
raw = {}
|
|
|
|
|
return LinearConfig.model_validate(cast(dict[str, Any], raw))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _query_bool(query: QueryParams, key: str) -> bool:
|
|
|
|
|
return (query_first(query, key) or "").strip().lower() in {"1", "true", "yes"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _terminal(session_id: str, status: str, message: str) -> dict[str, Any]:
|
|
|
|
|
return {"session_id": session_id, "status": status, "message": message}
|
2026-09-23 01:32:19 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
def _installation_payloads(
|
|
|
|
|
state: LinearStateStore,
|
|
|
|
|
oauth_client_id: str | None,
|
|
|
|
|
) -> list[dict[str, Any]]:
|
|
|
|
|
now = time.time()
|
|
|
|
|
payloads: list[dict[str, Any]] = []
|
|
|
|
|
for installation in state.list_installations(oauth_client_id):
|
|
|
|
|
missing_scopes = sorted(set(LINEAR_SCOPES) - set(installation.scope))
|
|
|
|
|
payloads.append({
|
|
|
|
|
"organization_id": installation.organization_id,
|
|
|
|
|
"organization_name": installation.organization_name,
|
|
|
|
|
"scopes": list(installation.scope),
|
|
|
|
|
"authorization_status": (
|
|
|
|
|
"missing_scopes"
|
|
|
|
|
if missing_scopes
|
|
|
|
|
else "refresh_required"
|
|
|
|
|
if installation.expires_at <= now + 60
|
|
|
|
|
else "authorized"
|
|
|
|
|
),
|
|
|
|
|
"missing_scopes": missing_scopes,
|
|
|
|
|
})
|
|
|
|
|
return payloads
|