def login(user: Dict[str, Any]) -> Tuple[Content, HttpStatusCode]: try: current_user = User.find_by_username(user['username']) assert User.verify_hash(user['password'], current_user.password), \ USER['login']['failure']['credentials'] except NoResultFound: content = {'msg': USER['not_found']} status = 404 except AssertionError as error_message: content = {'msg': str(error_message)} status = 401 except Exception: content = {'msg': GENERAL['internal_error']} status = 500 else: content = { 'msg': USER['login']['success'].format(username=current_user.username), 'access_token': create_access_token(identity=current_user.id, fresh=True), 'refresh_token': create_refresh_token(identity=current_user.id) } status = 200 finally: return content, status
def trigger_action(self, violation_data: Dict[str, Any]) -> None: '''Contains business logic for intruder and admin email notifications. It relies on early returns if any error occures. :param violation_data: data received from ProtectionService ''' # Expect certain keys beforehand assert { 'INTRUDER_USERNAME', 'RESERVATION_OWNER_USERNAME', 'RESERVATION_OWNER_EMAIL', 'RESERVATION_END', 'UUID', 'HOSTNAME' }.issubset(violation_data), 'Invalid keys in violation_data' if not self._test_smtp_configuration(): return try: # Fetch intruder email address and extend violation data intruder_email = User.find_by_username( violation_data['INTRUDER_USERNAME']).email except NoResultFound as e: intruder_email = None log.warning(e) finally: violation_data['INTRUDER_EMAIL'] = intruder_email if not intruder_email: # Intruder has no account or email assigned, try notify admin then timer = self._get_timer(violation_data['INTRUDER_USERNAME']) if MAILBOT.NOTIFY_ADMIN and self._time_to_resend(timer, to_admin=True): self._email_admin(violation_data, timer) return # Intruder has account and email address, try email him and admin then timer = self._get_timer(intruder_email) if MAILBOT.NOTIFY_INTRUDER and self._time_to_resend(timer): self._email_intruder(intruder_email, violation_data, timer) if MAILBOT.NOTIFY_ADMIN and self._time_to_resend(timer, to_admin=True): self._email_admin(violation_data, timer)