def post(self): json_data = request.get_json() username = json_data.get('username') email = json_data.get('email') non_hash_password = json_data.get('password') if User.get_by_username(username): return { 'message': 'Username already exists' }, HTTPStatus.BAD_REQUEST if User.get_by_email(email): return {'message': 'Email already used'}, HTTPStatus.BAD_REQUEST password = hash_password(non_hash_password) user = User(username=username, email=email, password=password) user.save() data = { 'id': user.id, 'username': user.username, 'email': user.email, 'is_admin': user.is_admin, } return data, HTTPStatus.CREATED
def user_login(cls, username, password): hashed = hash_password(username, password) if session.query(User).filter( User.username == username, User.password == hashed).scalar() is not None: return session.query(User).filter(User.username == username, User.password == hashed).one() else: return None
def register_user(): req = request.get_json(force=True) User.if_user_already_created(req['login']) sess = db_session.create_session() user = User(login=req['login'], password=hash_password(req['password'])) sess.add(user) sess.commit() user_id = sess.query(User.id).filter(User.login == req['login']).first() access_token = create_access_token(identity=user_id) res = jsonify({'success': 'OK'}) set_access_cookies(res, access_token) return res
async def create_user(username, password, full_name): """ create user if exist return a dictionary with user is None :param username: user username :param password: user password :param full_name: user full name :return: a dictionary which contains user dictionary and a msg """ try: if await User.query.where(User.username == username).gino.first(): return None, "User exists" user = User(username=username, password=utils.hash_password(password), full_name=full_name) await user.create() return user.as_json(), None except Exception as ex: global_logger.write_log('error', f"error: {ex}") return None, ex
async def authenticate(request, *args, **kwargs): """ to authenticate JWT for generating token :param request: :param args: has post data :param kwargs: :return: user json data """ username = request.json.get('username', None) password = request.json.get('password', None) if not username or not password: raise exceptions.AuthenticationFailed("Missing username or password.") user = await User.query.where(User.username == username).gino.first() if user is None: raise exceptions.AuthenticationFailed("User not found.") if hash_password(password) != user.password: raise exceptions.AuthenticationFailed("Password is incorrect.") return user.as_json()
def insert_into_user_table(self, *args): self.conn.execute_query(INSERT_NEW_USER, args[0], hash_password(args[0], args[1]))
def register_user(self, *args): self.conn.execute_query(INSERT_NEW_USER, args[0], hash_password(args[0], args[1]))
def user_login(self, *args): user = self.conn.get_one(USER_LOGIN, args[0], hash_password(args[0], args[1])) if user == None: return False return User(user[0], user[1])
def delete_from_user_table(self, *args): self.conn.execute_query(DELETE_USER, args[0], hash_password(args[0], args[1]))
def insert_new_user(cls, username, password): hashed = hash_password(username, password) user = User(username=username, password=hashed) session.add(user) session.commit()