Skip to content

Commit dc4f79d

Browse files
authored
edit: assert xtoken equality (#419)
refactor: comments
1 parent 2324df4 commit dc4f79d

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

scratchattach/site/session.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@
1616
from contextlib import contextmanager
1717
from threading import local
1818

19-
# import secrets
20-
# import zipfile
21-
# from typing import Type
2219
Type = type
2320

2421
if TYPE_CHECKING:
@@ -910,6 +907,7 @@ def connect_user_by_id(self, user_id: int) -> user.User:
910907
Returns:
911908
scratchattach.user.User: An object that represents the requested user and allows you to perform actions on the user (like user.follow)
912909
"""
910+
# noinspection PyDeprecation
913911
return self._make_linked_object("username", self.find_username_from_id(user_id), user.User,
914912
exceptions.UserNotFound)
915913

@@ -1131,21 +1129,18 @@ def login_by_id(session_id: str, *, username: Optional[str] = None, password: Op
11311129
scratchattach.session.Session: An object that represents the created login / session
11321130
"""
11331131
# Generate session_string (a scratchattach-specific authentication method)
1132+
# should this be changed to a @property?
11341133
issue_login_warning()
11351134
if password is not None:
11361135
session_data = dict(id=session_id, username=username, password=password)
11371136
session_string = base64.b64encode(json.dumps(session_data).encode()).decode()
11381137
else:
11391138
session_string = None
11401139

1141-
if xtoken is not None:
1142-
# todo: consider removing the xtoken parameter?
1143-
warnings.warn("xtoken is redundant because it is retrieved by decoding the session id.")
1144-
11451140
_session = Session(id=session_id, username=username, session_string=session_string)
1146-
1147-
# xtoken is decoded from sessid, so don't use sess.update
1148-
# but this will cause incompatibilities, warranting a change in the 2nd (semver) version number
1141+
if xtoken is not None:
1142+
# xtoken is retrievable from session id, so the most we can do is assert equality
1143+
assert xtoken == _session.xtoken
11491144

11501145
return _session
11511146

@@ -1176,7 +1171,6 @@ def login(username, password, *, timeout=10) -> Session:
11761171
with requests.no_error_handling():
11771172
request = requests.post(
11781173
"https://scratch.mit.edu/login/", json={"username": username, "password": password}, headers=_headers,
1179-
11801174
timeout=timeout
11811175
)
11821176
try:

0 commit comments

Comments
 (0)