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:
Jelle Spijker 2021-03-11 14:21:51 +01:00
parent 720b356221
commit 47df060bee
No known key found for this signature in database
GPG key ID: 6662DC033BE6B99A
2 changed files with 27 additions and 4 deletions

View file

@ -17,7 +17,8 @@ from UM.i18n import i18nCatalog
from cura.OAuth2.AuthorizationHelpers import AuthorizationHelpers, TOKEN_TIMESTAMP_FORMAT
from cura.OAuth2.LocalAuthorizationServer import LocalAuthorizationServer
from cura.OAuth2.Models import AuthenticationResponse
import keyring
from cura.OAuth2.SecretStorage import SecretStorage
i18n_catalog = i18nCatalog("cura")
if TYPE_CHECKING:
@ -52,6 +53,8 @@ class AuthorizationService:
self.onAuthStateChanged.connect(self._authChanged)
self._secret_storage = SecretStorage()
def _authChanged(self, logged_in):
if logged_in and self._unable_to_get_data_message is not None:
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.
# 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:
self._auth_data = AuthenticationResponse(**preferences_data)
@ -263,7 +266,8 @@ class AuthorizationService:
# 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.
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.
# 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.accessTokenChanged.emit()