-
Notifications
You must be signed in to change notification settings - Fork 0
/
types.go
188 lines (145 loc) · 4.42 KB
/
types.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package types
import (
"regexp"
"time"
)
type Version int
const (
V1 Version = iota
V2
)
// abstracts a wrapper for v1 or v2 implementations to use to describe the database and to implement shell commands
type KeepassWrapper interface {
// Raw returns the underlying object that the wrapper wraps aroud
Raw() interface{}
// Path returns the path to the object's location
Path() (string, error)
// Search searches this object and all nested objects for a given regular expression
Search(*regexp.Regexp) ([]string, error)
}
type Database interface {
KeepassWrapper
// Binary returns a binary with a given ID, naming it with a given name
// the OptionalWrapper is used because v2 is the only version that implements this
Binary(id int, name string) (OptionalWrapper, error)
// Changed indicates whether the DB has been changed during the user's session
Changed() bool
SetChanged(bool)
// CurrentLocation returns the current location for the shell
CurrentLocation() Group
SetCurrentLocation(Group)
Root() Group
Save() error
// Init initializes a database wrapper, using the given parameters. Existing DB will be opened, otherwise the wrapper will be configured to save to that location
Init(Options) error
// Lock will lock the database by dropping a lockfile
Lock() error
// Unlock will remove the lockfile created by Lock()
Unlock() error
// Locked will determine if the lockfile is in place
Locked() bool
// SavePath dictates where the DB will be saved
SavePath() string
SetSavePath(string)
// Version will return the Version enum for this database
Version() Version
}
// Options are parameters to use for calls to the database interface's Init function
type Options struct {
// the path to the database
DBPath string
// the path to the key
KeyPath string
// the password for the database
Password string
// How many rounds of encryption to use for the new key (currently only supported by keepassv1)
KeyRounds int
}
type UUIDer interface {
// UUIDString returns the string form of this object's UUID
UUIDString() (string, error)
}
type Group interface {
KeepassWrapper
UUIDer
// Returns all entries in this group
Entries() []Entry
// Returns all groups nested in this group
Groups() []Group
// Returns this group's parent, if it has one
Parent() Group
SetParent(Group) error
// inverse of 'SetParent', needed mainly for the internals of keepassv2
AddEntry(Entry) error
Name() string
SetName(string)
IsRoot() bool
// Creates a new subgroup with a given name under this group
NewSubgroup(name string) (Group, error)
RemoveSubgroup(Group) error
AddSubgroup(Group) error
NewEntry(name string) (Entry, error)
RemoveEntry(Entry) error
}
type Entry interface {
UUIDer
KeepassWrapper
// Returns the value for a given field, or an empty struct if the field doesn't exist
Get(string) Value
// Title and Password are needed to ensure that v1 and v2 both render
// their specific representations of that data (they access it in different ways, fun times)
Title() string
SetTitle(string)
Password() string
SetPassword(string)
Username() string
SetUsername(name string)
// Sets a given field to a given value, returns bool indicating whether or not the field was updated
Set(value Value) bool
LastAccessTime() time.Time
SetLastAccessTime(time.Time)
LastModificationTime() time.Time
SetLastModificationTime(time.Time)
CreationTime() time.Time
SetCreationTime(time.Time)
ExpiredTime() time.Time
SetExpiredTime(time.Time)
Parent() Group
SetParent(Group) error
// Formats an entry for printing
Output(full bool) string
// Values returns all referencable value fields from the database
//
// NOTE: values are not references, updating them must be done through the Set* functions
Values() (values []Value, err error)
// DB returns the Database this entry is associated with
DB() Database
SetDB(Database)
}
type ValueType int
const (
STRING ValueType = iota
LONGSTRING
BINARY
)
// OptionalWrapper wraps Values with functions that force the caller of a function to detect whether the value being
// returned is implemented by the function, this is to help bridge the gap between v2 and v1
// Proper usage:
// if wrapper.Present {
// <use value>
// } else {
// <adapt>
// }
type OptionalWrapper struct {
Present bool
Value Value
}
type Value interface {
FormattedValue(full bool) string
Value() []byte
Name() string
Searchable() bool
Protected() bool
ReadOnly() bool
Type() ValueType
}