def decorated_auth(*args, **kwargs): if 'api-token' not in request.headers: return Response( mimetype="application/json", response=json.dumps({'error': 'Authentication token is not available, please login to get one'}), status=400 ) token = request.headers.get('api-token') data = Auth.decode_token(token) if data['error']: return Response( mimetype="application/json", response=json.dumps(data['error']), status=400 ) user_id = data['data']['user_id'] check_user = UserModel.get(user_id) if not check_user: return Response( mimetype="application/json", response=json.dumps({'error': 'user does not exist, invalid token'}), status=400 ) g.user = {'id': user_id} return func(*args, **kwargs)
def post(self): parser = reqparse.RequestParser() parser.add_argument('email', required=True, help='Email required!') parser.add_argument('password', required=True, help='Password required!') data = parser.parse_args() try: email = data['email'] user = UserModel.get_first([UserModel.email == email]) if user is None: return APIResponse.error_404("User not found!") if user.verified: if verify_hash(data['password'], user.password): response = { 'access_token': create_access_token(identity=email), 'refresh_token': create_refresh_token(identity=email) } return APIResponse.success_200(response) else: return APIResponse.error_400("Invalid password!") else: return APIResponse.error_403("User not verified!") except Exception as e: print(e) return APIResponse.error_500()
def put(self, id): parser = reqparse.RequestParser() parser.add_argument('email', required=True, help='Email required!') parser.add_argument('password', required=True, help='Password required!') roles = ("Admin", "User") parser.add_argument('role', choices=roles, required=True, help='Invalid role!') parser.add_argument('first_name') parser.add_argument('last_name') data = parser.parse_args() try: user = UserModel.get_first( [UserModel.id == id, UserModel.active == True]) if user is None: return APIResponse.error_404() user.email = data['email'] user.password = generate_hash(data['password']) user.role = UserRole.role(data['role']) user.first_name = data['first_name'] user.last_name = data['last_name'] user.save() result = UserSchema().dumps(user) response = json.loads(result) return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def post(self): parser = reqparse.RequestParser() parser.add_argument('email', required=True, help='Email required!') parser.add_argument('password', required=True, help='Password required!') roles = ("Admin", "User") parser.add_argument('role', choices=roles, required=True, help='Invalid role!') parser.add_argument('first_name') parser.add_argument('last_name') data = parser.parse_args() try: user = UserModel.get_first([UserModel.email == data['email']]) if user is not None: if user.verified: return APIResponse.error_409("User already exist!") else: user = UserModel(email=data['email']) user.password = generate_hash(data['password']) user.role = UserRole.USER user.first_name = data['first_name'] user.last_name = data['last_name'] user.verified = False token = generate_confirmation_token(data['email']) payload = { 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, 'service_name': app.config['SERVICE_NAME'], 'host_name': app.config['HOST_NAME'] } send_registration_email(payload, token) user.save() response = {'message': 'Email sent!'} return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def get(self): try: users = UserModel.get_all([UserModel.active == True]) result = UserSchema().dumps(users, many=True) response = json.loads(result) return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def populate(self): user1 = UserModel({ 'name': fake.name(), 'username': '******', 'password': '******' }) user1.save() names = ["Window", "Linux", "Other"] for _ in range(50): name = names[fake.random.randint(0, 2)] machines = MachineModel({ 'computer_name': fake.user_name(), 'processors': fake.random.randint(2048, 4096), 'memory': fake.random.randint(2048, 4096), 'last_login_date': fake.date_between_dates(datetime.date(2019, 1, 1), date.today()), 'user_id': user1.id, 'machine_type': name }) machines.save() user2 = UserModel({ 'name': fake.name(), 'username': '******', 'password': '******' }) user2.save() for _ in range(15000): name = names[fake.random.randint(0, 2)] machines = MachineModel({ 'computer_name': fake.user_name(), 'processors': fake.random.randint(2048, 4096), 'memory': fake.random.randint(2048, 4096), 'last_login_date': fake.date_between_dates(datetime.date(2019, 1, 1), date.today()), 'user_id': user2.id, 'machine_type': name }) machines.save()
def delete(self, id): try: user = UserModel.get_first( [UserModel.id == id, UserModel.active == True]) if user is None: return APIResponse.error_404() user.delete() response = {'message': 'Entity deleted'} return APIResponse.success_204(response) except Exception as e: print(e) return APIResponse.error_500()
def get(self, id): try: user = UserModel.get_first( [UserModel.id == id, UserModel.active == True]) if user is None: return APIResponse.error_404() result = UserSchema().dumps(user) response = json.loads(result) return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def post(self): parser = reqparse.RequestParser() parser.add_argument('old_email', required=True, help='Old email required!') parser.add_argument('new_email', required=True, help='New email required!') data = parser.parse_args() try: user = UserModel.get_first([UserModel.email == data['old_email']]) if user is None: return APIResponse.error_404("User not found") new_user = UserModel.get_first( [UserModel.email == data['new_email']]) if new_user is not None: return APIResponse.error_409("Email already exist.") user.email = data['new_email'] token = generate_confirmation_token(data['new_email']) payload = { 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name, 'service_name': app.config['SERVICE_NAME'], 'host_name': app.config['HOST_NAME'] } send_registration_email(payload, token) user.save() response = {'message': 'Email updated'} return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def post(self): parser = reqparse.RequestParser() parser.add_argument('email', required=True, help='Email required!') data = parser.parse_args() try: email = data['email'] user = UserModel.get_first([UserModel.email == email]) if user.verified: return APIResponse.error_404("User not found.") payload = { 'email': user.email, 'first_name': user.first_name, 'last_name': user.last_name } token = generate_confirmation_token(email) send_registration_email(payload, token) response = {"message": "Email resent."} return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()
def post(self): parser = reqparse.RequestParser() parser.add_argument('token', required=True, help='Token required!') data = parser.parse_args() try: email = confirm_token(data['token']) if email is None: return APIResponse.error_404( "The link has been expired or invalid.") user = UserModel.get_first([UserModel.email == email]) if user.verified: return APIResponse.error_400("User already verified.") user.verified = True user.verified_at = datetime.utcnow() user.save() response = {"message": "User has been verified."} return APIResponse.success_200(response) except Exception as e: print(e) return APIResponse.error_500()