def login(self, username: str, password: str) -> dict: errors = {} if username == "": errors['username'] = "******" if password == "": errors['password'] = "******" if errors != {}: return {'success': False, 'errors': errors} elif not self.username_exists(username): errors['username'] = "******" return {'success': False, 'errors': errors} else: # "Warning"? Issue in pylint: https://github.com/MongoEngine/mongoengine/issues/858 users = User.objects( username=username) if users.count() == 1 and APIUtils.decrypt_password(users[0].password) == password: # Generate JWT Token jwt_iat = int(time.time()) jwt_exp = int(jwt_iat + (60*60*14)) # Expires after 24 hrs decrypted_token = { 'email': users[0].emailAddress, 'username': users[0].username, 'publicip': users[0].publicIP, 'iat': jwt_iat, 'exp': jwt_exp } return {'success': True, 'emailVerified': users[0].emailVerified, 'message': "Login successful!", 'token': APIUtils.encrypt_jwt_token(decrypted_token)} else: return {'success': False, 'message': "Invalid username or password!"}
def get_password(self, username: str) -> str: user: User = User.objects(username=username)[0] return APIUtils.decrypt_password(user.password)