-
-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CredentialManager: Store passwords in system keyring
Credential Manager is used for safely storing passwords in system's keyring services.
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import keyring | ||
|
||
SERVICE_NAME = 'Orange3 - {}' | ||
|
||
|
||
class CredentialManager: | ||
""" | ||
Class for storage of passwords in the system keyring service. | ||
All attributes of this class are safely stored. | ||
Args: | ||
service_name (str): service name used for storing in keyring. | ||
Examples: | ||
>>> cm = CredentialManager('Widget Name') | ||
>>> cm.username = 'Orange' # store username | ||
>>> cm.password = 'Secret' # store password | ||
>>> cm.username # get username | ||
'Orange' | ||
>>> cm.password # get password | ||
'Secret' | ||
""" | ||
def __init__(self, service_name): | ||
self.__dict__['__service_name'] = SERVICE_NAME.format(service_name) | ||
|
||
@property | ||
def service_name(self): | ||
return self.__dict__['__service_name'] | ||
|
||
def __setattr__(self, key, value): | ||
keyring.set_password(self.service_name, key, value) | ||
|
||
def __getattr__(self, item): | ||
return keyring.get_password(self.service_name, item) | ||
|
||
def __delattr__(self, item): | ||
keyring.delete_password(self.service_name, item) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import unittest | ||
|
||
from Orange.widgets.credentials import CredentialManager | ||
|
||
|
||
class CredentialManagerTests(unittest.TestCase): | ||
def test_credential_manager(self): | ||
cm = CredentialManager('Orange') | ||
cm.key = 'Foo' | ||
self.assertEqual(cm.key, 'Foo') | ||
del cm.key | ||
self.assertEqual(cm.key, None) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters