Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Commit

Permalink
修復cahce login 的問題#74 (#75)
Browse files Browse the repository at this point in the history
* 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.
  • Loading branch information
takidog authored and abc873693 committed Jul 1, 2019
1 parent cc0f15c commit 4ccac14
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 18 deletions.
41 changes: 24 additions & 17 deletions src/kuas_api/kuas/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
"""

Expand All @@ -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)
Expand All @@ -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()
Expand All @@ -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)

Expand Down Expand Up @@ -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)
print(is_login)
2 changes: 1 addition & 1 deletion src/kuas_api/modules/stateless_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down

0 comments on commit 4ccac14

Please sign in to comment.