Merge branch 'CURA-8332_Dont_crash_if_keyring_access_is_denied_on_mac' into 4.10

This commit is contained in:
Ghostkeeper 2021-06-21 17:36:06 +02:00
commit 3331b7c267
No known key found for this signature in database
GPG Key ID: D2A8871EE34EC59A

View File

@ -1,10 +1,10 @@
# Copyright (c) 2021 Ultimaker B.V.
# Cura is released under the terms of the LGPLv3 or higher.
from typing import Type, TYPE_CHECKING, Optional, List
from typing import Type, TYPE_CHECKING, Optional, List, Union
import keyring
from keyring.backend import KeyringBackend
from keyring.errors import NoKeyringError, PasswordSetError
from keyring.errors import NoKeyringError, PasswordSetError, KeyringError
from UM.Logger import Logger
@ -14,13 +14,24 @@ if TYPE_CHECKING:
# Need to do some extra workarounds on windows:
import sys
from UM.Platform import Platform
class _KeychainDenied(Exception):
pass
if Platform.isWindows() and hasattr(sys, "frozen"):
import win32timezone
from keyring.backends.Windows import WinVaultKeyring
keyring.set_keyring(WinVaultKeyring())
if Platform.isOSX() and hasattr(sys, "frozen"):
from keyring.backends.macOS import Keyring
from keyring.backends.macOS.api import KeychainDenied as _KeychainDeniedMacOS
KeychainDenied: Union[Type[_KeychainDenied], Type[_KeychainDeniedMacOS]] = _KeychainDeniedMacOS
keyring.set_keyring(Keyring())
else:
KeychainDenied = _KeychainDenied
# Even if errors happen, we don't want this stored locally:
DONT_EVER_STORE_LOCALLY: List[str] = ["refresh_token"]
@ -39,6 +50,10 @@ class KeyringAttribute:
self._store_secure = False
Logger.logException("w", "No keyring backend present")
return getattr(instance, self._name)
except KeychainDenied:
self._store_secure = False
Logger.log("i", "Access to the keyring was denied.")
return getattr(instance, self._name)
else:
return getattr(instance, self._name)