Created
August 3, 2015 14:50
-
-
Save grenade/1a176d66bf51dd90ebce to your computer and use it in GitHub Desktop.
Should produce a json file containing secrets from the gnome keyring, but it doesn't. --http://stackoverflow.com/questions/31785239/serialise-child-collections-in-python-with-jsonpickle
This file contains hidden or 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
| #! /usr/bin/env python | |
| import secretstorage | |
| import jsonpickle | |
| class Secret(object): | |
| label = "" | |
| username = "" | |
| password = "" | |
| def __init__(self, secret): | |
| self.label = secret.get_label() | |
| self.password = '%s' % secret.get_secret() | |
| attributes = secret.get_attributes() | |
| if attributes and 'username_value' in attributes: | |
| self.username = '%s' % attributes['username_value'] | |
| class Collection(object): | |
| label = "" | |
| secrets = [] | |
| def __init__(self, collection): | |
| self.label = collection.get_label() | |
| for secret in collection.get_all_items(): | |
| self.secrets.append(Secret(secret)) | |
| def keyring_to_json(): | |
| collections = [] | |
| bus = secretstorage.dbus_init() | |
| for collection in secretstorage.get_all_collections(bus): | |
| collections.append(Collection(collection)) | |
| pickle = jsonpickle.encode(collections, unpicklable=False); | |
| print(pickle) | |
| if __name__ == '__main__': | |
| keyring_to_json() |
This file contains hidden or 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
| #! /usr/bin/env python | |
| import secretstorage | |
| def keyring_to_json(): | |
| target = open('secrets.json', 'w') | |
| target.write('{\n') | |
| json_collections = [] | |
| bus = secretstorage.dbus_init() | |
| collections = secretstorage.get_all_collections(bus) | |
| for collection in collections: | |
| label = '%s' % collection.get_label() | |
| if label == '': | |
| label = '_none' | |
| target.write(' "%s": [\n' % label) | |
| json_items = [] | |
| items = collection.get_all_items() | |
| for item in items: | |
| target.write(' {\n') | |
| target.write(' "label": "%s",\n' % (item.get_label())) | |
| attributes = item.get_attributes() | |
| if attributes and 'username_value' in attributes: | |
| target.write(' "username": "%s",\n' % attributes['username_value'].replace('\\', '\\\\')) | |
| target.write(' "password": "%s"\n' % item.get_secret().replace('\\', '\\\\')) | |
| target.write(' },\n') | |
| target.write(' ],\n') | |
| target.write('}\n') | |
| if __name__ == '__main__': | |
| keyring_to_json() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment