def login(): """login as our user. input: LoginRequest (if POST) output: ok. Request: ?email=xx&password=xx[&remember_me=1] (if GET, else arguments in LoginRequest) """ if request.method == "POST": input_pb = protobuf_json.json2pb(all_pbs.LoginRequest(), request.get_json()) else: input_pb = all_pbs.LoginRequest() input_pb.email = request.args.get("email", "") input_pb.password = request.args.get("password", "") input_pb.remember = request.args.get("remember", "") == "1" table = data_models.GetTable(data_models.RW_USERS) user_pb = data_models.ToProto( table.find_one({"info.email": input_pb.email}), data_models.RW_USERS) if not user_pb or user_pb.info.password != input_pb.password: abort(400) user = user_util.FLUser(user_pb) flogin.login_user(user, remember=input_pb.remember) session.permanent = True return 'ok'
def load_user(userid): """Used by Flask to make a FLUser from our user id, for auto login sessions from cookie etc.""" try: user_id = int(userid) table = data_models.GetTable(data_models.RW_USERS) user_pb = data_models.ToProto(table.find_one({"_id": user_id}), data_models.RW_USERS) if not user_pb: return None return FLUser(user_pb) except: return None
def debug_login(): """Auto login with no password. ?user_id=2 """ if app.config[Constants.KEY_MODE] == Constants.PROD: abort(401) table = data_models.GetTable(data_models.RW_USERS) user_pb = data_models.ToProto( table.find_one({"_id": int(request.args.get('user_id'))}), data_models.RW_USERS) if not user_pb: return "error" user = user_util.FLUser(user_pb) flogin.login_user(user, remember=True) return 'ok'
def forgot_password(): """Sends an email. output: "ok". Request [email protected] """ table = data_models.GetTable(data_models.RW_USERS) user_pb = data_models.ToProto( table.find_one({"info.email": request.args.get("email", "")}), data_models.RW_USERS) if not user_pb: abort(401) utils.SendMail(email_from="no-reply", to=user_pb.info.email, obj="Credentials", body="Your login credentials :\n%s\n%s" % (user_pb.info.email, user_pb.info.password)) return "ok"
def doesEmailExist(email): """Returns true if a user exist with this email.""" table = data_models.GetTable(data_models.RW_USERS) user_pb = data_models.ToProto(table.find_one({"info.email": email}), data_models.RW_USERS) return user_pb is not None