def send_signup_email(receiver, token): try: SignUp.check_user(receiver) port = 465 # For SSL sender, password = EMAIL, PASSWORD signup_url = SIGNUP_URL + f'/{token}' body = f"Dear Sir/Madam,\n\nThis message is sent from Urban Buildings' Earthquake Resistance Assessor(UBERA).\nGo to the URL below to set up your account. The link has a validity of 1 hour.\nPlease access the link within that time.\n\nLink: {signup_url}\n\nBest Regards,\nUBERA Team" message= email.message.Message() message['Subject'] = 'UBERA Registration' message['From'] = sender message['To'] = receiver message.set_payload(body) # Create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server: server.login(sender, password) server.sendmail(sender, receiver, message.as_string()) except CustomException as ce: print(str(ce)) raise CustomException(ce) except Exception as e: print(f"SignUp email not sent due to {str(e)}") raise CustomException("500:Mail not sent")
def validate_jwt_token(token): try: result = jwt.decode(token, SECRET_KEY, algorithms=['HS256']) return result except (jwt.exceptions.InvalidTokenError, jwt.exceptions.InvalidSignatureError): raise CustomException("401:Invalid Token") except jwt.exceptions.ExpiredSignatureError: raise CustomException("401:Expired Token") except Exception as e: raise CustomException("500:Internal Server Error")
def validate_user(email): db_obj = db.DatabaseInterface(USR_COL) # Initialize with collection/table name query = {'email': email} result = db_obj.get(query) if result == False: raise CustomException('404:User doesn\'t exist') return result
def validate_credentials(self): try: db_obj = db.DatabaseInterface(USR_COL) # Initialize with collection/table name query = {'email': self.email} result = db_obj.get(query) if result == False: raise CustomException('404:User not found') if result['password'] != self.password: raise CustomException('401:Unauthenticated') return result except CustomException as ce: raise CustomException(ce) except Exception: # log raise CustomException("500:Internal Server Error")
def signup(self, role): # Possible roles: user, moderator, admin try: db_obj = db.DatabaseInterface(USR_COL) # Initialize with collection/table name query = [{'email': self.email, 'password': self.password, 'fullname': self.fullname, 'designation': self.designation, 'organisation':self.organisation,'role': role}] result = db_obj.insert(query) if result == False: raise CustomException('403:User already exists') return result except CustomException as ce: raise CustomException(ce) except Exception as e: print(e) raise CustomException("500:Internal Server Error")
def check_user(email): db_obj = db.DatabaseInterface(USR_COL) # Initialize with collection/table name query = {'email': email} result = db_obj.get(query) if result != False: raise CustomException('403:User already exists') return result
def pass_reset(email, new_pass): try: result = validate_user(email) new_dict = copy.deepcopy(result) new_dict['password'] = new_pass ret = db_obj.update(result, new_dict) if ret == False: raise CustomException('500:Password not updated') return result except CustomException as ce: raise CustomException(ce) except Exception as e: print(e) raise CustomException("500:Internal Server Error")
def calc_fema_score(self): try: fema_obj = fem.FEMA(self.img_marked, self.img_static, self.build_type, self.floors, self.lat_long, self.glass, self.soil_type, self.area) result = fema_obj.calc_fema_score() if result['success'] == True: self.ver_irrg = result['vertical_irregularity'] self.plan_irrg = result['plan_irregularity'] self.pounding = result['pounding'] self.struct_eval = result['structural_evaluation'] return {'ver_irrg': self.ver_irrg, 'plan_irrg': self.plan_irrg, 'pounding': self.pounding} else: raise CustomException('500:FEMA Score not calculated') except CustomException as ce: raise CustomException(ce) except Exception as e: raise Exception(e)
def retrieve_from_db(self): # Moderator allowed to see all # User only allowed to see own area and own building if self.role == 'user': self.check_key_validity() db_obj = db.DatabaseInterface(BUILD_COL) query = {k: v for k, v in zip(self.keys, self.key_values)} result = db_obj.get_by_limit(query, self.max_builds) if result == False: raise CustomException('404: No buildings found') print(result) return result
def send_pass_reset_email(receiver, token): try: port = 465 # For SSL sender, password = EMAIL, PASSWORD pass_reset_url = PASS_RESET_URL + f'/{token}' body = f"Dear Sir/Madam,\n\nThis message is sent from Urban Buildings' Earthquake Resistance Assessor(UBERA).\nGo to the URL below to reset your password. The link has a validity of 1 hour.\nPlease access the link within that time.\n\nLink: {pass_reset_url}\n\nBest Regards,\nUBERA Team" message= email.message.Message() message['Subject'] = 'UBERA Password Reset' message['From'] = sender message['To'] = receiver message.set_payload(body) # Create a secure SSL context context = ssl.create_default_context() with smtplib.SMTP_SSL("smtp.gmail.com", port, context=context) as server: server.login(sender, password) server.sendmail(sender, receiver, message.as_string()) except Exception: # log raise CustomException("500:Mail not sent")
def check_key_validity(self): # Check if user is allowed to access for i in self.keys: if i != 'lat_long': raise CustomException('401: Unauthorized to access resource')