Skip to content

Commit

Permalink
mob next [ci-skip] [ci skip] [skip ci]
Browse files Browse the repository at this point in the history
lastFile:skore/src/skore/hub/token.py
  • Loading branch information
rouk1 committed Feb 25, 2025
1 parent cc62828 commit 2c43897
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 29 deletions.
9 changes: 0 additions & 9 deletions skore/src/skore/hub/callback_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def log_message(self, *args):
del args

def do_GET(self):
breakpoint()
parsed = urlparse(self.path)
params = parse_qs(parsed.query)
self.send_response(HTTPStatus.OK)
Expand Down Expand Up @@ -68,7 +67,6 @@ def callback_wrapper(state):
server = TCPServer(("", 0), _get_handler(callback_wrapper))
_, port = server.server_address
server_thread = Thread(target=server.serve_forever)
print(f"LAUCH_CALLBACK_SERVER - {port}")

def watchdog():
shutdown_event.wait(timeout=timeout)
Expand All @@ -81,10 +79,3 @@ def watchdog():
watchdog_thread.start()

return port


if __name__ == "__main__":
from skore import console

port = launch_callback_server(lambda s: console.print(s), 500)
console.print(f"Listening for success callback on http://localhost:{port}")
1 change: 0 additions & 1 deletion skore/src/skore/hub/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ def login(timeout=600, auto_otp=True):
expires_at = None

def callback(state):
print("CALLBACK")
nonlocal access
nonlocal refreshment
nonlocal expires_at
Expand Down
11 changes: 5 additions & 6 deletions skore/src/skore/hub/token.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@
import pathlib
import tempfile
from datetime import datetime
from typing import Optional

from skore.hub import api


class AuthenticationToken:
"""Wrap access, refresh and expires_at."""

EMPTY = object()

def __init__(
self,
access: str = EMPTY,
refreshment: str = EMPTY,
expires_at: str = EMPTY,
access: Optional[str] = None,
refreshment: Optional[str] = None,
expires_at: Optional[str] = None,
):
if all(attr != self.EMPTY for attr in (access, refreshment, expires_at)):
if all(attr is not None for attr in (access, refreshment, expires_at)):
self.access = access
self.refreshment = refreshment
self.expires_at = datetime.fromisoformat(expires_at)
Expand Down
21 changes: 8 additions & 13 deletions skore/tests/unit/hub/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import pytest
from httpx import Response
from skore.hub.api import URI
from skore.hub.callback_server import launch_callback_server
from skore.hub.client import AuthenticatedClient, AuthenticationError, login


Expand Down Expand Up @@ -121,11 +120,9 @@ def test_auto_otp_login(monkeypatch, respx_mock, mock_now, mock_nowstr):
},
)
)
callback_route = respx_mock.post(
urljoin(URI, "identity/oauth/device/callback")
).mock(Response(201))
respx_mock.post(urljoin(URI, "identity/oauth/device/callback")).mock(Response(201))

token_route = respx_mock.get(
respx_mock.get(
urljoin(URI, "identity/oauth/device/token?device_code=device-123")
).mock(
Response(
Expand All @@ -139,6 +136,7 @@ def test_auto_otp_login(monkeypatch, respx_mock, mock_now, mock_nowstr):
},
)
)
respx_mock.route(host="localhost").pass_through()

called_url = None

Expand All @@ -148,32 +146,29 @@ def browser_open(url):

monkeypatch.setattr("webbrowser.open", browser_open)

port = 1992
port = -1

def mock_create_server(callback):
from skore.hub.callback_server import launch_callback_server

nonlocal port
port = launch_callback_server(callback)
return port

monkeypatch.setattr(
"skore.hub.callback_server.launch_callback_server", mock_create_server
)
monkeypatch.setattr("skore.hub.client.launch_callback_server", mock_create_server)

def call_success():
with httpx.Client() as client:
print("CALL_SUCCESS - START")
while not login_route.called:
time.sleep(0.5)
print(f"CALL_SUCCESS - MIDDLE - {port}")
client.get(f"http://localhost:{port}")
print("CALL_SUCCESS - END")

success_thread = Thread(target=call_success)
success_thread.start()

token = login(auto_otp=True)

# assert called_url == "https://idp.com"
assert called_url == "https://idp.com"
assert token.access == "A"
assert token.refreshment == "B"
assert token.expires_at == mock_now

0 comments on commit 2c43897

Please sign in to comment.