def identify(self, request): """ 用户鉴权 :param request: :return: """ auth_header = request.headers.get('Authorization') if auth_header: auth_token_arr = auth_header.split(' ') if not auth_token_arr or auth_token_arr[0] != 'JWT' or len(auth_token_arr) != 2: result = common.false_return('', '验证错误') else: auth_toke = auth_token_arr[1] payload = self.decode_auth_token(auth_toke) if not isinstance(payload, str): user = Users.get(Users, payload['data']['id']) if user is None: result = common.false_return('', '用户不存在') else: if user.login_time == payload['data']['login_time']: result = common.true_return(user.id, '请求成功') else: result = common.false_return('', 'token已更改,请重新登录') else: result = common.false_return('', payload) else: result = common.false_return('', '无认证token') return result
def getUser(userId): user = Users.get(Users, userId) if user is None: return jsonify(Common.falseReturn(Common, None, '找不到数据')) else: return jsonify(Common.trueReturn(Common, Users.output(Users, user)))
def identify(self, request): """ 用户鉴权 :return: list """ auth_header = request.headers.get('Authorization') if (auth_header): auth_tokenArr = auth_header.split(" ") if (not auth_tokenArr or auth_tokenArr[0] != 'JWT' or len(auth_tokenArr) != 2): result = common.falseReturn('', '请传递正确的验证头信息') else: auth_token = auth_tokenArr[1] payload = self.decode_auth_token(auth_token) if not isinstance(payload, str): user = Users.get(Users, payload['data']['id']) if (user is None): result = common.falseReturn('', '找不到该用户信息') else: if (user.login_time == payload['data']['login_time']): result = common.trueReturn(user.id, '请求成功') else: result = common.falseReturn('', 'Token已更改,请重新登录获取') else: result = common.falseReturn('', payload) else: result = common.falseReturn('', '没有提供认证token') return result
def decorated_function(*args, **kwargs): auth_header = request.headers.get('Authorization') if (auth_header): auth_tokenArr = auth_header.split(" ") if (not auth_tokenArr or auth_tokenArr[0] != 'JWT' or len(auth_tokenArr) != 2): result = common.returnFalseMsg( '', 'Please check HTTP Header format.') else: auth_token = auth_tokenArr[1] payload = Auth.decode_auth_token(auth_token) if not isinstance(payload, str): user = Users.get(Users, payload['data']['id']) if (user is None): result = common.returnFalseMsg( '', 'Cannot find the user.') else: if (user.login_time == payload['data']['login_time']): return f(user.id, *args, **kwargs) else: result = common.returnFalseMsg( '', 'Token has changed, please login again.') else: result = common.returnFalseMsg('', payload) else: result = common.returnFalseMsg('', 'Please provide the auth-token.') return jsonify(result), 401
def get_info(uid): """ Get User Info """ user = Users.get(Users, uid) returnUser = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } result = common.returnTrueMsg(returnUser, "Request success.") return jsonify(result)
def get(): """ 获取用户信息 :return: json """ user = Users.get(Users, current_identity.id) ret = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } result = common.trueReturn(ret, '请求成功') return jsonify(result)
def get(): """ 获取用户信息 :return: json """ result = Auth.identify(Auth, request) if (result['status'] and result['data']): user = Users.get(Users, result['data']) returnUser = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } result = common.trueReturn(returnUser, "请求成功") return jsonify(result)
def updateUser(userId): user = Users.get(Users, userId) if user is None: return jsonify(Common.falseReturn(Common, None, '找不到要修改的数据')) else: user_name = request.form.get('user_name') user_password = request.form.get('user_password') user_nickname = request.form.get('user_nickname') user_email = request.form.get('user_email') user.user_name = user_name user.user_password = user_password user.user_nickname = user_nickname user.user_email = user_email result = Users.update(Users) return getUser(user.user_id)
def register(): """ 用户注册 :return: json """ email = request.form.get('email') username = request.form.get('username').strip() password = request.form.get('password').strip() if username is None or username == "" or username == "null": return jsonify(common.falseReturn('', '账号不能为空!')) if password is None or password == "" or password == "null": return jsonify(common.falseReturn('', '密码不能为空!')) if not (username.isalnum()) or is_Chinese(username): return jsonify(common.falseReturn('', '账号只能是字母或数字组成!')) if not (password.isalnum()) or is_Chinese(username): return jsonify(common.falseReturn('', '密码只能是字母或数字组成!')) # 最后一条记录及其ID lastUserRecord = Users.query.order_by('-id').first() if (lastUserRecord is None): newRecordId = 1 else: newRecordId = lastUserRecord.id + 1 user = Users(id=None, email=email, username=username, password=Users.set_password(Users, password)) if (Users.getUsername(Users, user.username)): return jsonify(common.falseReturn('', '用户已存在')) else: Users.add(Users, user) userInfo = Users.get(Users, user.id) if userInfo: returnUser = { 'id': userInfo.id, 'username': userInfo.username, 'email': userInfo.email, 'login_time': userInfo.login_time } return jsonify(common.trueReturn(returnUser, "用户注册成功")) else: return jsonify(common.falseReturn('', '用户注册失败'))
def identity(self, payload): id = payload['identity'] return Users.get(Users, id)