feat(providers): update KiloProviderHandler to use token refresh

parent fad24002
...@@ -210,10 +210,6 @@ class KiloProviderHandler(BaseProviderHandler): ...@@ -210,10 +210,6 @@ class KiloProviderHandler(BaseProviderHandler):
import logging import logging
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
# DEBUG: Check self.oauth2 before using it
logger.info(f"KiloProviderHandler._ensure_authenticated: self.oauth2 type={type(self.oauth2)}, value={self.oauth2}")
logger.info(f"KiloProviderHandler._ensure_authenticated: self._use_api_key_auth={self._use_api_key_auth}")
# If API key authentication is configured, use it directly - NO OAUTH EVER # If API key authentication is configured, use it directly - NO OAUTH EVER
if self._use_api_key_auth: if self._use_api_key_auth:
logger.info("KiloProviderHandler: Using configured API key authentication - skipping OAuth2 flow") logger.info("KiloProviderHandler: Using configured API key authentication - skipping OAuth2 flow")
...@@ -222,23 +218,12 @@ class KiloProviderHandler(BaseProviderHandler): ...@@ -222,23 +218,12 @@ class KiloProviderHandler(BaseProviderHandler):
"token": self.api_key "token": self.api_key
} }
logger.info(f"KiloProviderHandler._ensure_authenticated: About to call self.oauth2.get_valid_token()") # Try to get valid token with automatic refresh
token = self.oauth2.get_valid_token() # NOT async - don't await logger.info(f"KiloProviderHandler._ensure_authenticated: Calling get_valid_token_with_refresh()")
token = await self.oauth2.get_valid_token_with_refresh()
if token: if token:
logger.info("KiloProviderHandler: Using existing OAuth2 token") logger.info("KiloProviderHandler: Using OAuth2 token (valid or refreshed)")
return {
"status": "authenticated",
"token": token
}
# Try to reload credentials one more time - this handles the case where credentials
# were saved by another process/handler instance after this handler was created
self.oauth2._load_credentials()
token = self.oauth2.get_valid_token() # NOT async - don't await
if token:
logger.info("KiloProviderHandler: Found OAuth2 token after reloading credentials")
return { return {
"status": "authenticated", "status": "authenticated",
"token": token "token": token
......
import pytest
import time
from unittest.mock import Mock, AsyncMock, patch
from aisbf.providers.kilo import KiloProviderHandler
@pytest.fixture
def mock_oauth2_valid():
"""Mock KiloOAuth2 with valid token."""
mock = Mock()
mock.get_valid_token_with_refresh = AsyncMock(return_value="valid_token")
return mock
@pytest.fixture
def mock_oauth2_expired():
"""Mock KiloOAuth2 with expired token (returns None)."""
mock = Mock()
mock.get_valid_token_with_refresh = AsyncMock(return_value=None)
mock.initiate_device_flow = AsyncMock(return_value={
"code": "ABC123",
"verification_url": "https://kilo.ai/device",
"expires_in": 600,
"poll_interval": 3.0
})
return mock
@pytest.mark.asyncio
async def test_ensure_authenticated_returns_token_when_valid(mock_oauth2_valid):
"""Test that _ensure_authenticated returns token when valid."""
handler = KiloProviderHandler(provider_id="test_kilo", api_key=None, user_id=1)
handler.oauth2 = mock_oauth2_valid
handler._use_api_key_auth = False
result = await handler._ensure_authenticated()
assert result["status"] == "authenticated"
assert result["token"] == "valid_token"
mock_oauth2_valid.get_valid_token_with_refresh.assert_called_once()
@pytest.mark.asyncio
async def test_ensure_authenticated_initiates_device_flow_when_expired(mock_oauth2_expired):
"""Test that _ensure_authenticated initiates device flow when token expired."""
handler = KiloProviderHandler(provider_id="test_kilo", api_key=None, user_id=1)
handler.oauth2 = mock_oauth2_expired
handler._use_api_key_auth = False
result = await handler._ensure_authenticated()
assert result["status"] == "pending_authorization"
assert result["code"] == "ABC123"
assert result["verification_url"] == "https://kilo.ai/device"
mock_oauth2_expired.get_valid_token_with_refresh.assert_called_once()
mock_oauth2_expired.initiate_device_flow.assert_called_once()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment