def register(): """ 用户注册 :return: json """ email = request.form.get('email') username = request.form.get('username') password = request.form.get('password') print(username) print(password) print(email) user = Users(email=email, username=username, password=Users.set_password(Users, password)) result = Users.add(Users, user) print(result) if user.id: return_user = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } return jsonify(common.resp(return_user, "用户注册成功")) else: return jsonify(common.resp('', '用户注册失败', 400))
def user_login(): if not request.json: return abort(500) username = request.json.get('user') password = request.json.get('pass') response = Users.login(username, password) return jsonify(response)
def identify(self, request): """ 用户鉴权 :return: list """ 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_token = auth_token_arr[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.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 user_register(): if not request.json: return abort(500) username = request.json.get('user') password = request.json.get('pass') repassword = request.json.get('repass') email = request.json.get('email') response = Users.register(username, password, repassword, email) return jsonify(response)
def login() -> str: if current_user.is_authenticated: return redirect(url_for('index')) errors = validate_create_users(request) if errors is not None: raise InvalidUsage(errors) auth_telegram = Telegram(request.json, bot_token) if not auth_telegram.check(): raise InvalidUsage("auth_failed") user_model = Users(db) user = user_model.find(request.json.get('id')) if user is None: user = user_model.create(request.json) login_user(user) session.pop('_flashes', None) return jsonify({'code': 'ok'})
def get(): """ 获取用户信息 :return: json """ result = Auth.identify(Auth, request) if result['status'] and result['data']: user = Users.get(Users, result['data']) return_user = { 'id': user.id, 'username': user.username, 'email': user.email, 'login_time': user.login_time } result = common.resp(return_user, "请求成功") return jsonify(result)
def authenticate(self, user_name, password): """ 用户登录,登录成功返回token,写将登录时间写入数据库;登录失败返回失败原因 :param user_name: :param password: :return: json """ user_info = Users.query.filter_by(username=user_name).first() if user_info is None: return jsonify(common.false_return('', '找不到用户')) else: if Users.check_password(Users, user_info.password, password): login_time = int(time.time()) user_info.login_time = login_time # Users.update(Users) token = self.encode_auth_token(user_info.id, login_time) return jsonify(common.true_return(token.decode(), '登录成功')) else: return jsonify(common.false_return('', '密码不正确'))
app.config['SESSION_COOKIE_HTTPONLY'] = str(environ.get("SESSION_COOKIE_HTTPONLY", None)) app.config['SECRET_KEY'] = str(environ.get("SECRET_KEY", None)) Session(app) Bootstrap(app) port = int(environ.get("PORT", 5000)) debug = bool(environ.get("DEBUG", False)) telegram_bot_token = str(environ.get("TELEGRAM_BOT_TOKEN", "")) dynamoDb = boto3.resource('dynamodb') app.register_blueprint(keywords_blueprint(dynamoDb), url_prefix='/keyword') app.register_blueprint(auth_blueprint(dynamoDb, telegram_bot_token), url_prefix='/auth') login_manager = LoginManager() login_manager.login_view = 'auth.login' login_manager.user_loader(Users(dynamoDb).find) login_manager.init_app(app) @user_logged_out.connect_via(app) def user_logged_out(app, user): session.clear() @app.route('/k8s') def k8s(): return '' @app.route('/') @login_required