-
Notifications
You must be signed in to change notification settings - Fork 0
/
bitwarden2pass.py
executable file
·91 lines (70 loc) · 2.27 KB
/
bitwarden2pass.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env python3
import glob
import json
import subprocess
def parseFolders(bitwardenFolders):
folders = {}
for folder in bitwardenFolders:
folders[folder["id"]] = folder["name"]
return folders
def readBitwardenJson(fname):
with open(fname) as f:
inJson = f.read()
input = json.loads(inJson)
return input
def insertPass(path, username=None, password="", uris=None, totp=None, extra=None):
msg = "%s\n" % password
if username:
msg += "Username: %s\n" % username
if uris:
for uri in uris:
msg += "URL: %s\n" % uri
if totp:
msg += "otpauth://totp/totp-secret?secret=%s\n" % totp
if extra:
for name, value in extra:
msg += "x_%s: %s\n" % (name, value)
p = subprocess.Popen(["gopass", "insert", "-m", "-f", path], stdin=subprocess.PIPE)
stdout, stderr = p.communicate(input=msg.encode("utf-8"))
if p.returncode != 0:
print("failed %s: %s" % (path, stderr))
else:
print("ok %s: %s" % (path, stdout))
def main():
filename = glob.glob("bitwarden_export_*.json")[0]
j = readBitwardenJson(filename)
folders = parseFolders(j["folders"])
for item in j["items"]:
path = "%s/%s" % (folders[item["folderId"]], item["name"])
path = path.replace(" ", "_")
path = path.replace("_-_", "_")
path = path.replace("&", "_and_")
username = None
password = None
uris = []
totp = None
extra = []
if "login" in item:
login = item["login"]
if "username" in login:
username = (login["username"],)
if "password" in login:
password = (login["password"],)
if "uris" in login and login["uris"]:
for uri in login["uris"]:
uris.append(uri["uri"])
if "totp" in login:
totp = login["totp"]
if "fields" in item:
for field in item["fields"]:
extra.append((field["name"], field["value"]))
insertPass(
path,
username=username,
password=password,
uris=uris,
totp=totp,
extra=extra,
)
if __name__ == "__main__":
main()