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

42 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 

6import json 

7import os 

8 

9from pass_import.clean import replaces 

10from pass_import.core import register_managers 

11from pass_import.formats.csv import CSV 

12from pass_import.formats.json import JSON 

13 

14 

15class PassmanCSV(CSV): 

16 """Importer for Passman in CSV format.""" 

17 name = 'passman' 

18 url = 'https://passman.cc' 

19 hexport = 'Settings > Export credentials > Export type: CSV' 

20 himport = 'pass import passman file.csv' 

21 keys = { 

22 'title': 'label', 

23 'password': 'password', 

24 'login': 'username', 

25 'email': 'email', 

26 'url': 'url', 

27 'comments': 'description', 

28 'group': 'tags' 

29 } 

30 

31 def parse(self): 

32 """Parse Passman CSV file.""" 

33 super().parse() 

34 characters = {'\\': os.sep, '[': '', ']': ''} 

35 for entry in self.data: 

36 entry['group'] = replaces(characters, entry.get('group', '')) 

37 

38 

39class PassmanJSON(JSON): 

40 """Importer for Passman in JSON format.""" 

41 name = 'passman' 

42 default = False 

43 url = 'https://passman.cc' 

44 hexport = 'Settings > Export credentials > Export type: JSON' 

45 himport = 'pass import passman file.json' 

46 keys = { 

47 'title': 'label', 

48 'login': 'username', 

49 'comments': 'description', 

50 'otpauth': 'otp' 

51 } 

52 json_header = [{ 

53 'credential_id': int, 

54 'guid': str, 

55 'user_id': str, 

56 'label': str, 

57 'description': str, 

58 'tags': list, 

59 'username': str, 

60 'password': str, 

61 'url': str, 

62 'icon': dict, 

63 'custom_fields': list, 

64 'otp': dict, 

65 'compromised': bool 

66 }] 

67 

68 def parse(self): 

69 """Parse Passman JSON file.""" 

70 ignore = {'custom_fields', 'icon', 'tags'} 

71 keys = self.invkeys() 

72 jsons = json.loads(self.file.read()) 

73 for item in jsons: 

74 entry = {} 

75 if item['tags']: 

76 group = item['tags'][0]['text'] 

77 entry['group'] = group.replace('\\', os.sep) 

78 custom_fields = item.get('custom_fields', []) 

79 for field in custom_fields: 

80 item.update({field['label']: field['value']}) 

81 

82 for key, value in item.items(): 

83 if key not in ignore: 

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

85 

86 self.data.append(entry) 

87 

88 

89register_managers(PassmanCSV, PassmanJSON)