Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes for return_on_mfa code #92

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,10 @@ garth.login(email, password, prompt_mfa=lambda: input("Enter MFA code: "))
For advanced use cases (like async handling), MFA can be handled separately:

```python
result = garth.login(email, password, return_on_mfa=True)
if isinstance(result, dict): # MFA is required
result1, result2 = garth.login(email, password, return_on_mfa=True)
if result1 == "needs_mfa": # MFA is required
mfa_code = "123456" # Get this from your custom MFA flow
oauth1, oauth2 = garth.resume_login(result['client_state'], mfa_code)
oauth1, oauth2 = garth.resume_login(result2, mfa_code)
```

### Configure
Expand Down
19 changes: 13 additions & 6 deletions garth/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ def user_profile(self):
self._user_profile = self.connectapi(
"/userprofile-service/socialProfile"
)
assert isinstance(
self._user_profile, dict
), "No profile from connectapi"
assert isinstance(self._user_profile, dict), (
"No profile from connectapi"
)
return self._user_profile

@property
Expand All @@ -126,9 +126,9 @@ def request(
if referrer is True and self.last_resp:
headers["referer"] = self.last_resp.url
if api:
assert (
self.oauth1_token
), "OAuth1 token is required for API requests"
assert self.oauth1_token, (
"OAuth1 token is required for API requests"
)
if not self.oauth2_token or self.oauth2_token.expired:
self.refresh_oauth2()
headers["Authorization"] = str(self.oauth2_token)
Expand Down Expand Up @@ -164,6 +164,13 @@ def login(self, *args, **kwargs):
self.oauth1_token, self.oauth2_token = sso.login(
*args, **kwargs, client=self
)
return self.oauth1_token, self.oauth2_token

def resume_login(self, *args, **kwargs):
self.oauth1_token, self.oauth2_token = sso.resume_login(
*args, **kwargs
)
return self.oauth1_token, self.oauth2_token

def refresh_oauth2(self):
assert self.oauth1_token, "OAuth1 token is required for OAuth2 refresh"
Expand Down
15 changes: 6 additions & 9 deletions garth/sso.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import asyncio
import re
import time
from typing import Callable, Dict, Tuple
from typing import Callable, Dict, Literal, Tuple
from urllib.parse import parse_qs

import requests
Expand Down Expand Up @@ -74,7 +74,7 @@ def login(
client: "http.Client | None" = None,
prompt_mfa: Callable | None = lambda: input("MFA code: "),
return_on_mfa: bool = False,
) -> Tuple[OAuth1Token, OAuth2Token] | dict:
) -> Tuple[OAuth1Token, OAuth2Token] | Tuple[Literal["needs_mfa"], dict]:
"""Login to Garmin Connect.

Args:
Expand Down Expand Up @@ -141,13 +141,10 @@ def login(
# Handle MFA
if "MFA" in title:
if return_on_mfa or prompt_mfa is None:
return {
"needs_mfa": True,
"client_state": {
"csrf_token": csrf_token,
"signin_params": SIGNIN_PARAMS,
"client": client,
},
return "needs_mfa", {
"csrf_token": csrf_token,
"signin_params": SIGNIN_PARAMS,
"client": client,
}

handle_mfa(client, SIGNIN_PARAMS, prompt_mfa)
Expand Down
2 changes: 1 addition & 1 deletion garth/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.5.2"
__version__ = "0.5.3"
21 changes: 18 additions & 3 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,9 @@ def test_configure_pool_connections(client: Client):
assert client.pool_connections == 99
adapter = client.sess.adapters["https://"]
assert isinstance(adapter, HTTPAdapter)
assert (
getattr(adapter, "_pool_connections", None) == 99
), "Pool connections not properly configured"
assert getattr(adapter, "_pool_connections", None) == 99, (
"Pool connections not properly configured"
)


@pytest.mark.vcr
Expand Down Expand Up @@ -247,3 +247,18 @@ def test_put(authed_client: Client):
json=data,
)
assert authed_client.connectapi(path)


@pytest.mark.vcr(record_mode="once")
def test_resume_login(client: Client):
client.oauth1_token, client.oauth2_token = client.login(
"[email protected]", "correct_password"
)
assert client.oauth1_token
assert client.oauth2_token
client.oauth1_token, client.oauth2_token = None, None
assert client.oauth1_token is None
assert client.oauth2_token is None
client.resume_login("[email protected]", "correct_password")
assert client.oauth1_token
assert client.oauth2_token
11 changes: 5 additions & 6 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading