def get_moderator_by_request(request): "通过request获取moderator" port = request.META["HTTP_HOST"].split(":")[1] cv = request.COOKIES.get("rkmoderator" + port) if cv is None: return None else: cv = urllib.unquote(cv).decode("ascii") mid_login_stamp_token = cv.split('|') if len(mid_login_stamp_token) != 3: moderator = None else: mid, login_stamp, token = mid_login_stamp_token moderator = get_moderator(mid) if moderator is None: return None raw_last_login_stamp = int( time.mktime(moderator.last_login.timetuple())) new_token = build_rkauth_signature({ "mid": mid, "last_login": raw_last_login_stamp, "secret_key": admin_configuration.ADMIN_SECRET_KEY }) if new_token == token: return moderator else: return None
def get_moderator_by_request(request): "通过request获取moderator" port = request.META["HTTP_HOST"].split(":")[1] cv = request.COOKIES.get("rkmoderator" + port) if cv is None: return None else: cv = urllib.unquote(cv).decode("ascii") mid_login_stamp_token = cv.split('|') if len(mid_login_stamp_token) != 3: moderator = None else: mid,login_stamp,token = mid_login_stamp_token moderator = get_moderator(mid) if moderator is None: return None raw_last_login_stamp = int(time.mktime(moderator.last_login.timetuple())) new_token = build_rkauth_signature({ "mid":mid, "last_login": raw_last_login_stamp, "secret_key":admin_configuration.ADMIN_SECRET_KEY }) if new_token == token: return moderator else: return None
def login(request, response, moderator): """ 管理员登陆 """ login_time = datetime.datetime.now() login_ip = request.META["REMOTE_ADDR"] port = request.META["HTTP_HOST"].split(":")[1] moderator.set_last_login(login_time, login_ip) mid = moderator.mid last_login_stamp = int(time.mktime(moderator.last_login.timetuple())) token = build_rkauth_signature({ "mid": mid, "last_login": last_login_stamp, "secret_key": admin_configuration.ADMIN_SECRET_KEY }) cv = "%s|%s|%s" % (mid, last_login_stamp, token) cv = urllib.quote(cv.encode("ascii")) response.set_cookie("rkmoderator" + port, cv, expires=datetime.datetime.now() + datetime.timedelta(seconds=int(60 * 60 * 24))) return response
def login(request,response,moderator): """ 管理员登陆 """ login_time = datetime.datetime.now() login_ip = request.META["REMOTE_ADDR"] port = request.META["HTTP_HOST"].split(":")[1] moderator.set_last_login(login_time,login_ip) mid = moderator.mid last_login_stamp = int(time.mktime(moderator.last_login.timetuple())) token = build_rkauth_signature({ "mid":mid, "last_login": last_login_stamp, "secret_key":admin_configuration.ADMIN_SECRET_KEY }) cv = "%s|%s|%s" % (mid, last_login_stamp,token) cv = urllib.quote(cv.encode("ascii")) response.set_cookie("rkmoderator" + port,cv,expires=datetime.datetime.now() + datetime.timedelta(seconds = int(60*60*24))) return response