From c1823c3ac0ddb28464a19265bb337c74e4e53bbb Mon Sep 17 00:00:00 2001 From: Nirvana Date: Thu, 13 Nov 2025 22:30:18 +0100 Subject: [PATCH] Add Magenta 2.0 (DE) --- .../providers/magenta2/auth.py | 9 ++- .../providers/magenta2/sam3_client.py | 55 ++++++++++++++++++- 2 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/streaming_providers/providers/magenta2/auth.py b/lib/streaming_providers/providers/magenta2/auth.py index b77cb5c..be34af4 100644 --- a/lib/streaming_providers/providers/magenta2/auth.py +++ b/lib/streaming_providers/providers/magenta2/auth.py @@ -404,8 +404,15 @@ class Magenta2Authenticator(BaseOAuth2Authenticator): def update_sam3_client_id(self, client_id: str) -> None: """Public method to update SAM3 client ID""" + old_client_id = self._sam3_client_id self._sam3_client_id = client_id - logger.debug(f"Updated SAM3 client ID: {client_id}") + + # Also update the SAM3 client if it exists + if self._sam3_client: + self._sam3_client.update_sam3_client_id(client_id) + logger.info(f"✓ Updated SAM3 client ID: {old_client_id[:8]}... -> {client_id[:8]}...") + else: + logger.debug(f"Updated SAM3 client ID (no client to update yet): {client_id}") def update_client_model(self, client_model: str) -> None: """Public method to update client model""" diff --git a/lib/streaming_providers/providers/magenta2/sam3_client.py b/lib/streaming_providers/providers/magenta2/sam3_client.py index 50acf80..bb69b0e 100644 --- a/lib/streaming_providers/providers/magenta2/sam3_client.py +++ b/lib/streaming_providers/providers/magenta2/sam3_client.py @@ -670,4 +670,57 @@ class Sam3Client: f"auth={bool(self.authorization_endpoint)}, " f"oauth_token={bool(self.oauth_token_endpoint)}, " f"line_auth={bool(self.line_auth_endpoint)}, " - f"backchannel={bool(self.backchannel_start_url)}") \ No newline at end of file + f"backchannel={bool(self.backchannel_start_url)}") + + def update_sam3_client_id(self, new_client_id: str) -> None: + """Update SAM3 client ID and recreate remote login handler""" + old_client_id = self.sam3_client_id + self.sam3_client_id = new_client_id + + # Invalidate the existing remote login handler so it gets recreated with new client ID + if self._remote_login_handler: + logger.debug( + f"Invalidating remote login handler due to client ID change: {old_client_id} -> {new_client_id}") + self._remote_login_handler = None + + logger.info(f"✓ Updated SAM3 client ID to: {new_client_id}") + + def _get_remote_login_handler(self) -> Optional['RemoteLoginHandler']: + """ + Get or create remote login handler + """ + # Return existing handler if available + if self._remote_login_handler: + return self._remote_login_handler + + # Check if we have required endpoints + has_backchannel = bool(self.backchannel_start_url) + has_token_endpoint = bool(self._get_token_endpoint()) + has_qr_template = bool(self.qr_code_url_template) + + if not all([has_backchannel, has_token_endpoint, has_qr_template]): + # Detailed debug logging + logger.debug("🚫 Remote login not available - missing endpoints:") + logger.debug(f" • backchannel_start_url: {has_backchannel} -> {self.backchannel_start_url}") + logger.debug(f" • token_endpoint: {has_token_endpoint} -> {self._get_token_endpoint()}") + logger.debug(f" • qr_code_url_template: {has_qr_template} -> {self.qr_code_url_template}") + return None + + # LAZY IMPORT to avoid circular dependency + try: + from .remote_login_handler import RemoteLoginHandler + except ImportError as e: + logger.error(f"Failed to import RemoteLoginHandler: {e}") + return None + + # Create handler with CURRENT client ID + self._remote_login_handler = RemoteLoginHandler( + http_manager=self.http_manager, + sam3_client_id=self.sam3_client_id, # Use current value + backchannel_start_url=self.backchannel_start_url, + token_endpoint=self._get_token_endpoint(), + qr_code_url_template=self.qr_code_url_template + ) + + logger.info(f"✓ Remote login handler initialized with client_id: {self.sam3_client_id[:8]}...") + return self._remote_login_handler \ No newline at end of file