-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.go
More file actions
582 lines (477 loc) · 14.8 KB
/
app.go
File metadata and controls
582 lines (477 loc) · 14.8 KB
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
package main
import (
"context"
"errors"
"time"
"github.com/google/uuid"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
vault *Vault
masterKey []byte
storage *StorageManager
ipcServer *IPCServer
isUnlocked bool
passwordHash string
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
storage, err := NewStorageManager()
if err != nil {
panic(err)
}
a.storage = storage
// Start IPC server for browser extension
a.ipcServer = NewIPCServer(a)
if err := a.ipcServer.Start(); err != nil {
// Log error but don't crash - extension won't work but app will
println("Warning: Failed to start IPC server for browser extension:", err.Error())
} else {
println("IPC server started - browser extension ready")
}
}
// domReady is called after front-end resources have been loaded
func (a *App) domReady(ctx context.Context) {
// Add your action here
}
// beforeClose is called when the application is about to quit
func (a *App) beforeClose(ctx context.Context) (prevent bool) {
return false
}
// shutdown is called at application termination
func (a *App) shutdown(ctx context.Context) {
// Stop IPC server
if a.ipcServer != nil {
a.ipcServer.Stop()
println("IPC server stopped")
}
}
// CheckVaultExists checks if a vault file already exists
func (a *App) CheckVaultExists() bool {
return a.storage.VaultExists()
}
// CreateVault initializes a new vault with a master password
func (a *App) CreateVault(masterPassword string) error {
if a.storage.VaultExists() {
return errors.New("vault already exists")
}
// Generate salt
salt, err := GenerateSalt()
if err != nil {
return err
}
// Derive master key
a.masterKey = DeriveKey(masterPassword, salt)
a.passwordHash = HashPassword(masterPassword)
// Create empty vault
a.vault = &Vault{
Credentials: []Credential{},
CreditCards: []CreditCard{},
Salt: salt,
}
// Save vault
if err := a.storage.SaveVault(a.vault, a.masterKey); err != nil {
return err
}
a.isUnlocked = true
return nil
}
// UnlockVault unlocks an existing vault with the master password
func (a *App) UnlockVault(masterPassword string) error {
if !a.storage.VaultExists() {
return errors.New("vault does not exist")
}
// Load the salt first (stored separately, unencrypted)
salt, err := a.storage.LoadSalt()
if err != nil {
return errors.New("vault corrupted: salt not found")
}
// Derive the master key using the password and salt
a.masterKey = DeriveKey(masterPassword, salt)
// Now load and decrypt the vault with the derived key
vault, err := a.storage.LoadVault(a.masterKey, salt)
if err != nil {
return err
}
a.vault = vault
a.passwordHash = HashPassword(masterPassword)
a.isUnlocked = true
return nil
}
// IsUnlocked checks if the vault is currently unlocked
func (a *App) IsUnlocked() bool {
return a.isUnlocked
}
// ChangeMasterPassword changes the master password and re-encrypts the vault
func (a *App) ChangeMasterPassword(currentPassword, newPassword string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
// Verify current password
if !VerifyPassword(currentPassword, a.passwordHash) {
return errors.New("current password is incorrect")
}
// Validate new password
if len(newPassword) < 8 {
return errors.New("new password must be at least 8 characters")
}
// Generate new salt
newSalt, err := GenerateSalt()
if err != nil {
return errors.New("failed to generate new salt")
}
// Derive new master key
newMasterKey := DeriveKey(newPassword, newSalt)
// Update vault salt
a.vault.Salt = newSalt
// Save vault with new key
if err := a.storage.SaveVault(a.vault, newMasterKey); err != nil {
return errors.New("failed to save vault with new password")
}
// Update in-memory references
a.masterKey = newMasterKey
a.passwordHash = HashPassword(newPassword)
return nil
}
// GetAllCredentials returns all credentials from the vault
func (a *App) GetAllCredentials() ([]Credential, error) {
if !a.isUnlocked {
return nil, errors.New("vault is locked")
}
return a.vault.Credentials, nil
}
// AddCredential adds a new credential to the vault
func (a *App) AddCredential(serviceName, urlStr, username, password, category string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
credential := Credential{
ID: uuid.New().String(),
ServiceName: serviceName,
URL: urlStr,
Username: username,
Password: password,
Category: category,
IconURL: FetchFavicon(urlStr),
CreatedAt: time.Now(),
}
a.vault.Credentials = append(a.vault.Credentials, credential)
// Save vault
err := a.storage.SaveVault(a.vault, a.masterKey)
if err != nil {
return err
}
// Emit event to notify frontend
runtime.EventsEmit(a.ctx, "credentials-updated")
return nil
}
// UpdateCredential updates an existing credential
func (a *App) UpdateCredential(id, serviceName, urlStr, username, password, category string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, cred := range a.vault.Credentials {
if cred.ID == id {
a.vault.Credentials[i].ServiceName = serviceName
a.vault.Credentials[i].URL = urlStr
a.vault.Credentials[i].Username = username
a.vault.Credentials[i].Password = password
a.vault.Credentials[i].Category = category
a.vault.Credentials[i].IconURL = FetchFavicon(urlStr)
return a.storage.SaveVault(a.vault, a.masterKey)
}
}
return errors.New("credential not found")
}
// DeleteCredential removes a credential from the vault
func (a *App) DeleteCredential(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, cred := range a.vault.Credentials {
if cred.ID == id {
a.vault.Credentials = append(a.vault.Credentials[:i], a.vault.Credentials[i+1:]...)
return a.storage.SaveVault(a.vault, a.masterKey)
}
}
return errors.New("credential not found")
}
// ToggleFavorite toggles the favorite status of a credential
func (a *App) ToggleFavorite(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, cred := range a.vault.Credentials {
if cred.ID == id {
a.vault.Credentials[i].IsFavorite = !a.vault.Credentials[i].IsFavorite
if err := a.storage.SaveVault(a.vault, a.masterKey); err != nil {
return err
}
runtime.EventsEmit(a.ctx, "credentials-updated")
return nil
}
}
return errors.New("credential not found")
}
// CopyPassword copies a password to clipboard with auto-clear
func (a *App) CopyPassword(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for _, cred := range a.vault.Credentials {
if cred.ID == id {
return ClipboardCopy(cred.Password)
}
}
return errors.New("credential not found")
}
// CopyUsername copies a username to clipboard with auto-clear
func (a *App) CopyUsername(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for _, cred := range a.vault.Credentials {
if cred.ID == id {
return ClipboardCopy(cred.Username)
}
}
return errors.New("credential not found")
}
// GeneratePasswordWithOptions generates a password with custom options
func (a *App) GeneratePasswordWithOptions(options PasswordGeneratorOptions) (string, error) {
return GeneratePassword(options)
}
// GenerateQuickPassword generates a strong password with default settings
func (a *App) GenerateQuickPassword(length int) (string, error) {
if length < 8 {
length = 16
}
return GenerateStrongPassword(length)
}
// ============ Credit Card Methods ============
// GetAllCreditCards returns all credit cards from the vault
func (a *App) GetAllCreditCards() ([]CreditCard, error) {
if !a.isUnlocked {
return nil, errors.New("vault is locked")
}
return a.vault.CreditCards, nil
}
// AddCreditCard adds a new credit card to the vault
func (a *App) AddCreditCard(cardName, cardholderName, cardNumber, expiryMonth, expiryYear, cvv, cardType, billingZip string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
card := CreditCard{
ID: uuid.New().String(),
CardName: cardName,
CardholderName: cardholderName,
CardNumber: cardNumber,
ExpiryMonth: expiryMonth,
ExpiryYear: expiryYear,
CVV: cvv,
CardType: cardType,
BillingZip: billingZip,
CreatedAt: time.Now(),
}
a.vault.CreditCards = append(a.vault.CreditCards, card)
// Save vault
err := a.storage.SaveVault(a.vault, a.masterKey)
if err != nil {
return err
}
// Emit event to notify frontend
runtime.EventsEmit(a.ctx, "creditcards-updated")
return nil
}
// UpdateCreditCard updates an existing credit card
func (a *App) UpdateCreditCard(id, cardName, cardholderName, cardNumber, expiryMonth, expiryYear, cvv, cardType, billingZip string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, card := range a.vault.CreditCards {
if card.ID == id {
a.vault.CreditCards[i].CardName = cardName
a.vault.CreditCards[i].CardholderName = cardholderName
a.vault.CreditCards[i].CardNumber = cardNumber
a.vault.CreditCards[i].ExpiryMonth = expiryMonth
a.vault.CreditCards[i].ExpiryYear = expiryYear
a.vault.CreditCards[i].CVV = cvv
a.vault.CreditCards[i].CardType = cardType
a.vault.CreditCards[i].BillingZip = billingZip
return a.storage.SaveVault(a.vault, a.masterKey)
}
}
return errors.New("credit card not found")
}
// DeleteCreditCard removes a credit card from the vault
func (a *App) DeleteCreditCard(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, card := range a.vault.CreditCards {
if card.ID == id {
a.vault.CreditCards = append(a.vault.CreditCards[:i], a.vault.CreditCards[i+1:]...)
return a.storage.SaveVault(a.vault, a.masterKey)
}
}
return errors.New("credit card not found")
}
// ToggleCreditCardFavorite toggles the favorite status of a credit card
func (a *App) ToggleCreditCardFavorite(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for i, card := range a.vault.CreditCards {
if card.ID == id {
a.vault.CreditCards[i].IsFavorite = !a.vault.CreditCards[i].IsFavorite
if err := a.storage.SaveVault(a.vault, a.masterKey); err != nil {
return err
}
runtime.EventsEmit(a.ctx, "creditcards-updated")
return nil
}
}
return errors.New("credit card not found")
}
// CopyCardNumber copies a card number to clipboard with auto-clear
func (a *App) CopyCardNumber(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for _, card := range a.vault.CreditCards {
if card.ID == id {
return ClipboardCopy(card.CardNumber)
}
}
return errors.New("credit card not found")
}
// CopyCVV copies a CVV to clipboard with auto-clear
func (a *App) CopyCVV(id string) error {
if !a.isUnlocked {
return errors.New("vault is locked")
}
for _, card := range a.vault.CreditCards {
if card.ID == id {
return ClipboardCopy(card.CVV)
}
}
return errors.New("credit card not found")
}
// LockVault locks the vault and clears sensitive data from memory
func (a *App) LockVault() {
a.isUnlocked = false
a.masterKey = nil
a.vault = nil
}
// DeleteVault permanently deletes the vault (use with caution!)
func (a *App) DeleteVault() error {
// Lock first
a.LockVault()
// Delete vault files
return a.storage.DeleteVault()
}
// ExportToCSV exports all credentials to a CSV file
func (a *App) ExportToCSV() (string, error) {
if !a.isUnlocked {
return "", errors.New("vault is locked")
}
// Let user choose where to save
filePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
DefaultFilename: "vaultzero-export.csv",
Title: "Export Credentials to CSV",
Filters: []runtime.FileFilter{
{DisplayName: "CSV Files (*.csv)", Pattern: "*.csv"},
},
})
if err != nil || filePath == "" {
return "", errors.New("export cancelled")
}
// Export to CSV
err = ExportCredentialsToCSV(a.vault.Credentials, filePath)
if err != nil {
return "", err
}
return filePath, nil
}
// ExportEncryptedBackup creates an encrypted backup of the entire vault
func (a *App) ExportEncryptedBackup() (string, error) {
if !a.isUnlocked {
return "", errors.New("vault is locked")
}
// Let user choose where to save
filePath, err := runtime.SaveFileDialog(a.ctx, runtime.SaveDialogOptions{
DefaultFilename: "vaultzero-backup.vault",
Title: "Export Encrypted Backup",
Filters: []runtime.FileFilter{
{DisplayName: "Vault Backup (*.vault)", Pattern: "*.vault"},
},
})
if err != nil || filePath == "" {
return "", errors.New("export cancelled")
}
// Create encrypted backup
err = a.storage.ExportEncryptedBackup(a.vault, a.masterKey, filePath)
if err != nil {
return "", err
}
return filePath, nil
}
// ImportEncryptedBackup imports credentials from an encrypted backup file
func (a *App) ImportEncryptedBackup() (*ImportResult, error) {
if !a.isUnlocked {
return nil, errors.New("vault is locked")
}
// Let user choose backup file
filePath, err := runtime.OpenFileDialog(a.ctx, runtime.OpenDialogOptions{
Title: "Import Encrypted Backup",
Filters: []runtime.FileFilter{
{DisplayName: "Vault Backup (*.vault)", Pattern: "*.vault"},
},
})
if err != nil || filePath == "" {
return nil, errors.New("import cancelled")
}
// Load and decrypt backup
credentials, err := a.storage.ImportEncryptedBackup(filePath, a.masterKey)
if err != nil {
return nil, err
}
// Import credentials
result := &ImportResult{
TotalProcessed: len(credentials),
Errors: []string{},
}
for _, cred := range credentials {
// Check if credential already exists (by URL + username)
exists := false
for _, existingCred := range a.vault.Credentials {
if existingCred.URL == cred.URL && existingCred.Username == cred.Username {
exists = true
break
}
}
if exists {
result.Skipped++
continue
}
// Add to vault
a.vault.Credentials = append(a.vault.Credentials, cred)
result.Imported++
}
// Save vault if any credentials were imported
if result.Imported > 0 {
if err := a.storage.SaveVault(a.vault, a.masterKey); err != nil {
return nil, err
}
// Emit event to notify frontend
runtime.EventsEmit(a.ctx, "credentials-updated")
}
return result, nil
}