Skip to content
Merged
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
12 changes: 7 additions & 5 deletions pytr/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ def _timezone_name(self):
return "Etc/UTC"

def _login_headers(self):
"""The headers Trade Republic requires on the api/v2 login calls.
"""The headers Trade Republic requires on the web login calls.

Without them the endpoints answer 400 MISSING_REQUIRED_HEADER. The web
frontend sends the same set on all three of them: a base64-encoded JSON
Expand Down Expand Up @@ -396,12 +396,11 @@ def initiate_weblogin(self):
self.log.debug(f"WAF Token: {self._waf_token}")
self._set_waf_cookie(self._waf_token)

extra_headers = self._login_headers() if self._use_v2_login else None
login_path = "/api/v2/auth/web/login" if self._use_v2_login else "/api/v1/auth/web/login"
r = self._websession.post(
f"{self._host}{login_path}",
json={"phoneNumber": self.phone_no, "pin": self.pin},
headers=extra_headers,
headers=self._login_headers(),
)
self.log.debug(f"Web login returned: {r.status_code}")
if r.status_code == 405 and "awselb" in r.headers.get("server", "").lower():
Expand Down Expand Up @@ -446,7 +445,7 @@ def resend_weblogin(self):
return
r = self._websession.post(
f"{self._host}/api/v1/auth/web/login/{self._process_id}/resend",
headers=self._default_headers,
headers=self._login_headers(),
)
r.raise_for_status()

Expand All @@ -455,7 +454,10 @@ def complete_weblogin(self, verify_code=None):
raise ValueError("Initiate web login first.")

if not self._use_v2_login:
r = self._websession.post(f"{self._host}/api/v1/auth/web/login/{self._process_id}/{verify_code}")
r = self._websession.post(
f"{self._host}/api/v1/auth/web/login/{self._process_id}/{verify_code}",
headers=self._login_headers(),
)
r.raise_for_status()
self.save_websession()
return
Expand Down
24 changes: 22 additions & 2 deletions tests/test_api_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)

LOGIN = "https://api.traderepublic.com/api/v2/auth/web/login"
LOGIN_V1 = "https://api.traderepublic.com/api/v1/auth/web/login"
PROCESS = "https://api.traderepublic.com/api/v2/auth/web/login/processes/pid-1"


Expand Down Expand Up @@ -68,8 +69,8 @@ def request(self, method, url, data=None):
return self._record(method, url, None, None)


def _api(replies):
tr = TradeRepublicApi(phone_no="+490000000000", pin="0000", waf_token=None, use_v2_login=True)
def _api(replies, use_v2_login=True):
tr = TradeRepublicApi(phone_no="+490000000000", pin="0000", waf_token=None, use_v2_login=use_v2_login)
tr._websession = _Session(replies)
return tr

Expand Down Expand Up @@ -248,6 +249,25 @@ def test_every_v2_login_call_sends_the_required_headers():
assert call["headers"].get(header), f"{header} missing on {call['method']} {call['url']}"


def test_every_v1_login_call_sends_the_required_headers():
"""v1 login endpoints also reject requests missing X-TR-Device-Info with 400 MISSING_REQUIRED_HEADER."""
tr = _api([{"processId": "pid-1", "countdownInSeconds": 60}, {}, {}], use_v2_login=False)

countdown = tr.initiate_weblogin()
tr.resend_weblogin()
tr.complete_weblogin("1234")

assert countdown == 61
assert [c["url"] for c in tr._websession.calls] == [
LOGIN_V1,
f"{LOGIN_V1}/pid-1/resend",
f"{LOGIN_V1}/pid-1/1234",
]
for call in tr._websession.calls:
for header in REQUIRED_HEADERS:
assert call["headers"].get(header), f"{header} missing on {call['method']} {call['url']}"


def test_device_info_is_base64_encoded_json():
device = jsonlib.loads(base64.b64decode(_api([])._login_headers()["X-TR-Device-Info"]))

Expand Down
Loading