forked from coyim/coyim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
master_password.go
146 lines (120 loc) · 3.66 KB
/
master_password.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
package gui
import (
"github.com/twstrike/coyim/Godeps/_workspace/src/github.com/twstrike/gotk3adapter/gtki"
"github.com/twstrike/coyim/config"
"github.com/twstrike/coyim/i18n"
)
func (u *gtkUI) captureInitialMasterPassword(k func()) {
dialogID := "CaptureInitialMasterPassword"
builder := builderForDefinition(dialogID)
dialogOb, _ := builder.GetObject(dialogID)
pwdDialog := dialogOb.(gtki.Dialog)
passObj, _ := builder.GetObject("password")
password := passObj.(gtki.Entry)
pass2Obj, _ := builder.GetObject("password2")
password2 := pass2Obj.(gtki.Entry)
msgObj, _ := builder.GetObject("passMessage")
messageObj := msgObj.(gtki.Label)
messageObj.SetSelectable(true)
builder.ConnectSignals(map[string]interface{}{
"on_save_signal": func() {
passText1, _ := password.GetText()
passText2, _ := password2.GetText()
if len(passText1) == 0 {
messageObj.SetLabel(i18n.Local("Password can not be empty - please try again"))
password.GrabFocus()
} else if passText1 != passText2 {
messageObj.SetLabel(i18n.Local("Passwords have to be the same - please try again"))
password.GrabFocus()
} else {
u.keySupplier = &onetimeSavedPassword{
savedPassword: passText1,
realF: u.keySupplier,
}
pwdDialog.Destroy()
k()
}
},
"on_cancel_signal": func() {
pwdDialog.Destroy()
},
})
doInUIThread(func() {
pwdDialog.SetTransientFor(u.window)
pwdDialog.ShowAll()
})
}
func (u *gtkUI) wouldYouLikeToEncryptYourFile(k func(bool)) {
dialogID := "AskToEncrypt"
builder := builderForDefinition(dialogID)
dialogOb, _ := builder.GetObject(dialogID)
encryptDialog := dialogOb.(gtki.MessageDialog)
encryptDialog.SetDefaultResponse(gtki.RESPONSE_YES)
encryptDialog.SetTransientFor(u.window)
responseType := gtki.ResponseType(encryptDialog.Run())
result := responseType == gtki.RESPONSE_YES
encryptDialog.Destroy()
k(result)
}
type onetimeSavedPassword struct {
savedPassword string
realF config.KeySupplier
}
func (o *onetimeSavedPassword) Invalidate() {
o.realF.Invalidate()
}
func (o *onetimeSavedPassword) LastAttemptFailed() {
o.realF.LastAttemptFailed()
}
func (o *onetimeSavedPassword) GenerateKey(params config.EncryptionParameters) ([]byte, []byte, bool) {
if o.savedPassword != "" {
ourPwd := o.savedPassword
o.savedPassword = ""
l, r := config.GenerateKeys(ourPwd, params)
return l, r, true
}
return o.realF.GenerateKey(params)
}
func (u *gtkUI) getMasterPassword(params config.EncryptionParameters, lastAttemptFailed bool) ([]byte, []byte, bool) {
dialogID := "MasterPassword"
pwdResultChan := make(chan string)
var cleanup func()
doInUIThread(func() {
builder := builderForDefinition(dialogID)
dialogOb, _ := builder.GetObject(dialogID)
dialog := dialogOb.(gtki.Dialog)
cleanup = dialog.Destroy
passObj, _ := builder.GetObject("password")
password := passObj.(gtki.Entry)
msgObj, _ := builder.GetObject("passMessage")
messageObj := msgObj.(gtki.Label)
messageObj.SetSelectable(true)
if lastAttemptFailed {
messageObj.SetLabel(i18n.Local("Incorrect password entered, please try again."))
}
builder.ConnectSignals(map[string]interface{}{
"on_save_signal": func() {
passText, _ := password.GetText()
if len(passText) > 0 {
messageObj.SetLabel(i18n.Local("Checking password..."))
pwdResultChan <- passText
close(pwdResultChan)
}
},
"on_cancel_signal": func() {
close(pwdResultChan)
u.quit()
},
})
dialog.SetTransientFor(u.window)
dialog.ShowAll()
})
pwd, ok := <-pwdResultChan
if !ok {
doInUIThread(cleanup)
return nil, nil, false
}
l, r := config.GenerateKeys(pwd, params)
doInUIThread(cleanup)
return l, r, true
}