mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-08-09 04:40:57 +00:00
feat: report independent instance and workspace identities (#2394)
* feat: report independent instance and workspace identities * test: include workspace in OAuth callback fixture * ci: pin production cloud adapter to Space release * fix: preserve authenticated Workspace telemetry attribution * ci: pin production cloud adapter to final Space release --------- Co-authored-by: dadachann <185672915+dadachann@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import quart
|
||||
import argon2
|
||||
import asyncio
|
||||
import datetime
|
||||
import uuid
|
||||
from urllib.parse import parse_qs, urlsplit
|
||||
|
||||
@@ -218,7 +219,22 @@ class UserRouterGroup(group.RouterGroup):
|
||||
try:
|
||||
consumed_state = await self.ap.user_service.consume_space_oauth_state_details(state, 'login')
|
||||
# Exchange code for tokens
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
launch_workspace_uuid = consumed_state.launch_workspace_uuid
|
||||
workspace_uuids = [launch_workspace_uuid] if launch_workspace_uuid else []
|
||||
workspace_created_ats: dict[str, int] = {}
|
||||
if not workspace_uuids and getattr(getattr(self.ap, 'deployment', None), 'mode', 'oss') != 'cloud':
|
||||
binding = await self.ap.workspace_service.get_execution_binding()
|
||||
workspace_uuids = [binding.workspace_uuid]
|
||||
workspace_created_at = binding.workspace_created_at
|
||||
if workspace_created_at is not None:
|
||||
if workspace_created_at.tzinfo is None:
|
||||
workspace_created_at = workspace_created_at.replace(tzinfo=datetime.UTC)
|
||||
workspace_created_ats[binding.workspace_uuid] = int(workspace_created_at.timestamp())
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(
|
||||
code,
|
||||
workspace_uuids,
|
||||
workspace_created_ats,
|
||||
)
|
||||
access_token = token_data.get('access_token')
|
||||
refresh_token = token_data.get('refresh_token')
|
||||
expires_in = token_data.get('expires_in', 0)
|
||||
@@ -231,7 +247,6 @@ class UserRouterGroup(group.RouterGroup):
|
||||
access_token, refresh_token, expires_in
|
||||
)
|
||||
|
||||
launch_workspace_uuid = consumed_state.launch_workspace_uuid
|
||||
if launch_workspace_uuid:
|
||||
try:
|
||||
access = await self.ap.workspace_collaboration_service.resolve_account_workspace(
|
||||
|
||||
@@ -59,6 +59,10 @@ class SpaceService:
|
||||
result_list = result.all()
|
||||
return result_list[0] if result_list else None
|
||||
|
||||
async def get_valid_access_token(self, user_email: str) -> str | None:
|
||||
"""Return a current Space bearer, refreshing and persisting it when needed."""
|
||||
return await self._ensure_valid_token(user_email)
|
||||
|
||||
async def _ensure_valid_token(self, user_email: str) -> str | None:
|
||||
"""Ensure access token is valid, refresh if expired. Returns valid access_token or None."""
|
||||
user_obj = await self._get_user_by_email(user_email)
|
||||
@@ -117,7 +121,12 @@ class SpaceService:
|
||||
params['state'] = state
|
||||
return f'{authorize_url}?{urlencode(params)}'
|
||||
|
||||
async def exchange_oauth_code(self, code: str) -> typing.Dict:
|
||||
async def exchange_oauth_code(
|
||||
self,
|
||||
code: str,
|
||||
workspace_uuids: list[str] | None = None,
|
||||
workspace_created_ats: dict[str, int] | None = None,
|
||||
) -> typing.Dict:
|
||||
"""Exchange OAuth authorization code for tokens"""
|
||||
from langbot.pkg.utils import constants
|
||||
|
||||
@@ -127,7 +136,14 @@ class SpaceService:
|
||||
session = httpclient.get_session()
|
||||
async with session.post(
|
||||
f'{space_url}/api/v1/accounts/oauth/token',
|
||||
json={'code': code, 'instance_id': constants.instance_id},
|
||||
json={
|
||||
'code': code,
|
||||
'instance_id': constants.instance_id,
|
||||
# Sending an explicit empty list tells new Space servers not to
|
||||
# synthesize a legacy instance-derived Workspace binding.
|
||||
'workspace_uuids': workspace_uuids if workspace_uuids is not None else [],
|
||||
'workspace_created_ats': workspace_created_ats or {},
|
||||
},
|
||||
) as response:
|
||||
if response.status != 200:
|
||||
error = await httpclient.read_text_limited(response)
|
||||
|
||||
@@ -779,8 +779,27 @@ class UserService:
|
||||
local_account = await self.get_user_by_email(user_email)
|
||||
if local_account is None:
|
||||
raise ValueError('User not found')
|
||||
# Exchange code for tokens
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
# Exchange code for tokens and bind both installation and the active
|
||||
# OSS Workspace as independent identities.
|
||||
workspace_service = getattr(self.ap, 'workspace_service', None)
|
||||
if workspace_service is not None:
|
||||
binding = await workspace_service.get_execution_binding()
|
||||
created_at = binding.workspace_created_at
|
||||
created_ts = (
|
||||
int(created_at.replace(tzinfo=datetime.timezone.utc).timestamp())
|
||||
if created_at.tzinfo is None
|
||||
else int(created_at.timestamp())
|
||||
)
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(
|
||||
code,
|
||||
[binding.workspace_uuid],
|
||||
{binding.workspace_uuid: created_ts},
|
||||
)
|
||||
else:
|
||||
# Compatibility for early/bootstrap call sites that have not wired
|
||||
# WorkspaceService yet; old Space servers still derive the legacy
|
||||
# Workspace identity from instance_id when the field is omitted.
|
||||
token_data = await self.ap.space_service.exchange_oauth_code(code)
|
||||
access_token = token_data.get('access_token')
|
||||
refresh_token = token_data.get('refresh_token')
|
||||
expires_in = token_data.get('expires_in', 0)
|
||||
|
||||
Reference in New Issue
Block a user