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 4 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
9 changes: 8 additions & 1 deletion garth/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,14 @@ 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"
# There is a way to perform a refresh of an OAuth2 token, but it
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, Tuple, Literal
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,
cyberjunky marked this conversation as resolved.
Show resolved Hide resolved
}

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"
Loading