def auth_login(): if request.method == "GET": if g.user: return redirect("/") return render_template("auth/login.html", **locals()) if request.method == "POST": ret = { "msg": "", } name = request.form.get("name") password = request.form.get("password") ldap = request.form.get("ldap") or "0" if not name or not password: ret["msg"] = "no name or password" return json.dumps(ret) if ldap == "1": try: ldap_info = view_utils.ldap_login_user(name, password) password = id_generator() user_info = { "name": name, "password": password, "cnname": ldap_info['cnname'], "email": ldap_info['email'], "phone": ldap_info['phone'], } Apitoken = view_utils.get_Apitoken(config.API_USER, config.API_PASS) ut = view_utils.admin_login_user(name, Apitoken) if not ut: view_utils.create_user(user_info) ut = view_utils.admin_login_user(name, Apitoken) #if user not exist, create user , signup must be enabled ret["data"] = { "name": ut.name, "sig": ut.sig, } return json.dumps(ret) except Exception as e: ret["msg"] = str(e) return json.dumps(ret) try: ut = view_utils.login_user(name, password) if not ut: ret["msg"] = "no such user" return json.dumps(ret) ret["data"] = { "name": ut.name, "sig": ut.sig, } return json.dumps(ret) except Exception as e: ret["msg"] = str(e) return json.dumps(ret)
def auth_login(): if request.method == "GET": if g.user: return redirect("/") return render_template("auth/login.html", **locals()) if request.method == "POST": ret = { "msg": "", } name = request.form.get("name") password = request.form.get("password") #ldap = request.form.get("ldap") or "0" #disable ldap ldap = 0 if not name or not password: ret["msg"] = "no name or password" return json.dumps(ret) if ldap == "1": try: ldap_info = view_utils.ldap_login_user(name, password) h = {"Content-type": "application/json"} d = { "name": name, "password": password, "cnname": ldap_info['cnname'], "email": ldap_info['email'], "phone": ldap_info['phone'], } r = requests.post("%s/user/create" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" % (r.status_code, r.text)) #TODO: update password in db if ldap password changed except Exception as e: ret["msg"] = str(e) return json.dumps(ret) try: ut = view_utils.login_user(name, password) if not ut: ret["msg"] = "no such user" return json.dumps(ret) ret["data"] = { "name": ut.name, "sig": ut.sig, } return json.dumps(ret) except Exception as e: ret["msg"] = str(e) return json.dumps(ret)
def auth_login(): if request.method == "GET": if g.user: return redirect("/") return render_template("auth/login.html", **locals()) if request.method == "POST": ret = { "msg": "", } name = request.form.get("name") password = request.form.get("password") ldap = request.form.get("ldap") or "0" if not name or not password: ret["msg"] = "no name or password" return json.dumps(ret) if ldap == "1": try: ldap_info = view_utils.ldap_login_user(name, password) h = {"Content-type":"application/json"} d = { "name": name, "password": password, "cnname": ldap_info['cnname'], "email": ldap_info['email'], "phone": ldap_info['phone'], } r = requests.post("%s/user/create" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" %(r.status_code, r.text)) #TODO: update password in db if ldap password changed except Exception as e: ret["msg"] = str(e) return json.dumps(ret) try: ut = view_utils.login_user(name, password) if not ut: ret["msg"] = "no such user" return json.dumps(ret) ret["data"] = { "name": ut.name, "sig": ut.sig, } return json.dumps(ret) except Exception as e: ret["msg"] = str(e) return json.dumps(ret)
def auth_login(): if request.method == "GET": if g.user: return redirect("/") return render_template("auth/login.html", **locals()) if request.method == "POST": ret = { "msg": "", } name = request.form.get("name") user_id = -1 password = request.form.get("password") random_pass = view_utils.gen_random_pass(12) ldap = request.form.get("ldap") or "0" if not name or not password: ret["msg"] = "no name or password" return json.dumps(ret) if ldap == "1": try: ldap_info = view_utils.ldap_login_user(name, password) h = {"Content-type": "application/json"} d = { "name": name, "password": random_pass, "cnname": ldap_info['cnname'], "email": ldap_info['email'], "phone": ldap_info['phone'], } root_sig = view_utils.get_root_sig("root", config.ROOT_PASSWD) if not root_sig: ret["msg"] = "ldap user login failed" return json.dumps(ret) Apitoken = {"name": "root", "sig": root_sig} h.update({"apitoken": json.dumps(Apitoken)}) r = requests.get("%s/user/name/%s" % (config.API_ADDR, name), headers=h) if r.status_code == 200: j = r.json() user_id = j["id"] d = { "user_id": user_id, "password": random_pass, } req = requests.put("%s/admin/change_user_passwd" % (config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" % (req.status_code, req.text)) if req.status_code != 200: raise Exception("%s %s" % (req.status_code, req.text)) else: req = requests.post("%s/user/create" %(config.API_ADDR,), \ data=json.dumps(d), headers=h) log.debug("%s:%s" % (req.status_code, reqr.text)) password = random_pass #TODO: update password in db if ldap password changed except Exception as e: ret["msg"] = str(e) return json.dumps(ret) try: ut = view_utils.login_user(name, password) if not ut: ret["msg"] = "no such user" return json.dumps(ret) ret["data"] = { "name": ut.name, "sig": ut.sig, } return json.dumps(ret) except Exception as e: ret["msg"] = str(e) return json.dumps(ret)