-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.go
147 lines (115 loc) · 2.97 KB
/
app.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
package main
import (
"context"
"fmt"
"log"
"time"
"github.com/sammy-t/avdu"
"github.com/sammy-t/avdu/vault"
"github.com/wailsapp/wails/v2/pkg/runtime"
)
// App struct
type App struct {
ctx context.Context
vaultData *vault.Vault
}
// NewApp creates a new App application struct
func NewApp() *App {
return &App{}
}
// startup is called when the app starts. The context is saved
// so we can call the runtime methods
func (a *App) startup(ctx context.Context) {
a.ctx = ctx
}
// GetAppInfo returns the info from the app's config file.
func (a *App) GetAppInfo() Response {
return Response{
Status: "success",
Data: appInfo,
}
}
// SelectVault opens a file selection dialog
// and returns the selected file's path.
func (a *App) SelectVault() Response {
filters := []runtime.FileFilter{
{DisplayName: "Vault Files (*.json)", Pattern: "*.json"},
}
options := runtime.OpenDialogOptions{
Title: "Select vault file",
Filters: filters,
}
filePath, err := runtime.OpenFileDialog(a.ctx, options)
response := Response{}
if err != nil {
log.Printf("unable to open file: %v\n", err)
response.Status = "error"
response.Message = err.Error()
} else {
response.Status = "success"
response.Data = filePath
}
return response
}
// OpenVault reads the vault file at the provided path,
// sets the vault data on the app struct,
// starts OTP watching in a goroutine,
// and returns whether the read was successful.
func (a *App) OpenVault(filePath string, password string) Response {
var vaultData *vault.Vault
var err error
if password == "" {
vaultData, err = avdu.ReadVaultFile(filePath)
} else {
vaultData, err = avdu.ReadAndDecryptVaultFile(filePath, password)
}
var response Response
if err != nil {
errMsg := fmt.Sprintf("unable to open vault at %q: %v", filePath, err)
log.Println(errMsg)
response.Status = "error"
response.Message = errMsg
} else {
response.Status = "success"
a.vaultData = vaultData
go a.watchOTPs()
}
return response
}
// CloseVault removes the vault data.
func (a *App) CloseVault() {
a.vaultData = nil
}
// watchOTPs loops through updating the OTPs
// until the current vault data is removed.
//
// NOTE: This is intended to be called as a goroutine.
func (a *App) watchOTPs() {
a.updateOTPs()
for a.vaultData != nil {
ttn := avdu.GetTTN()
if ttn > 29000 {
a.updateOTPs()
}
runtime.EventsEmit(a.ctx, "onTimeUpdated", ttn)
time.Sleep(1000 * time.Millisecond)
}
}
// updateOTPs loads the OTPs for the current vault data
// and emits an "onCodesUpdated" event containing
// an array of each entry with its current OTP.
func (a *App) updateOTPs() {
otps, err := avdu.GetOTPs(a.vaultData)
if err != nil {
log.Println(err)
}
var entryCodes []EntryCode
for _, entry := range a.vaultData.Db.Entries {
entryCode := EntryCode{
Entry: entry,
Code: fmt.Sprintf("%v", otps[entry.Uuid]),
}
entryCodes = append(entryCodes, entryCode)
}
runtime.EventsEmit(a.ctx, "onCodesUpdated", entryCodes)
}