def delete_project(self, projectid, userid): try: # get the project existing_project = ProjectMethods().get_project_by_id( projectid, userid) if (existing_project == None): raise CustomException("Project does not exist", 400) if (existing_project.userid != userid): raise CustomException("Userid does not match project's userid", 403) # for each URL in the project, delete its responses and the target itself urls = Target.query.filter_by(projectid=projectid).all() for url in urls: ResponseMethods().delete_responses(url.urlid) Target.query.filter_by(urlid=url.urlid).delete() # delete the project project = Project.query.filter_by(projectid=projectid).first() db.session.delete(project) db.session.commit() except Exception as e: print(e) raise CustomException("Delete failed", 400)
def individual_project(projectid): data = validate_token(request) if data == None: return "Token is invalid or not present", 403 if (request.method == 'GET'): try: existing_project = ProjectMethods().get_project_by_id(projectid, data['user']) if (existing_project == None): raise CustomException("Project does not exist under userid", 400) return serialize_one(existing_project) except CustomException as e: return e.message, e.status_code elif (request.method == 'PUT'): try: update_parameters = request.json ProjectMethods().update_project(projectid, update_parameters, data['user']) return "Successfully updated", 201 except CustomException as e: return e.message, e.status_code elif (request.method == 'DELETE'): try: ProjectMethods().delete_project(projectid, data['user']) return "Successfully deleted", 201 except CustomException as e: return e.message, e.status_code else: return "Invalid Request method", 405
def create_project(self, userid, projectname): if (projectname == None): raise CustomException("No projectname parameter in body", 400) existing_project = self.get_project_by_name(userid, projectname) if (existing_project != None): raise CustomException("Project name already exists for user", 400) new_project = Project(userid=userid, projectname=projectname, numberurls=0, uptime=0) db.session.add(new_project) db.session.commit()
def login(self, email, password): # Ensure user is registered existing_user = self.get_user_details(email, remove_password=False) if (existing_user == None): raise CustomException("User not registered", 401) # Check that hashing the password entered matches what's in the database bytesval = bytes(password, 'utf-8') hashed_entered_password = hashlib.sha256(bytesval).hexdigest() if (hashed_entered_password != existing_user.password): raise CustomException("Incorrect password", 401) # Otherwise we know that user is valid, so create a jwt token token = jwt.encode({'user' : existing_user.userid, 'exp' : datetime.datetime.utcnow() + datetime.timedelta(days=1)}, config.SECRET_KEY, algorithm="HS256") return {'token': token, 'userid': existing_user.userid, 'email': existing_user.email}
def create_target(self, projectid, userid, target_info): # Project based on projectid should exist existing_project = ProjectMethods().get_project_by_id( projectid, userid) if (existing_project == None): raise CustomException("Project does not exist", 400) # User can only create target for a project they own if (existing_project.userid != userid): raise CustomException("Userid does not match project's userid", 403) try: link = target_info['link'] testtype = target_info['testtype'] requestheaders = target_info.get('requestheaders') requestbody = target_info.get('requestbody') requesttype = target_info.get('requesttype') except: raise CustomException("Invalid json body parameters", 400) try: # Create a new target based on request body json new_target = Target(projectid=projectid, link=link, testtype=testtype, numsuccess=0, numfailure=0, requesttype=requesttype) # Optional parameters if (requestheaders): new_target.requestheaders = requestheaders if (requestbody): new_target.requestbody = requestbody db.session.add(new_target) db.session.commit() params = {"numberurls": 1} ProjectMethods().update_project(projectid, params, userid) return new_target except: raise CustomException("Invalid json body parameters", 400)
def update_project(self, projectid, update_parameters, userid): existing_project = self.get_project_by_id(projectid, userid) if (existing_project == None): raise CustomException("Project does not exist", 400) if (existing_project.userid != userid): raise CustomException("User Id doesn't match", 400) if (update_parameters.get('projectname')): existing_project.projectname = update_parameters['projectname'] if (update_parameters.get('numberurls')): existing_project.numberurls += update_parameters['numberurls'] if (update_parameters.get('uptime')): existing_project.uptime = update_parameters['uptime'] db.session.commit()
def update_target(self, projectid, userid, targetid, target_update_info): # Project based on projectid should exist existing_project = ProjectMethods().get_project_by_id( projectid, userid) if (existing_project == None): raise CustomException("Project does not exist", 400) # User can only update target for a project they own if (existing_project.userid != userid): raise CustomException("Userid does not match project's userid", 403) try: # Get update parameters from request body json link = target_update_info.get('link') testtype = target_update_info.get('testtype') requestheaders = target_update_info.get('requestheaders') requestbody = target_update_info.get('requestbody') requesttype = target_update_info.get('requesttype') except: raise CustomException("Invalid request json parameters", 400) # Target ID should correspond to a valid target existing_target = self.get_target_by_id(targetid) if (existing_target == None): raise CustomException("Target based on target id does not exist", 400) # Update fields of existing target record existing_target.link = link existing_target.testtype = testtype existing_target.requestheaders = requestheaders existing_target.requestbody = requestbody existing_target.requesttype = requesttype db.session.commit()
def delete_target(self, projectid, userid, targetid): # Project based on projectid should exist existing_project = ProjectMethods().get_project_by_id( projectid, userid) if (existing_project == None): raise CustomException("Project does not exist", 400) # User can only update target for a project they own if (existing_project.userid != userid): raise CustomException("Userid does not match project's userid", 403) try: ResponseMethods().delete_responses(targetid) target = Target.query.filter_by(urlid=targetid).first() db.session.delete(target) db.session.commit() params = {"numberurls": -1} ProjectMethods().update_project(projectid, params, userid) except Exception as e: print(e) raise CustomException("Error when deleting", 400)
def signup(self, email, password): # Ensure user is a new user existing_user = self.get_user_details(email, remove_password=False) if (existing_user != None): raise CustomException("User is already registered", 422) # If the user is new, hash their password and create new user record in database bytesval = bytes(password, 'utf-8') hashed_entered_password = hashlib.sha256(bytesval).hexdigest() newuser = User( email=email, password=hashed_entered_password, datejoined = datetime.datetime.now() ) db.session.add(newuser) db.session.commit() # Create new jwt token and return token = jwt.encode({'user' : newuser.userid, 'exp' : datetime.datetime.now() + datetime.timedelta(days=1)}, config.SECRET_KEY, algorithm="HS256") return {'token': token, 'userid': newuser.userid, 'email': newuser.email}