Exemplo n.º 1
0
    def get_connection(self):
        if not self.password:
            self.password = get_password('account', self.host, self.username)

        if not self.password:
            raise ConnectionSettingsError(
                self.account,
                'Missing SMTP password! Please re-enter your password in settings.',
            )

        server_string = (
            f'{self.username}@{self.host}:{self.port} (ssl={self.ssl}, tls={self.tls})'
        )
        logger.debug(f'Connecting to SMTP server: {server_string}')

        cls = SMTP_SSL if self.ssl else SMTP

        smtp = cls(self.host, self.port)

        if DEBUG_SMTP:
            smtp.set_debuglevel(1)

        smtp.connect(self.host, self.port)

        if self.tls:
            smtp.starttls()

        smtp.login(self.username, self.password)

        yield smtp

        smtp.quit()
Exemplo n.º 2
0
    def get_connection(self, selected_folder=None):
        if not self.password:
            self.password = get_password('account', self.host, self.username)

        if not self.password:
            raise ConnectionSettingsError(
                self.account,
                'Missing IMAP password! Please re-enter your password in settings.',
            )

        connection = self.pool.get()
        self.log('debug',
                 f'Got connection from pool: {self.pool.qsize()} (-1)')

        try:
            if selected_folder:
                connection.set_selected_folder(selected_folder)

            yield connection

        finally:
            try:
                if selected_folder:
                    connection.unset_selected_folder()
            except Exception:
                self.log('warning', 'Failed to unselect folder!')

            self.pool.put(connection)
            self.log('debug',
                     f'Returned connection to pool: {self.pool.qsize()} (+1)')
Exemplo n.º 3
0
def api_get_settings() -> Response:
    settings = get_settings()

    account_name_to_connected = {}

    for account in settings.get('accounts', []):
        host = account['imap_connection']['host']
        username = account['imap_connection']['username']

        if account['imap_connection'].get('oauth_provider'):
            has_password = get_password('oauth-account', host,
                                        username) is not None
        else:
            has_password = get_password('account', host, username) is not None

        account_name_to_connected[account['name']] = has_password

    return jsonify(
        settings=settings,
        account_name_to_connected=account_name_to_connected,
        settings_file=SETTINGS_FILE,
    )
Exemplo n.º 4
0
def validate_or_remove_license() -> None:
    '''
    A "hard" check for the presence and validity of a license key. Will also remove
    any invalid license file/keyring item.

    This function is executed in a separate thread on app start.
    '''

    license_email = check_get_license_email()
    if not license_email:
        return

    logger.debug(f'Validating license for {license_email}...')

    combined_token = get_password('license', LICENSE_SERVER_APP_TOKEN,
                                  license_email)
    if not combined_token:
        return

    token, device_token = combined_token.split(':')

    try:
        response = requests.get(
            f'{LICENSE_SERVER_URL}/api/check',
            json={
                'app_token': LICENSE_SERVER_APP_TOKEN,
                'memo': license_email,
                'token': token,
                'device_token': device_token,
            },
        )
    except ConnectionError:
        logger.warning(
            'Could not connect to license server, please try again later!')
        return

    if response.status_code == 404:
        logger.warning('Valid license NOT found, removing!')
        remove_license()
        return

    if response.status_code == 201:
        logger.debug('Licence validated!')
        return

    try:
        response.raise_for_status()
    except HTTPError as e:
        logger.warning(
            f'Unexpected status from license server: {e} ({response.text})')
Exemplo n.º 5
0
    def check_auth_settings(self):
        missing_key = None
        fix_description = None

        if self.oauth_provider:
            if not self.oauth_refresh_token:
                self.oauth_refresh_token = get_password('oauth-account', self.host, self.username)

            if not self.oauth_refresh_token:
                missing_key = 'token'
                fix_description = 'Please delete & re-create this account in settings.'
        else:
            if not self.password:
                self.password = get_password('account', self.host, self.username)

            if not self.password:
                missing_key = 'password'
                fix_description = 'Please re-enter your password in settings.'

        if missing_key:
            raise ConnectionSettingsError(
                self.account,
                f'Missing {self.connection_type} {missing_key}! {fix_description}',
            )
Exemplo n.º 6
0
def check_get_license_email() -> Optional[str]:
    '''
    A "soft" check for the presence of a license file and associated keyring item.

    Note: this *does not* check the license against the key server.
    '''

    license_email = get_email_from_license_file()
    if not license_email:
        return

    combined_token = get_password('license', LICENSE_SERVER_APP_TOKEN,
                                  license_email)

    if combined_token:
        return license_email
Exemplo n.º 7
0
    def get_connection(self):
        if not self.password:
            self.password = get_password('account', self.host, self.username)

        if not self.password:
            raise ConnectionSettingsError(
                self.account,
                'Missing SMTP password! Please re-enter your password in settings.',
            )

        server_string = (
            f'{self.username}@{self.host}:{self.port} (ssl={self.ssl}, tls={self.tls})'
        )
        self.log('debug', f'Connecting to SMTP server: {server_string}')

        if self.ssl:
            ssl_context = ssl.create_default_context()
            if self.ssl_verify_hostname is False:
                self.log('warning', 'Disabling SSL hostname verification!')
                ssl_context.check_hostname = False

            smtp = SMTP_SSL(self.host, self.port, context=ssl_context)
        else:
            smtp = SMTP(self.host, self.port)

        if DEBUG_SMTP:
            smtp.set_debuglevel(1)

        smtp.connect(self.host, self.port)

        if self.tls:
            smtp.starttls()

        # Pending a *Python stdlib bugfix*: https://bugs.python.org/issue29750
        # username = self.username.encode('utf-8')
        # password = self.password.encode('utf-8')
        smtp.login(self.username, self.password)

        yield smtp

        smtp.quit()