-
-
Notifications
You must be signed in to change notification settings - Fork 91
feat(auth): add DataDome bypass and PIN-based authentication #378
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
Open
jalaliamirreza
wants to merge
2
commits into
ahivert:master
Choose a base branch
from
jalaliamirreza:feat/datadome-pin-auth
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # CLAUDE.md | ||
|
|
||
| This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. | ||
|
|
||
| ## Project Overview | ||
|
|
||
| Unofficial Python client for the [TooGoodToGo](https://toogoodtogo.com) API. Published on PyPI as `tgtg`. Python 3.9+. Licensed under GPL-3.0. | ||
|
|
||
| ## Commands | ||
|
|
||
| ```bash | ||
| # Install dependencies (requires poetry) | ||
| # Option 1: if pipx is available | ||
| pipx install poetry | ||
|
|
||
| # Option 2: via venv (if pipx/poetry not available) | ||
| python3 -m venv .venv | ||
| source .venv/bin/activate | ||
| pip install poetry | ||
|
|
||
| # Then install project deps | ||
| poetry install | ||
|
|
||
| # Run all tests (with coverage) | ||
| make test # or: poetry run pytest | ||
|
|
||
| # Run a single test file | ||
| poetry run pytest tests/test_login.py | ||
|
|
||
| # Run a single test function | ||
| poetry run pytest tests/test_login.py::test_login_with_tokens | ||
|
|
||
| # Run linting (black, isort, flake8) | ||
| make lint # or: poetry run pre-commit run -a | ||
|
|
||
| # Build and publish to PyPI | ||
| make publish | ||
| ``` | ||
|
|
||
| ## Code Style | ||
|
|
||
| - Formatter: **Black** | ||
| - Import sorting: **isort** (profile: black) | ||
| - Linter: **flake8** (max-line-length: 119, max-complexity: 10) | ||
| - All enforced via pre-commit hooks | ||
|
|
||
| ## Architecture | ||
|
|
||
| The entire library is a single class `TgtgClient` in `tgtg/__init__.py` (~500 lines). There are no subpackages or complex abstractions. | ||
|
|
||
| ### Key components | ||
|
|
||
| - **`tgtg/__init__.py`** - `TgtgClient` class plus all API endpoint constants. Every public method calls `self.login()` first, which handles token refresh automatically. The client uses `requests.Session` for HTTP, with Android user-agent spoofing. All API calls go through the central `_post()` method which handles DataDome cookie management and 403 retry logic. | ||
| - **`tgtg/exceptions.py`** - Three exception types: `TgtgLoginError`, `TgtgAPIError`, `TgtgPollingError`. | ||
| - **`tgtg/google_play_scraper.py`** - Scrapes the Google Play Store page to get the current TooGoodToGo APK version for user-agent strings. Falls back to `DEFAULT_APK_VERSION`. | ||
|
|
||
| ### DataDome bot protection | ||
|
|
||
| TooGoodToGo uses DataDome for bot protection. The client handles this by: | ||
|
|
||
| 1. Fetching a `datadome` cookie from `https://api-sdk.datadome.co/sdk/` before API requests, mimicking the Android app's DataDome SDK integration (device fingerprint parameters like model, OS version, screen size). | ||
| 2. The `_post()` method automatically ensures a DataDome cookie is present. On 403, it invalidates the cookie and retries with a fresh one. | ||
| 3. The `_generate_datadome_cid()` helper creates random 120-char client IDs matching DataDome's expected format. | ||
| 4. VPN/datacenter IPs are frequently blocked by DataDome regardless of cookie validity — residential IPs work best. | ||
|
|
||
| ### Authentication flow | ||
|
|
||
| 1. Email-based login sends a request to `auth/v5/authByEmail` | ||
| 2. User receives a PIN code via email | ||
| 3. Client submits PIN via `auth/v5/authByRequestPin` to complete authentication | ||
| 4. Fallback: client can also poll `auth/v5/authByRequestPollingId` (legacy link-click flow) | ||
| 5. On success, stores `access_token`, `refresh_token`, and `cookie` | ||
| 6. Tokens auto-refresh after `access_token_lifetime` (default 4 hours) | ||
|
|
||
| ### API versions in use | ||
|
|
||
| Auth endpoints use `v5`, item endpoints use `v8`, order endpoints use `v8`, token refresh uses `v1`, favorites use `v1`, discover uses `v1`. | ||
|
|
||
| ## Testing | ||
|
|
||
| - Framework: **pytest** with **responses** library for HTTP mocking | ||
| - Coverage: branch coverage enabled, configured in `setup.cfg` (`addopts = --cov=tgtg`) | ||
| - Tests in `tests/` use fixtures from `conftest.py` that mock auth/refresh endpoints | ||
| - `tests/constants.py` contains expected API response property lists and fake token credentials | ||
| - `test_api.py` contains integration tests marked `@pytest.mark.withoutresponses` that hit the real API (requires `TGTG_EMAIL` env var) | ||
| - Time-dependent tests use **freezegun** for mocking `datetime.datetime.now()` |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
isn't it too much :o