forked from stuartcryan/lastpass-alfred-workflow
/
config.go
601 lines (562 loc) · 18.3 KB
/
config.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// Copyright (c) 2020 Claas Lisowski <github@lisowski-development.com>
// MIT Licence - http://opensource.org/licenses/MIT
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strconv"
"strings"
"time"
"github.com/blacs30/bitwarden-alfred-workflow/alfred"
"github.com/kelseyhightower/envconfig"
"log"
aw "github.com/deanishe/awgo"
)
// Valid modifier keys used to specify alternate actions in Script Filters.
var (
conf config
mod1 []string
mod1Emoji string
mod2 []string
mod2Emoji string
mod3 []string
mod3Emoji string
mod4 []string
mod4Emoji string
mod5 []string
mod5Emoji string
bwData BwData
)
func loadBitwardenJSON() error {
bwDataPath := conf.BwDataPath
if bwDataPath == "" {
homedir, err := os.UserHomeDir()
if err != nil {
return err
}
bwDataPath = fmt.Sprintf("%s/Library/Application Support/Bitwarden CLI/data.json", homedir)
debugLog(fmt.Sprintf("bwDataPath is: %s", bwDataPath))
}
if err := loadDataFile(bwDataPath); err != nil {
return err
}
return nil
}
func loadDataFile(path string) error {
f, err := os.Open(path)
if os.IsNotExist(err) {
return nil
} else if err != nil {
return err
}
defer f.Close()
byteData, err := io.ReadAll(f)
if err != nil {
return err
}
bwConfigData, err := decodeBitwardenDataJson(byteData)
if err != nil {
return err
}
bwData = bwConfigData
bwData.path = path
return nil
}
func decodeBitwardenDataJson(byteData []byte) (BwData, error) {
// unmarshal and decode towards an interface
newBwData := BwData{}
var parsed interface{}
err := json.Unmarshal(byteData, &parsed)
if err != nil {
return newBwData, err
}
switch parsed.(type) {
case map[string]interface{}:
table := parsed.(map[string]interface{})
if val, ok := table["userId"]; ok {
newBwData.UserId = fmt.Sprintf("%s", val)
if val == nil {
newBwData.UserId = ""
}
} else if val == nil {
newBwData.UserId = ""
}
if val, ok := table["activeUserId"]; ok {
newBwData.ActiveUserId = fmt.Sprintf("%s", val)
if val == nil {
newBwData.ActiveUserId = ""
}
} else if val == nil {
newBwData.ActiveUserId = ""
}
if newBwData.ActiveUserId != "" && newBwData.UserId == "" {
// version 1.21.1 and newer
newBwData.UserId = newBwData.ActiveUserId
if val, ok := table[fmt.Sprintf("__PROTECTED__%s_user_auto", newBwData.UserId)]; ok {
newBwData.ProtectedKey = fmt.Sprintf("%s", val)
}
if val, ok := table["global"]; ok {
globalTable := val.(map[string]interface{})
if globalVal, ok := globalTable["installedVersion"]; ok {
newBwData.InstalledVersion = fmt.Sprintf("%s", globalVal)
newBwData.Global.InstalledVersion = fmt.Sprintf("%s", globalVal)
}
}
if val, ok := table[newBwData.UserId]; ok {
userTable := val.(map[string]interface{})
if keyVal, ok := userTable["keys"]; ok {
if apiKeyVal, ok := keyVal.(map[string]interface{})["apiKeyClientSecret"]; ok {
newBwData.Keys.ApiKeyClientSecret = fmt.Sprintf("%s", apiKeyVal)
}
if cryptSymKeyVal, ok := keyVal.(map[string]interface{})["cryptoSymmetricKey"]; ok {
if val, ok := cryptSymKeyVal.(map[string]interface{})["encrypted"]; ok {
newBwData.Keys.CryptoSymmetricKey.Encrypted = fmt.Sprintf("%s", val)
newBwData.EncKey = newBwData.Keys.CryptoSymmetricKey.Encrypted
}
}
// For Version >= 2023.8.2
if masterKeyEncryptedUserKeyVal, ok := keyVal.(map[string]interface{})["masterKeyEncryptedUserKey"]; ok {
newBwData.Keys.CryptoSymmetricKey.Encrypted = fmt.Sprintf("%s", masterKeyEncryptedUserKeyVal)
newBwData.EncKey = newBwData.Keys.CryptoSymmetricKey.Encrypted
}
if privateKeyVal, ok := keyVal.(map[string]interface{})["privateKey"]; ok {
if val, ok := privateKeyVal.(map[string]interface{})["encrypted"]; ok {
newBwData.Keys.PrivateKey.Encrypted = fmt.Sprintf("%s", val)
}
}
}
if tokensVal, ok := userTable["tokens"]; ok {
if val, ok := tokensVal.(map[string]interface{})["accessToken"]; ok {
newBwData.Tokens.AccessToken = fmt.Sprintf("%s", val)
}
}
if profileVal, ok := userTable["profile"]; ok {
if val, ok := profileVal.(map[string]interface{})["everBeenUnlocked"]; ok {
newBwData.Profile.EverBeenUnlocked, _ = strconv.ParseBool(fmt.Sprintf("%t", val))
}
if val, ok := profileVal.(map[string]interface{})["lastSync"]; ok {
newBwData.Profile.LastSync = fmt.Sprintf("%s", val)
}
if val, ok := profileVal.(map[string]interface{})["email"]; ok {
newBwData.Profile.Email = fmt.Sprintf("%s", val)
newBwData.UserEmail = newBwData.Profile.Email
}
if val, ok := profileVal.(map[string]interface{})["userId"]; ok {
newBwData.Profile.UserId = fmt.Sprintf("%s", val)
}
if val, ok := profileVal.(map[string]interface{})["kdfIterations"]; ok {
kdfIteractionsFloat64, _ := strconv.ParseFloat(fmt.Sprintf("%f", val), 64)
newBwData.KdfIterations = int64(kdfIteractionsFloat64)
newBwData.Profile.KdfIterations = newBwData.KdfIterations
}
if val, ok := profileVal.(map[string]interface{})["kdfType"]; ok {
kdfFloat64, _ := strconv.ParseFloat(fmt.Sprintf("%f", val), 64)
newBwData.Kdf = int64(kdfFloat64)
newBwData.Profile.KdfType = newBwData.Kdf
}
}
}
} else {
// version 1.21.0 and earlier
newBwData.InstalledVersion = fmt.Sprintf("%s", table["installedVersion"])
newBwData.UserEmail = fmt.Sprintf("%s", table["userEmail"])
newBwData.ProtectedKey = fmt.Sprintf("%s", table["__PROTECTED__key"])
newBwData.EncKey = fmt.Sprintf("%s", table["encKey"])
// kdfIterations is the only int/float value
kdfIteractionsFloat64, _ := strconv.ParseFloat(fmt.Sprintf("%f", table["kdfIterations"]), 64)
newBwData.KdfIterations = int64(kdfIteractionsFloat64)
kdfFloat64, _ := strconv.ParseFloat(fmt.Sprintf("%f", table["kdf"]), 64)
newBwData.Kdf = int64(kdfFloat64)
}
default:
return newBwData, fmt.Errorf("type %T unexpected", parsed)
}
return newBwData, nil
}
func loadConfig() {
// Load workflow vars
err := envconfig.Process("", &conf)
if err != nil {
log.Fatal(err.Error())
}
// load the bitwarden data.json
err = loadBitwardenJSON()
if err != nil {
log.Print(err.Error())
}
debugLog(fmt.Sprintf("BwData config is: %+v", bwData))
conf.Email = alfred.GetEmail(wf, conf.Email, bwData.UserEmail)
conf.OutputFolder = alfred.GetOutputFolder(wf, conf.OutputFolder)
// Set a few cache timeout durations
iconCacheAgeDuration := time.Duration(conf.IconCacheAge)
conf.IconMaxCacheAge = iconCacheAgeDuration * time.Minute
autoFetchIconCacheAgeDuration := time.Duration(conf.AutoFetchIconCacheAge)
conf.AutoFetchIconMaxCacheAge = autoFetchIconCacheAgeDuration * time.Minute
conf.BwauthKeyword = os.Getenv("bwauth_keyword")
conf.BwconfKeyword = os.Getenv("bwconf_keyword")
conf.BwKeyword = os.Getenv("bw_keyword")
conf.BwfKeyword = os.Getenv("bwf_keyword")
initModifiers()
}
func initModifiers() {
mod1 = getModifierKey(conf.Mod1)
mod1Emoji = getModifierEmoji(conf.Mod1)
mod2 = getModifierKey(conf.Mod2)
mod2Emoji = getModifierEmoji(conf.Mod2)
mod3 = getModifierKey(conf.Mod3)
mod3Emoji = getModifierEmoji(conf.Mod3)
mod4 = getModifierKey(conf.Mod4)
mod4Emoji = getModifierEmoji(conf.Mod4)
mod5 = getModifierKey(conf.Mod5)
mod5Emoji = getModifierEmoji(conf.Mod5)
}
func getModifierKey(keys string) []string {
items := strings.Split(keys, ",")
var collectKeys []string
for _, item := range items {
switch item {
case "cmd":
collectKeys = append(collectKeys, "cmd")
case "alt":
collectKeys = append(collectKeys, "alt")
case "fn":
collectKeys = append(collectKeys, "fn")
case "opt":
collectKeys = append(collectKeys, "alt")
case "ctrl":
collectKeys = append(collectKeys, "ctrl")
case "shift":
collectKeys = append(collectKeys, "shift")
}
}
return collectKeys
}
func getModifierEmoji(keys string) string {
items := strings.Split(keys, ",")
var emojiSlice []string
for _, item := range items {
switch item {
case "cmd":
emojiSlice = append(emojiSlice, "⌘")
case "alt":
emojiSlice = append(emojiSlice, "⌥")
case "fn":
emojiSlice = append(emojiSlice, "fn")
case "opt":
emojiSlice = append(emojiSlice, "⌥")
case "ctrl":
emojiSlice = append(emojiSlice, "ˆ")
case "shift":
emojiSlice = append(emojiSlice, "⇧")
}
}
emojiString := strings.Join(emojiSlice, "+")
return emojiString
}
func getTypeEmoji(itemType string) (string, error) {
modKeysMap := map[string]string{
conf.NoModAction: "",
conf.Mod1Action: mod1Emoji,
conf.Mod2Action: mod2Emoji,
conf.Mod3Action: mod3Emoji,
conf.Mod4Action: mod4Emoji,
conf.Mod5Action: mod5Emoji,
}
for keys, emoji := range modKeysMap {
splitKeys := strings.Split(keys, ",")
for _, key := range splitKeys {
key = strings.TrimSpace(key)
if key == itemType {
return emoji, nil
}
}
}
return "", fmt.Errorf("no matching key found for type: %s", itemType)
}
func getModifierActionRelations(itemModConfig itemsModifierActionRelationMap, item Item, itemType string, icon *aw.Icon, totp string, url string) {
modModes := map[string]string{
"nomod": conf.NoModAction,
"mod1": conf.Mod1Action,
"mod2": conf.Mod2Action,
"mod3": conf.Mod3Action,
"mod4": conf.Mod4Action,
"mod5": conf.Mod5Action,
}
for modMode, action := range modModes {
setModAction(itemModConfig, item, itemType, modMode, action, icon, totp, url)
}
}
func setModAction(itemConfig itemsModifierActionRelationMap, item Item, itemType string, modMode string, actionString string, icon *aw.Icon, totp string, url string) {
// get emojis assigned to the modification key
moreEmoji, err := getTypeEmoji("more")
if err != nil {
log.Fatal(err.Error())
}
codeEmoji, err := getTypeEmoji("code")
if err != nil {
log.Fatal(err.Error())
}
cardEmoji, err := getTypeEmoji("card")
if err != nil {
log.Fatal(err.Error())
}
passEmoji, err := getTypeEmoji("password")
if err != nil {
log.Fatal(err.Error())
}
userEmoji, err := getTypeEmoji("username")
if err != nil {
log.Fatal(err.Error())
}
webUiEmoji, err := getTypeEmoji("webui")
if err != nil {
log.Fatal(err.Error())
}
// urlEmoji, err := getTypeEmoji("url")
// if err != nil {
// log.Fatal(err.Error())
// }
splitActions := strings.Split(actionString, ",")
for _, action := range splitActions {
action = strings.TrimSpace(action)
if itemType == "item1" {
title := item.Name
if conf.TitleWithUser {
title = fmt.Sprintf("%s - %s", item.Name, item.Login.Username)
}
var urlList string
for _, url := range item.Login.Uris {
urlList = fmt.Sprintf("%s - %s", urlList, url.Uri)
}
if conf.TitleWithUrls {
title = fmt.Sprintf("%s - %s", title, urlList)
}
if action == "password" {
subtitle := "Copy password"
if modMode == "nomod" {
subtitle = fmt.Sprintf("↩ or ⇥ copy Password, %s %s, %s %s %s Show more, %s open in WebUI", userEmoji, item.Login.Username, totp, url, moreEmoji, webUiEmoji)
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: fmt.Sprintf("Copy Password for user:\n%s", item.Login.Username),
Action: "-getitem",
Action2: fmt.Sprintf("-id %s", item.Id),
Action3: " ",
Arg: "login.password",
Icon: icon,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
if action == "username" {
assignedIcon := iconUser
subtitle := "Copy Username"
if modMode == "nomod" {
assignedIcon = icon
subtitle = fmt.Sprintf("↩ or ⇥ copy Username, %s Password, %s %s %s Show more, %s open in WebUI", passEmoji, totp, url, moreEmoji, webUiEmoji)
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: fmt.Sprintf("Copy Username:\n%s", item.Login.Username),
Action: "output",
Action2: " ",
Action3: " ",
Arg: item.Login.Username,
Icon: assignedIcon,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
if action == "url" {
if len(item.Login.Uris) == 0 {
continue
}
assignedIcon := iconLink
subtitle := "Open URL"
loginUrlAction := "-open"
notification := " "
if !conf.OpenLoginUrl {
subtitle = "Copy URL"
loginUrlAction = "output"
notification = fmt.Sprintf("Copy url for user:\n%s", item.Login.Username)
}
if modMode == "nomod" {
assignedIcon = icon
subtitle = fmt.Sprintf("↩ or ⇥ copy URL, %s Password, %s Username %s %s Show more, %s open in WebUI", passEmoji, userEmoji, totp, moreEmoji, webUiEmoji)
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: notification,
Action: loginUrlAction,
Action2: " ",
Action3: " ",
Arg: item.Login.Uris[0].Uri,
Icon: assignedIcon,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
if action == "totp" {
if totp == "" {
continue
}
assignedIcon := iconUserClock
subtitle := "Copy TOTP"
if modMode == "nomod" {
assignedIcon = icon
subtitle = fmt.Sprintf("↩ or ⇥ copy TOTP, %s Password, %s Username %s %s Show more", passEmoji, userEmoji, url, moreEmoji)
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: fmt.Sprintf("Copy TOTP for user:\n%s", item.Login.Username),
Action: "-getitem",
Action2: "-totp",
Action3: fmt.Sprintf("-id %s", item.Id),
Arg: " ",
Icon: assignedIcon,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
}
if itemType == "item2" {
modItem := modifierActionContent{
Title: item.Name,
Subtitle: fmt.Sprintf("↩ or ⇥ copy Note, %s show more", moreEmoji),
Notification: "Copy Note",
Action: "-getitem",
Action2: fmt.Sprintf("-id %s", item.Id),
Action3: " ",
Arg: "notes",
Icon: iconNote,
ActionName: "",
}
setItemMod(itemConfig, modItem, itemType, "nomod")
}
if itemType == "item3" {
title := item.Name
if conf.TitleWithUser {
title = fmt.Sprintf("%s - %s", item.Name, item.Card.Number)
}
var urlList string
for _, url := range item.Login.Uris {
urlList = fmt.Sprintf("%s - %s", urlList, url.Uri)
}
if conf.TitleWithUrls {
title = fmt.Sprintf("%s - %s", title, urlList)
}
if action == "card" {
subtitle := "Copy Card Number"
if modMode == "nomod" {
subtitle = fmt.Sprintf("%s, %s, ↩ or ⇥ copy Card Number, %s copy Security Code, %s show more, %s open in WebUI", item.Card.Brand, item.Card.Number, codeEmoji, moreEmoji, webUiEmoji)
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: fmt.Sprintf("Copied Card %s:\n%s", item.Card.Brand, item.Card.Number),
Action: "-getitem",
Action2: fmt.Sprintf("-id %s", item.Id),
Action3: " ",
Arg: "card.number",
Icon: iconCreditCard,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
if action == "code" {
subtitle := "Copy card security code"
if modMode == "nomod" {
subtitle = fmt.Sprintf("%s, %s, ↩ or ⇥ copy Security Code, %s copy Card Number, %s show more, %s open in WebUI", item.Card.Brand, item.Card.Number, cardEmoji, moreEmoji, webUiEmoji)
}
assignedIcon := iconPassword
if modMode == "nomod" {
assignedIcon = iconCreditCard
}
modItem := modifierActionContent{
Title: title,
Subtitle: subtitle,
Notification: "Copied Card Security Code",
Action: "-getitem",
Action2: fmt.Sprintf("-id %s", item.Id),
Action3: " ",
Arg: "card.code",
Icon: assignedIcon,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
}
if itemType == "item4" {
modItem := modifierActionContent{
Title: item.Name,
Subtitle: fmt.Sprintf("↩ or ⇥ copy name %s %s, %s show more", item.Identity.FirstName, item.Identity.LastName, moreEmoji),
Notification: fmt.Sprintf("Copied Identity Name:\n%s %s", item.Identity.FirstName, item.Identity.LastName),
Action: "output",
Action2: " ",
Action3: " ",
Arg: " ",
Icon: iconIdBatch,
ActionName: "",
}
setItemMod(itemConfig, modItem, itemType, "nomod")
}
if action == "more" {
modItem := modifierActionContent{
Title: item.Name,
Subtitle: "Show item",
Notification: " ",
Action: fmt.Sprintf("-id %s", item.Id),
Action2: " ",
Action3: " ",
Arg: " ",
Icon: iconList,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
if action == "webui" {
webUi := "https://vault.bitwarden.com"
if len(conf.WebUiURL) > 0 {
webUi = conf.WebUiURL
}
subtitle := "Open Bitwarden WebUI"
if modMode == "nomod" {
subtitle = fmt.Sprintf("↩ or ⇥ open in Web UI, %s Password, %s Username %s %s Show more", passEmoji, userEmoji, totp, moreEmoji)
}
modItem := modifierActionContent{
Title: item.Name,
Subtitle: subtitle,
Notification: " ",
Action: "-open",
Action2: " ",
Action3: " ",
Arg: fmt.Sprintf("%s/#/vault?itemId=%s", webUi, item.Id),
Icon: iconBw,
ActionName: action,
}
setItemMod(itemConfig, modItem, itemType, modMode)
}
}
}
func setItemMod(itemConfig itemsModifierActionRelationMap, content modifierActionContent, itemType string, modMode string) {
switch modMode {
case "nomod":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: nil, Content: content}
case "mod1":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: mod1, Content: content}
case "mod2":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: mod2, Content: content}
case "mod3":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: mod3, Content: content}
case "mod4":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: mod4, Content: content}
case "mod5":
itemConfig[itemType][modMode] = modifierActionRelation{Keys: mod5, Content: content}
}
}