-
Notifications
You must be signed in to change notification settings - Fork 3
/
db.go
126 lines (113 loc) · 2.66 KB
/
db.go
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"github.com/tobischo/gokeepasslib/v3"
"github.com/tobischo/gokeepasslib/v3/wrappers"
"log"
"os"
)
type db struct {
Version int
Entries []entry
}
type entry struct {
Type string
UUID string
Name string
Issuer string
Note string
Icon string
IconMime string `json:"icon_mime"`
Info dbInfo
}
type dbInfo struct {
Secret string
Algo string
Digits int
Period int
}
func (e entry) ToKeePassEntry() (gokeepasslib.Entry, error) {
convertedEntry := gokeepasslib.NewEntry()
var totpFormat string
switch e.Type {
case "totp":
totpFormat = fmt.Sprintf("%d;%d", e.Info.Period, e.Info.Digits)
case "steam":
totpFormat = fmt.Sprintf("%d;S", e.Info.Period)
default:
return convertedEntry, fmt.Errorf("unknown type: %s", e.Type)
}
values := []gokeepasslib.ValueData{
{
Key: "Title",
Value: gokeepasslib.V{
Content: e.Issuer,
},
},
{
Key: "UserName",
Value: gokeepasslib.V{
Content: e.Name,
},
},
{
Key: "Notes",
Value: gokeepasslib.V{
Content: e.Note,
},
},
{
Key: "TOTP Settings",
Value: gokeepasslib.V{
Content: totpFormat,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
{
Key: "TOTP Seed",
Value: gokeepasslib.V{
Content: e.Info.Secret,
Protected: wrappers.BoolWrapper{Bool: true},
},
},
}
convertedEntry.Values = values
return convertedEntry, nil
}
func (d db) ToKeePass(path string, password []byte) {
file, err := os.Create(path)
if err != nil {
errAndExit("Failed to create file for KeePass database: %v", err)
}
defer file.Close()
db := gokeepasslib.NewDatabase(gokeepasslib.WithDatabaseKDBXVersion4())
db.Content.Meta.DatabaseName = "TOTP" // TODO provide optional argument for database name
db.Credentials = gokeepasslib.NewPasswordCredentials(string(password))
entries := make([]gokeepasslib.Entry, 0, len(d.Entries))
for _, entry := range d.Entries {
converted, err := entry.ToKeePassEntry()
if err != nil {
log.Println(err)
continue
}
if entry.Icon != "" {
iconUUID := gokeepasslib.NewUUID()
db.Content.Meta.CustomIcons = append(db.Content.Meta.CustomIcons, gokeepasslib.CustomIcon{
UUID: iconUUID,
Data: entry.Icon,
})
converted.CustomIconUUID = iconUUID
}
entries = append(entries, converted)
}
db.Content.Root.Groups = []gokeepasslib.Group{{Name: "Default", Entries: entries}}
err = db.LockProtectedEntries()
if err != nil {
errAndExit("Failed to lock entries when saving KeePass database: %v", err)
}
keepassEncoder := gokeepasslib.NewEncoder(file)
err = keepassEncoder.Encode(db)
if err != nil {
errAndExit("Failed to save KeePass database: %v", err)
}
}