def get_usertoken_from_session(session_): if config.SITE_COOKIE in session_: cookies = session_[config.SITE_COOKIE] if not cookies: return None name, sig = cookies.split(":") return UserToken(name, sig)
def login_user(name, password): params = { "name": name, "password": password, } r = requests.post("%s/user/login" % config.API_ADDR, data=params) if r.status_code != 200: raise Exception("%s : %s" % (r.status_code, r.text)) j = r.json() ut = UserToken(j["name"], j["sig"]) set_user_cookie(ut, session) return ut
def login_user(name, password): log.debug("call_login_user %s" % name) params = { "name": name, "password": password, "admin_salt": SERVER_SIDE_SALT } r = requests.post("%s/user/login" % config.API_ADDR, data=params) if r.status_code == 400: if 'no such user' in r.json().get('error'): # need to create_user data = { "name": name, "password": name, "email": "*****@*****.**" % name, "cnname": name } headers = { "Apitoken": json.dumps({ "name": "root", "sig": SERVER_SIDE_TOKEN }), "Content-type": "application/json" } r_create = requests.post( "%s/user/create" % (config.API_ADDR, ), headers=headers, data=json.dumps(data), ) if r_create.status_code == 200: log.debug("successfully_create_user %s" % name) r = r_create else: log.error("error_create_user:status_code:%s %s ,%s" % (r_create.status_code, name, r_create.text)) raise Exception("%s : %s" % (r_create.status_code, r_create.text)) if r.status_code != 200: raise Exception("%s : %s" % (r.status_code, r.text)) j = r.json() ut = UserToken(j["name"], j["sig"]) set_user_cookie(ut, session) return ut
def admin_login_user(name, token): params = { "name": name, } h = {"token": token} r = requests.post("%s/admin/login" % config.API_ADDR, data=params, headers=h) log.debug("%s:%s" % (r.status_code, r.text)) if r.status_code != 200: if json.loads(r.text)["error"] == "no such user": return None else: raise Exception("%s : %s" % (r.status_code, r.text)) j = r.json() ut = UserToken(j["name"], j["sig"]) set_user_cookie(ut, session) return ut