From 4ccac140c20a67b41156442d9dc16ab9140dd59d Mon Sep 17 00:00:00 2001 From: YI-XIANG LIN <37475932+takidog@users.noreply.github.com> Date: Mon, 1 Jul 2019 11:37:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=BE=A9cahce=20login=20=E7=9A=84?= =?UTF-8?q?=E5=95=8F=E9=A1=8C#74=20(#75)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix cache login do not use cache cookie * Remove extra code ,make clarify * Add some comment Why use"red_auth" not "red" ? Because "red" return data is bytes , I think get data from redis should be able to use without any decode or encode action. --- src/kuas_api/kuas/cache.py | 41 +++++++++++++++----------- src/kuas_api/modules/stateless_auth.py | 2 +- 2 files changed, 25 insertions(+), 18 deletions(-) diff --git a/src/kuas_api/kuas/cache.py b/src/kuas_api/kuas/cache.py index 80b67af..4cef466 100644 --- a/src/kuas_api/kuas/cache.py +++ b/src/kuas_api/kuas/cache.py @@ -7,7 +7,6 @@ import requests from werkzeug.contrib.cache import SimpleCache - import kuas_api.kuas.ap as ap import kuas_api.kuas.leave as leave import kuas_api.kuas.parse as parse @@ -31,12 +30,16 @@ AP_GUEST_PASSWORD = "123" s_cache = SimpleCache() -red = redis.StrictRedis.from_url(url= os.environ['REDIS_URL'],db=2) +red = redis.StrictRedis.from_url(url=os.environ['REDIS_URL'], db=2) SECRET_KEY = red.get("SECRET_KEY") if red.exists( "SECRET_KEY") else str(os.urandom(32)) +# Only use in cache.login , get encoded data from redis. +# get data from redis should be able use without any decode or encode action. +red_auth = redis.StrictRedis.from_url( + url=os.environ['REDIS_URL'], db=2, charset="utf-8", decode_responses=True) -def dump_session_cookies(session,is_login): +def dump_session_cookies(session, is_login): """Dumps cookies to list """ @@ -47,13 +50,17 @@ def dump_session_cookies(session,is_login): 'domain': c.domain, 'value': c.value}) - return {'is_login': is_login, 'cookies':cookies} + return {'is_login': is_login, 'cookies': cookies} def login(username, password): session = requests.Session() is_login = {} + if red_auth.exists(username): + user_redis_cookies = red_auth.get(username) + return json.loads(user_redis_cookies) + # AP Login try: is_login["ap"] = ap.login(session, username, password) @@ -72,15 +79,15 @@ def login(username, password): is_login["leave"] = leave.login(session, username, password) except: is_login["leave"] = False - if is_login["ap"]: - return dump_session_cookies(session,is_login) + if is_login["ap"]: + return dump_session_cookies(session, is_login) else: - return False + return False def ap_query(session, qid=None, args=None, username=None, expire=AP_QUERY_EXPIRE): - ap_query_key_tag = str(username) + str(args) + str(SECRET_KEY) + ap_query_key_tag = str(username) + str(args) + str(SECRET_KEY) ap_query_key = qid + \ hashlib.sha512( bytes(ap_query_key_tag, "utf-8")).hexdigest() @@ -95,6 +102,7 @@ def ap_query(session, qid=None, args=None, return ap_query_content + def leave_query(session, year="102", semester="2"): return leave.getList(session, year, semester) @@ -194,31 +202,30 @@ def get_semester_list(): """ s = requests.Session() - ap.login(s,AP_GUEST_ACCOUNT, AP_GUEST_PASSWORD) + ap.login(s, AP_GUEST_ACCOUNT, AP_GUEST_PASSWORD) content = ap_query(s, "ag304_01") - if len(content)<3000: + if len(content) < 3000: return False root = etree.HTML(content) #options = root.xpath("id('yms_yms')/option") try: options = map(lambda x: {"value": x.values()[0].replace("#", ","), - "selected": 1 if "selected" in x.values() else 0, - "text": x.text}, - root.xpath("id('yms_yms')/option") - ) + "selected": 1 if "selected" in x.values() else 0, + "text": x.text}, + root.xpath("id('yms_yms')/option") + ) except: return False - + options = list(options) return options - if __name__ == "__main__": s = requests.Session() is_login = login(s, "guest", "123") - print(is_login) \ No newline at end of file + print(is_login) diff --git a/src/kuas_api/modules/stateless_auth.py b/src/kuas_api/modules/stateless_auth.py index de5e42b..e449668 100644 --- a/src/kuas_api/modules/stateless_auth.py +++ b/src/kuas_api/modules/stateless_auth.py @@ -70,7 +70,7 @@ def generate_auth_token(username, cookies, expiration=600): """ s = Serializer(DIRTY_SECRET_KEY, expires_in=expiration) - red.set(username, json.dumps(cookies)) + red.set(username, json.dumps(cookies), ex=600) return s.dumps({"sid": username})