This repository has been archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
item.go
323 lines (292 loc) · 8.87 KB
/
item.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
package op
import (
"time"
)
type Item struct {
ID string `json:"id"`
Title string `json:"title"`
Favorite bool `json:"favorite"`
Tags []string `json:"tags"`
Version int `json:"version"`
State ItemState `json:"state"`
Vault Vault `json:"vault"`
Category Category `json:"category"`
LastEditedBy string `json:"last_edited_by"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
AdditionalInformation string `json:"additional_information"`
URLs []URL `json:"urls"`
Sections []Section `json:"sections"`
Fields []Field `json:"fields"`
Files []File `json:"files"`
}
func (i Item) Field(name string) *Field {
var f *Field
for _, field := range i.Fields {
if field.ID == name || field.Label == name {
f = &field
break
}
}
return f
}
func (i Item) FindFields(match func(f Field) bool) []Field {
var f []Field
for _, field := range i.Fields {
if match(field) {
f = append(f, field)
}
}
return f
}
type Category string
const (
CategoryAPICredential = "API Credential"
CategoryBankAccount = "Bank Account"
CategoryCreditCard = "Credit Card"
CategoryDatabase = "Database"
CategoryDocument = "Document"
CategoryDriverLicense = "Driver License"
CategoryEmailAccount = "Email Account"
CategoryIdentity = "Identity"
CategoryLogin = "Login"
CategoryMedicalRecord = "Medical Record"
CategoryMembership = "Membership"
CategoryOutdoorLicense = "Outdoor License"
CategoryPassport = "Passport"
CategoryPassword = "Password"
CategoryRewardProgram = "Reward Program"
CategorySecureNote = "Secure Note"
CategoryServer = "Server"
CategorySocialSecurityNumber = "Social Security Number"
CategorySoftwareLicense = "Software License"
CategorySSHKey = "SSH Key"
CategoryWirelessRouter = "Wireless Router"
)
func (c *Category) UnmarshalText(text []byte) error {
switch string(text) {
case "API_CREDENTIAL":
*c = CategoryAPICredential
case "BANK_ACCOUNT":
*c = CategoryBankAccount
case "CREDIT_CARD":
*c = CategoryCreditCard
case "DATABASE":
*c = CategoryDatabase
case "DOCUMENT":
*c = CategoryDocument
case "DRIVER_LICENSE":
*c = CategoryDriverLicense
case "EMAIL_ACCOUNT":
*c = CategoryEmailAccount
case "IDENTITY":
*c = CategoryIdentity
case "LOGIN":
*c = CategoryLogin
case "MEDICAL_RECORD":
*c = CategoryMedicalRecord
case "MEMBERSHIP":
*c = CategoryMembership
case "OUTDOOR_LICENSE":
*c = CategoryOutdoorLicense
case "PASSPORT":
*c = CategoryPassport
case "PASSWORD":
*c = CategoryPassword
case "REWARD_PROGRAM":
*c = CategoryRewardProgram
case "SECURE_NOTE":
*c = CategorySecureNote
case "SERVER":
*c = CategoryServer
case "SOCIAL_SECURITY_NUMBER":
*c = CategorySocialSecurityNumber
case "SOFTWARE_LICENSE":
*c = CategorySoftwareLicense
case "SSH_KEY":
*c = CategorySSHKey
case "WIRELESS_ROUTER":
*c = CategoryWirelessRouter
default:
}
return nil
}
type Field struct {
ID string `json:"id"`
Section Section `json:"section"`
Type FieldType `json:"type"`
Purpose FieldPurpose `json:"purpose"`
Label string `json:"label"`
Value string `json:"value"`
TOTP string `json:"totp"`
Entropy int64 `json:"entropy"`
PasswordDetails PasswordDetails `json:"password_details"`
Reference string `json:"reference"`
}
type FieldAssignment struct {
Label string `json:"label"`
Type FieldAssignmentType `json:"type"`
Value string `json:"value"`
Purpose FieldPurpose `json:"purpose"`
}
type FieldAssignmentType string
const (
FieldAssignmentTypeConcealed = "concealed"
FieldAssignmentTypeText = "text"
FieldAssignmentTypeEmail = "email"
FieldAssignmentTypeURL = "url"
FieldAssignmentTypeDate = "date"
FieldAssignmentTypeMonthYear = "monthYear"
FieldAssignmentTypePhone = "phone"
)
type FieldType string
const (
FieldTypeAddress = "address"
FieldTypeConcealed = "concealed"
FieldTypeCreditCardNumber = "ccnum"
FieldTypeCreditCardType = "cctype"
FieldTypeDate = "date"
FieldTypeEmail = "email"
FieldTypeFile = "file"
FieldTypeGender = "gender"
FieldTypeMenu = "menu"
FieldTypeMonthYear = "monthYear"
FieldTypeOTP = "OTP"
FieldTypePhone = "phone"
FieldTypeReference = "reference"
FieldTypeSSHKey = "sshkey"
FieldTypeString = "string"
FieldTypeUnknown = ""
FieldTypeURL = "URL"
)
func (f *FieldType) UnmarshalText(text []byte) error {
switch string(text) {
case "ADDRESS":
*f = FieldTypeAddress
case "CONCEALED":
*f = FieldTypeConcealed
case "CREDIT_CARD_NUMBER":
*f = FieldTypeCreditCardNumber
case "CREDIT_CARD_TYPE":
*f = FieldTypeCreditCardType
case "DATE":
*f = FieldTypeDate
case "EMAIL":
*f = FieldTypeEmail
case "FILE":
*f = FieldTypeFile
case "GENDER":
*f = FieldTypeGender
case "MENU":
*f = FieldTypeMenu
case "MONTH_YEAR":
*f = FieldTypeMonthYear
case "OTP":
*f = FieldTypeOTP
case "PHONE":
*f = FieldTypePhone
case "REFERENCE":
*f = FieldTypeReference
case "SSHKEY":
*f = FieldTypeSSHKey
case "STRING":
*f = FieldTypeString
case "URL":
*f = FieldTypeURL
case "UNKNOWN":
fallthrough
default:
*f = FieldTypeUnknown
}
return nil
}
type FieldPurpose string
const (
FieldPurposeNotes = "NOTES"
FieldPurposePassword = "PASSWORD"
FieldPurposeUsername = "USERNAME"
)
type File struct {
ID string `json:"id"`
Name string `json:"name"`
Size int64 `json:"size"`
ContentPath string `json:"content_path"`
Section Section `json:"section"`
}
type ItemState string
const (
ItemStateArchived = "ARCHIVED"
)
type PasswordDetails struct {
Entropy int64 `json:"entropy"`
Generated bool `json:"generated"`
Strength PasswordStrength `json:"strength"`
}
type PasswordStrength string
const (
PasswordStrengthTerrible = "TERRIBLE"
PasswordStrengthWeak = "WEAK"
PasswordStrengthFair = "FAIR"
PasswordStrengthGood = "GOOD"
PasswordStrengthVeryGood = "VERY_GOOD"
PasswordStrengthExcellent = "EXCELLENT"
PasswordStrengthFantastic = "FANTASTIC"
)
type Section struct {
ID string `json:"id"`
Label string `json:"label"`
}
type URL struct {
Label string `json:"label"`
Primary bool `json:"primary"`
HRef string `json:"href"`
}
// ListItems returns a list of all items the account has read access to.
// Excludes items in the Archive by default.
//
// Supported filters:
//
// - WithIncludeArchive() Include items in the Archive.
// - WithCategories() Only list items in these categories (comma-separated).
// - WithFavorite() Only list favorite items
// - WithTags() Only list items with these tags (comma-separated).
// - WithVault() Only list items in this vault.
func (c *CLI) ListItems(filters ...Filter) ([]Item, error) {
var val []Item
err := c.execJSON(applyFilters([]string{"item", "list"}, filters), nil, &val)
return val, err
}
// CreateItem creates a new item and returns it with all the fileds like ID filled.
//
// --dry-run Perform a dry run of the command and output a preview of the resulting item.
// --generate-password[=recipe] Give the item a randomly generated password.
func (c *CLI) CreateItem(item *Item) (*Item, error) {
return nil, nil
}
// GetItem returns the details of an item specified by its name, ID, or sharing link.
//
// Supported filters:
//
// - WithIncludeArchive() Include items in the Archive.
// - WithVault() Only list items in this vault.
func (c *CLI) GetItem(name string, filters ...Filter) (*Item, error) {
var val *Item
err := c.execJSON(applyFilters([]string{"item", "get", sanitize(name)}, filters), nil, &val)
return val, err
}
// GetItemTemplate returns an item template for a given item category.
func (c *CLI) GetItemTemplate(cat Category) (*Item, error) {
var val *Item
err := c.execJSON([]string{"item", "template", "get", sanitize(string(cat))}, nil, &val)
return val, err
}
// DeleteItem permanently deletes an item specified by its name, ID, or sharing link.
func (c *CLI) DeleteItem(name string) error {
_, err := c.execRaw([]string{"item", "delete", sanitize(name)}, nil)
return err
}
// ArchiveItem archives the item specified by its name, ID, or sharing link.
func (c *CLI) ArchiveItem(name string) error {
_, err := c.execRaw([]string{"item", "delete", sanitize(name), "--archive"}, nil)
return err
}