def put(self): """Update an email address, i.e. delay its expiration.""" current_user = get_jwt_identity() new_access_token = create_access_token(identity=current_user) user_object: UserModel = UserModel.find_by_address(current_user) user_object.refresh_tokens(access_token=new_access_token) user_object.save_to_db() return { 'message': 'The duration is lengthened.', 'access_token': new_access_token }, HTTPStatus.OK
def post(self): """Create a new email address""" args = Auth.parser.parse_args() address = args['address'] if address is None: address = f'{generate_random_string()}@{host}' while UserModel.find_by_address(address) is not None: address = f'{generate_random_string()}@{host}' else: address = f'{address}@{host}' if UserModel.find_by_address(address) is not None: return { 'message': 'Email address has already been used.' }, HTTPStatus.BAD_REQUEST access_token = create_access_token(identity=address) refresh_token = create_refresh_token(identity=address) new_user = UserModel(address, access_token, refresh_token) new_user.save_to_db() return { 'account': new_user.json(), 'message': 'A temporary mail created' }, HTTPStatus.CREATED
def get(self): """Retrieve all mails from the mail box.""" address = get_jwt_identity() if address is None: return { 'message': 'The mail account does not exists or has expired.' }, HTTPStatus.BAD_REQUEST if not UserModel.find_by_address(address).check_validity(): return { 'message': 'Your token has expired', 'email_address': address }, HTTPStatus.UNAUTHORIZED mails = MailModel.fetch_by_address(address) if len(address) == 0: return { 'message': 'No mails found.', }, HTTPStatus.NOT_FOUND return { 'mails': [mail.json() for mail in mails], 'address': address, 'message': 'OK' }, HTTPStatus.OK