Moved KeePassHttp handling into kpwriter.

This commit is contained in:
Markus Birth 2021-08-21 02:14:28 +02:00
parent c1b57c05a9
commit 1ea51e6e28
Signed by: mbirth
GPG Key ID: A9928D7A098C3A9A
2 changed files with 28 additions and 26 deletions

View File

@ -2,12 +2,10 @@
import argparse
import datetime
import json
import onepif
import kpwriter
from os.path import splitext
from urllib.parse import urlparse
parser = argparse.ArgumentParser(description="Convert 1Password 1PIF exports into a KeePass KDBX file.")
parser.add_argument("inpath", metavar="input.1pif", help="1Password export file/folder")
@ -153,9 +151,19 @@ for item in opif:
secure = item["secureContents"]
# URLs
if "location" in item:
kp.add_url(item["location"])
if "URLs" in secure:
for u in secure["URLs"]:
kp.add_url(u["url"])
# Username
if "username" in secure:
entry.username = secure["username"]
@ -254,28 +262,6 @@ for item in opif:
if "notesPlain" in secure:
entry.notes = secure["notesPlain"]
# URLs
settings = {
"Allow": [],
"Deny": [],
"Realm": "",
}
applySettings = False
if "location" in item:
entry.url = item["location"]
if "URLs" in secure:
kp2idx = 0
for u in secure["URLs"]:
kp.add_url(u["url"])
url = urlparse(u["url"])
settings["Allow"].append(url.hostname)
applySettings = True
if applySettings:
settings["Allow"] = list(set(settings["Allow"]))
entry.set_custom_property("KeePassHttp Settings", json.dumps(settings))
# Dates
entry.ctime = datetime.datetime.fromtimestamp(item["createdAt"])
entry.mtime = datetime.datetime.fromtimestamp(item["updatedAt"])

View File

@ -1,5 +1,6 @@
import json
import pykeepass.icons
from urllib.parse import quote_plus
from urllib.parse import quote_plus, urlparse
from pykeepass import create_database
@ -60,6 +61,21 @@ class KpWriter:
suffix = "_{}".format(suffix_ctr)
self.set_prop("KP2A_URL{}".format(suffix), url)
# KeePassHttp
current_settings = self.current_entry.get_custom_property("KeePassHttp Settings")
if current_settings:
current_settings = json.loads(current_settings)
else:
current_settings = {
"Allow": [],
"Deny": [],
"Realm": "",
}
parsed_url = urlparse(url)
current_settings["Allow"].append(parsed_url.hostname)
current_settings["Allow"] = list(set(current_settings["Allow"]))
self.set_prop("KeePassHttp Settings", json.dumps(current_settings))
def set_prop(self, key, value, protected=False):
self.current_entry.set_custom_property(key, value)
if protected: