def exec_createuser(username, password, **kwargs): """创建账号""" ak = rsp("accounts") if check_username(username): if not password or len(password) < 6: echo("密码最少6位", "yellow") else: rc = create_redis_engine() if rc.sismember(ak, username): echo("用户名已存在", "red") else: is_admin = kwargs.pop("is_admin", 0) uk = rsp("account", username) pipe = rc.pipeline() pipe.sadd(ak, username) if kwargs: pipe.hmset(uk, kwargs) pipe.hmset( uk, dict(username=username, password=generate_password_hash(password), is_admin=1 if is_true(is_admin) else 0, ctime=get_current_timestamp())) try: pipe.execute() except RedisError as e: echo(e.message, "red") else: echo("注册成功!", "green") finally: rc.connection_pool.disconnect() else: echo("用户名不合法或不允许注册", "yellow")
def login(): if request.method == "GET": return abort(404) res = dict(code=1) usr = request.form.get("username") pwd = request.form.get("password") #: 定义是否设置cookie状态 set_state = is_true(request.form.get("set_state")) is_secure = False if request.url_root.split("://")[0] == "http" else True max_age = 7200 if is_true(request.form.get("remember")): #: Remember me 7d max_age = 604800 #: 登录接口钩子 try: if g.cfg.site_auth: so = current_app.extensions["hookmanager"].proxy(g.cfg.site_auth) if so and hasattr(so, "login_api"): result = so.login_api(usr, pwd, set_state, max_age, is_secure) if result and isinstance(result, Response): return result except (ValueError, TypeError, Exception) as e: logger.warning(e, exc_info=True) if usr and pwd and check_username(usr) and len(pwd) >= 6: ak = rsp("accounts") if g.rc.sismember(ak, usr): userinfo = g.rc.hgetall(rsp("account", usr)) password = userinfo.get("password") if password and check_password_hash(password, pwd): expire = get_current_timestamp() + max_age sid = "%s.%s.%s" % (usr, expire, sha256("%s:%s:%s:%s" % (usr, password, expire, current_app.config["SECRET_KEY"]))) sid = b64encode(sid.encode("utf-8")).decode("utf-8") res.update( code=0, sid=sid, expire=expire, # is_admin=is_true(userinfo.get("is_admin")) ) if set_state: res = make_response(jsonify(res)) res.set_cookie(key="dSid", value=sid, max_age=max_age, httponly=True, secure=is_secure) else: res.update(msg="Password verification failed") else: res.update(msg="No valid username found") else: res.update(msg="The username or password parameter error") return res
def register(): if is_true(g.cfg.register) is False: return abort(404) res = dict(code=1, msg=None) #: Required fields username = request.form.get("username") password = request.form.get("password") if username and password: username = username.lower() if check_username(username): if len(password) < 6: res.update(msg="Password must be at least 6 characters") else: ak = rsp("accounts") if g.rc.sismember(ak, username): res.update(msg="The username already exists") else: #: 用户状态 -1待审核 0禁用 1启用 #: 后台开启审核时默认是-1,否则是1 #: 禁用时无认证权限(无法登陆,无API权限) # ;待审核仅无法上传,允许登录和API调用 status = -1 if is_true(g.cfg.review) else 1 #: 参数校验通过,执行注册 options = dict( username=username, password=generate_password_hash(password), is_admin=0, avatar=request.form.get("avatar") or "", nickname=request.form.get("nickname") or "", ctime=get_current_timestamp(), status=status, ) uk = rsp("account", username) pipe = g.rc.pipeline() pipe.sadd(ak, username) pipe.hmset(uk, options) try: pipe.execute() except RedisError: res.update(msg="Program data storage service error") else: res.update(code=0) else: res.update( msg="The username is invalid or registration is not allowed") else: res.update(msg="Parameter error") return res
def login(): if request.method == "GET": return abort(404) res = dict(code=1) usr = request.form.get("username") pwd = request.form.get("password") #: 定义是否设置cookie状态 set_state = is_true(request.form.get("set_state")) is_secure = False if request.url_root.split("://")[0] == "http" else True max_age = 7200 if is_true(request.form.get("remember")): #: Remember me 7d max_age = 604800 if usr and pwd and check_username(usr) and len(pwd) >= 6: ak = rsp("accounts") if rc.sismember(ak, usr): userinfo = rc.hgetall(rsp("account", usr)) password = userinfo.get("password") if password and check_password_hash(password, pwd): expire = get_current_timestamp() + max_age sid = "%s.%s.%s" % (usr, expire, sha256("%s:%s:%s:%s" % (usr, password, expire, current_app.config["SECRET_KEY"]))) sid = b64encode(sid.encode("utf-8")).decode("utf-8") res.update( code=0, sid=sid, expire=expire, # is_admin=is_true(userinfo.get("is_admin")) ) if set_state: res = make_response(jsonify(res)) res.set_cookie(key="dSid", value=sid, max_age=max_age, httponly=True, secure=is_secure) else: res.update(msg="Password verification failed") else: res.update(msg="No valid username found") else: res.update(msg="Parameter error") return res
def register(): if request.method == "GET" or is_true(g.cfg.register) is False: return abort(404) res = dict(code=1) #: Required fields username = request.form.get("username") password = request.form.get("password") if username and password: if check_username(username): if len(password) < 6: res.update(msg="Password must be at least 6 characters") else: ak = rsp("accounts") if g.rc.sismember(ak, username): res.update(msg="The username already exists") else: #: 参数校验通过,执行注册 options = dict( username=username, password=generate_password_hash(password), is_admin=0, avatar=request.form.get("avatar") or "", nickname=request.form.get("nickname") or "", ctime=get_current_timestamp(), ) uk = rsp("account", username) pipe = g.rc.pipeline() pipe.sadd(ak, username) pipe.hmset(uk, options) try: pipe.execute() except RedisError: res.update(msg="Program data storage service error") else: res.update(code=0) else: res.update( msg="The username is invalid or registration is not allowed" ) return res