Example #1
0
def activate_license(email: str, token: str) -> bool:
    try:
        response = requests.post(
            f'{LICENSE_SERVER_URL}/api/activate',
            json={
                'app_token': LICENSE_SERVER_APP_TOKEN,
                'memo': email,
                'token': token,
            },
        )
    except ConnectionError:
        raise LicenseActivationError(
            'Could not connect to license server, please try again later!')

    try:
        response.raise_for_status()
    except HTTPError:
        try:
            error = response.json()['error']
        except Exception:
            error = response.content
        raise LicenseActivationError(error)

    data = response.json()
    device_token = data['deviceToken']

    add_email_to_license_file(email)

    combined_token = f'{token}:{device_token}'
    set_password('license', LICENSE_SERVER_APP_TOKEN, email, combined_token)

    return True
Example #2
0
    def get_oauth_access_token(self):
        oauth_tokens = get_oauth_tokens_from_refresh_token(
            self.oauth_provider,
            self.oauth_refresh_token,
        )

        if oauth_tokens['refresh_token'] != self.oauth_refresh_token:
            self.oauth_refresh_token = oauth_tokens['refresh_token']
            set_password(
                'oauth-account',
                self.host,
                self.username,
                self.oauth_refresh_token,
            )

        return oauth_tokens['access_token']
Example #3
0
def fix_any_old_setings(settings: dict):
    # Inline to prevent circular reference
    # TODO: consider moving this function into it's own file
    from kanmail.secrets import set_password

    has_changed = False

    style_settings = settings.get('style')
    if style_settings:
        # Fix for settings.style.sidebar_folders changing from str -> list
        # pre v1.2002191933
        sidebar_folders = style_settings.get('sidebar_folders')
        if isinstance(sidebar_folders, str):
            style_settings['sidebar_folders'] = sidebar_folders.split(',')
            has_changed = True

    # "Fix" for settings.accounts used to be a dict, now  a list for ordering
    # pre v1.2002211810
    accounts = settings.get('accounts')
    if isinstance(accounts, dict):
        settings['accounts'] = [{
            'name': account_key,
            **account
        } for account_key, account in accounts.items()]
        has_changed = True

    for account_settings in settings.get('accounts', []):
        # Fix for settings.accounts.<name>.imap_connection.port changing from str -> int
        # pre v1.2002191933
        imap_settings = account_settings.get('imap_connection')
        if imap_settings:
            imap_port = imap_settings.get('port')
            if isinstance(imap_port, str):
                imap_settings['port'] = int(imap_port)
                has_changed = True

            # Fix for removing passwords from on-disk settings
            # pre v1.2002211810
            password = imap_settings.pop('password', None)
            if password:
                set_password('account', imap_settings['host'],
                             imap_settings['username'], password)
                has_changed = True

            # Populate default SSL verify hostname (advanced users can disable)
            # pre (including) v1.2004231151
            if 'ssl_verify_hostname' not in imap_settings:
                imap_settings['ssl_verify_hostname'] = True
                has_changed = True

        # Fix for settings.accounts.<name>.smtp_connection.port changing from str -> int
        # pre v1.2002191933
        smtp_settings = account_settings.get('smtp_connection')
        if smtp_settings:
            smtp_port = smtp_settings.get('port')
            if isinstance(smtp_port, str):
                smtp_settings['port'] = int(smtp_port)
                has_changed = True

            # Fix for removing passwords from on-disk settings
            # pre v1.2002211810
            password = smtp_settings.pop('password', None)
            if password:
                set_password('account', smtp_settings['host'],
                             smtp_settings['username'], password)
                has_changed = True

            # Populate default SSL verify hostname (advanced users can disable)
            # pre (including) v1.2004231151
            if 'ssl_verify_hostname' not in smtp_settings:
                smtp_settings['ssl_verify_hostname'] = True
                has_changed = True

    return has_changed
Example #4
0
def _extract_password(obj) -> None:
    if not obj or not all(k in obj for k in ('host', 'username', 'password')):
        return

    set_password('account', obj['host'], obj['username'], obj.pop('password'))
Example #5
0
def _extract_any_secret(secret_type: str, secret_key: str, obj: dict) -> None:
    if not obj or not all(k in obj for k in ('host', 'username', secret_key)):
        return

    set_password(secret_type, obj['host'], obj['username'],
                 obj.pop(secret_key))