Coverage for pass_import/managers/gnomekeyring.py: 100%

38 statements  

« prev     ^ index     » next       coverage.py v7.4.3, created at 2024-02-26 12:11 +0000

1# -*- encoding: utf-8 -*- 

2# pass import - Passwords importer swiss army knife 

3# Copyright (C) 2017-2024 Alexandre PUJOL <alexandre@pujol.io>. 

4# 

5 

6try: 

7 import secretstorage 

8 SECRETSTORAGE = True 

9except ImportError: 

10 SECRETSTORAGE = False 

11 

12from pass_import.core import register_managers 

13from pass_import.manager import PasswordImporter 

14 

15 

16class GnomeKeyring(PasswordImporter): 

17 """Importer & exporter for Gnome Keyring. 

18 

19 :usage: 

20 You can provide a gnome-keyring collection label to import. It can be empty 

21 to import all collections. 

22 

23 """ 

24 name = 'gnome' 

25 format = 'libsecret' 

26 url = 'https://wiki.gnome.org/Projects/GnomeKeyring' 

27 himport = 'pass import gnome-keyring <label>' 

28 keys = {'login': 'account', 'url': 'host'} 

29 

30 # Import method 

31 

32 def parse(self): 

33 """Direct import from the Gnome keyring using Dbus.""" 

34 if not SECRETSTORAGE: 

35 raise ImportError(name='secretstorage') 

36 

37 keys = self.invkeys() 

38 connection = secretstorage.dbus_init() 

39 for collection in secretstorage.get_all_collections(connection): 

40 group = collection.get_label() 

41 if self.prefix not in ('', group): 

42 continue 

43 

44 collection.unlock() 

45 for item in collection.get_all_items(): 

46 entry = {} 

47 entry['group'] = group 

48 entry['title'] = item.get_label() 

49 entry['password'] = item.get_secret().decode('utf-8') 

50 entry['modified'] = item.get_modified() 

51 entry['created'] = item.get_created() 

52 for key, value in item.get_attributes().items(): 

53 entry[keys.get(key, key)] = value 

54 self.data.append(entry) 

55 

56 # Context manager methods 

57 

58 def exist(self): 

59 """Nothing to do.""" 

60 return True 

61 

62 @classmethod 

63 def isvalid(cls): 

64 """Nothing to do.""" 

65 return True 

66 

67 def open(self): 

68 """Nothing to open.""" 

69 

70 def close(self): 

71 """Nothing to close.""" 

72 

73 

74register_managers(GnomeKeyring)