mirror of
https://github.com/Ultimaker/Cura.git
synced 2025-07-16 19:28:07 -06:00
Added fundaments of SecretStorage vault
This class will handle the storing and processing of secrets. Such as tokens. It will try to use the system keyring by default. Falling back to less secure methods, if the user doesn't allow access to the keyring or if the back-end is unsupported. CURA-7180 keyring storage
This commit is contained in:
parent
720b356221
commit
47df060bee
2 changed files with 27 additions and 4 deletions
|
@ -17,7 +17,8 @@ from UM.i18n import i18nCatalog
|
||||||
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT
|
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT
|
||||||
from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
|
from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
|
||||||
from cura.OAuth2.Models import AuthenticationResponse
|
from cura.OAuth2.Models import AuthenticationResponse
|
||||||
import keyring
|
from cura.OAuth2.SecretStorage import SecretStorage
|
||||||
|
|
||||||
i18n_catalog = i18nCatalog("cura")
|
i18n_catalog = i18nCatalog("cura")
|
||||||
|
|
||||||
if TYPE_CHECKING:
|
if TYPE_CHECKING:
|
||||||
|
@ -52,6 +53,8 @@ class AuthorizationService:
|
||||||
|
|
||||||
self.onAuthStateChanged.connect(self._authChanged)
|
self.onAuthStateChanged.connect(self._authChanged)
|
||||||
|
|
||||||
|
self._secret_storage = SecretStorage()
|
||||||
|
|
||||||
def _authChanged(self, logged_in):
|
def _authChanged(self, logged_in):
|
||||||
if logged_in and self._unable_to_get_data_message is not None:
|
if logged_in and self._unable_to_get_data_message is not None:
|
||||||
self._unable_to_get_data_message.hide()
|
self._unable_to_get_data_message.hide()
|
||||||
|
@ -232,7 +235,7 @@ class AuthorizationService:
|
||||||
|
|
||||||
# Since we stored all the sensitive stuff in the keyring, restore that now.
|
# Since we stored all the sensitive stuff in the keyring, restore that now.
|
||||||
# Don't store the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
# Don't store the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
||||||
preferences_data["refresh_token"] = keyring.get_password("cura", "refresh_token")
|
preferences_data["refresh_token"] = self._secret_storage["refresh_token"]
|
||||||
|
|
||||||
if preferences_data:
|
if preferences_data:
|
||||||
self._auth_data = AuthenticationResponse(**preferences_data)
|
self._auth_data = AuthenticationResponse(**preferences_data)
|
||||||
|
@ -263,7 +266,8 @@ class AuthorizationService:
|
||||||
|
|
||||||
# Store all the sensitive stuff in the keyring
|
# Store all the sensitive stuff in the keyring
|
||||||
# Don't store the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
# Don't store the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
||||||
keyring.set_password("cura", "refresh_token", auth_data.refresh_token)
|
self._secret_storage["refresh_token"] = auth_data.refresh_token
|
||||||
|
|
||||||
|
|
||||||
# And remove that data again so it isn't stored in the preferences.
|
# And remove that data again so it isn't stored in the preferences.
|
||||||
# Keep the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
# Keep the access_token, as it's very long and that (or tried workarounds) causes issues on Windows.
|
||||||
|
@ -275,4 +279,3 @@ class AuthorizationService:
|
||||||
self._preferences.resetPreference(self._settings.AUTH_DATA_PREFERENCE_KEY)
|
self._preferences.resetPreference(self._settings.AUTH_DATA_PREFERENCE_KEY)
|
||||||
|
|
||||||
self.accessTokenChanged.emit()
|
self.accessTokenChanged.emit()
|
||||||
|
|
||||||
|
|
20
cura/OAuth2/SecretStorage.py
Normal file
20
cura/OAuth2/SecretStorage.py
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import keyring
|
||||||
|
|
||||||
|
|
||||||
|
class SecretStorage:
|
||||||
|
def __init__(self):
|
||||||
|
self._stored_secrets = []
|
||||||
|
|
||||||
|
def __delitem__(self, key):
|
||||||
|
if key in self._stored_secrets:
|
||||||
|
del self._stored_secrets[key]
|
||||||
|
keyring.delete_password("cura", key)
|
||||||
|
|
||||||
|
def __setitem__(self, key, value):
|
||||||
|
self._stored_secrets.append(key)
|
||||||
|
keyring.set_password("cura", key, value)
|
||||||
|
|
||||||
|
def __getitem__(self, key):
|
||||||
|
if key in self._stored_secrets:
|
||||||
|
return keyring.get_password("cura", key)
|
||||||
|
return None
|
Loading…
Add table
Add a link
Reference in a new issue