Skip to content

Commit 2cbbed3

Browse files
Save no session by default (kyuridenamida#38)
* Save no session by default
1 parent 2a1a05e commit 2cbbed3

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

atcodertools/client/atcoder.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ def save_cookie(session: requests.Session, cookie_path: Optional[str] = None):
2525
cookie_path = cookie_path or default_cookie_path
2626
os.makedirs(os.path.dirname(cookie_path), exist_ok=True)
2727
session.cookies.save()
28+
logging.info("Saved session into {}".format(os.path.abspath(cookie_path)))
2829
os.chmod(cookie_path, 0o600)
2930

3031

@@ -33,6 +34,8 @@ def load_cookie_to(session: requests.Session, cookie_path: Optional[str] = None)
3334
session.cookies = LWPCookieJar(cookie_path)
3435
if os.path.exists(cookie_path):
3536
session.cookies.load()
37+
logging.info(
38+
"Loaded session from {}".format(os.path.abspath(cookie_path)))
3639
return True
3740
return False
3841

@@ -57,7 +60,7 @@ def check_logging_in(self):
5760
resp = self._request(private_url)
5861
return resp.url == private_url
5962

60-
def login(self, username=None, password=None, use_local_session_cache=True):
63+
def login(self, username=None, password=None, use_local_session_cache=True, save_session_cache=True):
6164
if use_local_session_cache:
6265
load_cookie_to(self._session)
6366
if self.check_logging_in():
@@ -82,7 +85,7 @@ def login(self, username=None, password=None, use_local_session_cache=True):
8285
if resp.text.find("パスワードを忘れた方はこちら") != -1:
8386
raise LoginError
8487

85-
if use_local_session_cache:
88+
if use_local_session_cache and save_session_cache:
8689
save_cookie(self._session)
8790

8891
def download_problem_list(self, contest: Contest) -> List[Problem]:

atcodertools/tools/envgen.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,11 @@ def main(prog, args):
229229
help="Prepare problem directories one by one not using multi processors.",
230230
default=False)
231231

232+
parser.add_argument("--save-no-session-cache",
233+
action="store_true",
234+
help="Save no session cache to avoid security risk",
235+
default=False)
236+
232237
args = parser.parse_args(args)
233238

234239
try:
@@ -241,7 +246,7 @@ def main(prog, args):
241246
client = AtCoderClient()
242247
if not args.without_login:
243248
try:
244-
client.login()
249+
client.login(save_session_cache=not args.save_no_session_cache)
245250
logging.info("Login successful.")
246251
except LoginError:
247252
logging.error(

0 commit comments

Comments
 (0)