| 105 | return add_parser |
| 106 | |
| 107 | def execute(self, params, **kwargs): |
| 108 | title = kwargs['title'] if 'title' in kwargs else None |
| 109 | login = kwargs['login'] if 'login' in kwargs else None |
| 110 | password = kwargs['password'] if 'password' in kwargs else None |
| 111 | url = kwargs['url'] if 'url' in kwargs else None |
| 112 | custom_list = kwargs['custom'] if 'custom' in kwargs else None |
| 113 | notes = kwargs['notes'] if 'notes' in kwargs else None |
| 114 | |
| 115 | generate = kwargs['generate'] if 'generate' in kwargs else None |
| 116 | if generate: |
| 117 | password = None |
| 118 | |
| 119 | force = kwargs['force'] if 'force' in kwargs else None |
| 120 | if not force: |
| 121 | if not title: |
| 122 | title = input('...' + 'Title: '.rjust(16)) |
| 123 | if not login: |
| 124 | login = input('...' + 'Login: '.rjust(16)) |
| 125 | if not password and not generate: |
| 126 | password = input('...' + 'Password: '.rjust(16)) |
| 127 | if not url: |
| 128 | url = input('...' + 'Login URL: '.rjust(16)) |
| 129 | if not title: |
| 130 | raise CommandError('add', 'Invalid title. Expected non-empty string.') |
| 131 | |
| 132 | password_policy_source = {'url': url or ''} |
| 133 | if generate: |
| 134 | policy = PasswordComplexityEnforcer.get_generator_policy( |
| 135 | params, password_policy_source) |
| 136 | password = (generator.KeeperPasswordGenerator.create_from_policy(policy).generate() |
| 137 | if policy else generator.generate(16)) |
| 138 | _enforce_password_policy( |
| 139 | params, password_policy_source, password, bool(generate), 'add', force=force) |
| 140 | |
| 141 | custom = [] |
| 142 | if custom_list: |
| 143 | if type(custom_list) == str: |
| 144 | if custom_list[0] == '{' and custom_list[-1] == '}': |
| 145 | try: |
| 146 | custom_json = json.loads(custom_list) |
| 147 | for k, v in custom_json.items(): |
| 148 | custom.append({ |
| 149 | 'name': k, |
| 150 | 'value': self.custom_field_value(v) |
| 151 | }) |
| 152 | except ValueError as e: |
| 153 | raise CommandError('add', 'Invalid custom fields JSON input: {0}'.format(e)) |
| 154 | else: |
| 155 | pairs = custom_list.split(',') |
| 156 | for pair in pairs: |
| 157 | idx = pair.find(':') |
| 158 | if idx > 0: |
| 159 | custom.append({ |
| 160 | 'name': pair[:idx].strip(), |
| 161 | 'value': self.custom_field_value(pair[idx+1:].strip()) |
| 162 | }) |
| 163 | else: |
| 164 | raise CommandError('add', 'Invalid custom fields input. Expected: "Key:Value". Got: "{0}"'.format(pair)) |