|
@@ -4,8 +4,10 @@
|
4
|
4
|
package irc
|
5
|
5
|
|
6
|
6
|
import (
|
|
7
|
+ "encoding/json"
|
7
|
8
|
"errors"
|
8
|
9
|
"fmt"
|
|
10
|
+ "log"
|
9
|
11
|
"strconv"
|
10
|
12
|
"strings"
|
11
|
13
|
"time"
|
|
@@ -14,6 +16,12 @@ import (
|
14
|
16
|
"github.com/tidwall/buntdb"
|
15
|
17
|
)
|
16
|
18
|
|
|
19
|
+const (
|
|
20
|
+ keyAccountExists = "account %s exists"
|
|
21
|
+ keyAccountRegTime = "account %s registered.time"
|
|
22
|
+ keyAccountCredentials = "account %s credentials"
|
|
23
|
+)
|
|
24
|
+
|
17
|
25
|
var (
|
18
|
26
|
errAccountCreation = errors.New("Account could not be created")
|
19
|
27
|
)
|
|
@@ -25,6 +33,13 @@ type AccountRegistration struct {
|
25
|
33
|
EnabledCredentialTypes []string
|
26
|
34
|
}
|
27
|
35
|
|
|
36
|
+// AccountCredentials stores the various methods for verifying accounts.
|
|
37
|
+type AccountCredentials struct {
|
|
38
|
+ PassphraseSalt []byte
|
|
39
|
+ PassphraseHash []byte
|
|
40
|
+ Certificate string // fingerprint
|
|
41
|
+}
|
|
42
|
+
|
28
|
43
|
// NewAccountRegistration returns a new AccountRegistration, configured correctly.
|
29
|
44
|
func NewAccountRegistration(config AccountRegistrationConfig) (accountReg AccountRegistration) {
|
30
|
45
|
if config.Enabled {
|
|
@@ -60,6 +75,18 @@ func regHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
60
|
75
|
return false
|
61
|
76
|
}
|
62
|
77
|
|
|
78
|
+// removeFailedRegCreateData removes the data created by REG CREATE if the account creation fails early.
|
|
79
|
+func removeFailedRegCreateData(store buntdb.DB, account string) {
|
|
80
|
+ // error is ignored here, we can't do much about it anyways
|
|
81
|
+ store.Update(func(tx *buntdb.Tx) error {
|
|
82
|
+ tx.Delete(fmt.Sprintf(keyAccountExists, account))
|
|
83
|
+ tx.Delete(fmt.Sprintf(keyAccountRegTime, account))
|
|
84
|
+ tx.Delete(fmt.Sprintf(keyAccountCredentials, account))
|
|
85
|
+
|
|
86
|
+ return nil
|
|
87
|
+ })
|
|
88
|
+}
|
|
89
|
+
|
63
|
90
|
// regCreateHandler parses the REG CREATE command.
|
64
|
91
|
func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) bool {
|
65
|
92
|
client.Notice("Parsing CREATE")
|
|
@@ -75,7 +102,7 @@ func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) boo
|
75
|
102
|
// check whether account exists
|
76
|
103
|
// do it all in one write tx to prevent races
|
77
|
104
|
err := server.store.Update(func(tx *buntdb.Tx) error {
|
78
|
|
- accountKey := fmt.Sprintf("account %s exists", accountString)
|
|
105
|
+ accountKey := fmt.Sprintf(keyAccountExists, accountString)
|
79
|
106
|
|
80
|
107
|
_, err := tx.Get(accountKey)
|
81
|
108
|
if err != buntdb.ErrNotFound {
|
|
@@ -84,7 +111,7 @@ func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) boo
|
84
|
111
|
return errAccountCreation
|
85
|
112
|
}
|
86
|
113
|
|
87
|
|
- registeredTimeKey := fmt.Sprintf("account %s registered.time", accountString)
|
|
114
|
+ registeredTimeKey := fmt.Sprintf(keyAccountRegTime, accountString)
|
88
|
115
|
|
89
|
116
|
tx.Set(accountKey, "1", nil)
|
90
|
117
|
tx.Set(registeredTimeKey, strconv.FormatInt(time.Now().Unix(), 10), nil)
|
|
@@ -121,7 +148,7 @@ func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) boo
|
121
|
148
|
|
122
|
149
|
if !callbackValid {
|
123
|
150
|
client.Send(nil, server.nameString, ERR_REG_INVALID_CALLBACK, client.nickString, msg.Params[1], callbackNamespace, "Callback namespace is not supported")
|
124
|
|
- //TODO(dan): close out failed account reg (remove values from db)
|
|
151
|
+ removeFailedRegCreateData(server.store, accountString)
|
125
|
152
|
return false
|
126
|
153
|
}
|
127
|
154
|
|
|
@@ -136,7 +163,7 @@ func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) boo
|
136
|
163
|
credentialValue = msg.Params[3]
|
137
|
164
|
} else {
|
138
|
165
|
client.Send(nil, server.nameString, ERR_NEEDMOREPARAMS, client.nickString, msg.Command, "Not enough parameters")
|
139
|
|
- //TODO(dan): close out failed account reg (remove values from db)
|
|
166
|
+ removeFailedRegCreateData(server.store, accountString)
|
140
|
167
|
return false
|
141
|
168
|
}
|
142
|
169
|
|
|
@@ -147,22 +174,61 @@ func regCreateHandler(server *Server, client *Client, msg ircmsg.IrcMessage) boo
|
147
|
174
|
credentialValid = true
|
148
|
175
|
}
|
149
|
176
|
}
|
|
177
|
+ if credentialType == "certfp" && client.certfp == "" {
|
|
178
|
+ client.Send(nil, server.nameString, ERR_REG_INVALID_CRED_TYPE, client.nickString, credentialType, callbackNamespace, "You are not using a certificiate")
|
|
179
|
+ removeFailedRegCreateData(server.store, accountString)
|
|
180
|
+ return false
|
|
181
|
+ }
|
150
|
182
|
|
151
|
183
|
if !credentialValid {
|
152
|
184
|
client.Send(nil, server.nameString, ERR_REG_INVALID_CRED_TYPE, client.nickString, credentialType, callbackNamespace, "Credential type is not supported")
|
153
|
|
- //TODO(dan): close out failed account reg (remove values from db)
|
|
185
|
+ removeFailedRegCreateData(server.store, accountString)
|
154
|
186
|
return false
|
155
|
187
|
}
|
156
|
188
|
|
157
|
|
- // dispatch callback
|
|
189
|
+ // store details
|
|
190
|
+ err = server.store.Update(func(tx *buntdb.Tx) error {
|
|
191
|
+ var creds AccountCredentials
|
|
192
|
+
|
|
193
|
+ // always set passphrase salt
|
|
194
|
+ creds.PassphraseSalt, err = NewSalt()
|
|
195
|
+ if err != nil {
|
|
196
|
+ return fmt.Errorf("Could not create passphrase salt: %s", err.Error())
|
|
197
|
+ }
|
|
198
|
+
|
|
199
|
+ if credentialType == "certfp" {
|
|
200
|
+ creds.Certificate = client.certfp
|
|
201
|
+ } else if credentialType == "passphrase" {
|
|
202
|
+ creds.PassphraseHash, err = server.passwords.GenerateFromPassword(creds.PassphraseSalt, credentialValue)
|
|
203
|
+ if err != nil {
|
|
204
|
+ return fmt.Errorf("Could not hash password: %s", err)
|
|
205
|
+ }
|
|
206
|
+ }
|
|
207
|
+ credText, err := json.Marshal(creds)
|
|
208
|
+ if err != nil {
|
|
209
|
+ return fmt.Errorf("Could not marshal creds: %s", err)
|
|
210
|
+ }
|
|
211
|
+ tx.Set(keyAccountCredentials, string(credText), nil)
|
|
212
|
+
|
|
213
|
+ return nil
|
|
214
|
+ })
|
|
215
|
+
|
|
216
|
+ // details could not be stored and relevant numerics have been dispatched, abort
|
|
217
|
+ if err != nil {
|
|
218
|
+ client.Send(nil, server.nameString, ERR_UNKNOWNERROR, client.nickString, "REG", "CREATE", "Could not register")
|
|
219
|
+ log.Println("Could not save registration creds:", err.Error())
|
|
220
|
+ return false
|
|
221
|
+ }
|
|
222
|
+
|
|
223
|
+ // automatically complete registration
|
158
|
224
|
if callbackNamespace != "*" {
|
159
|
225
|
client.Notice("Account creation was successful!")
|
160
|
|
- //TODO(dan): close out failed account reg (remove values from db)
|
|
226
|
+ removeFailedRegCreateData(server.store, accountString)
|
161
|
227
|
return false
|
162
|
228
|
}
|
163
|
229
|
|
|
230
|
+ // dispatch callback
|
164
|
231
|
client.Notice(fmt.Sprintf("We should dispatch an actual callback here to %s:%s", callbackNamespace, callbackValue))
|
165
|
|
- client.Notice(fmt.Sprintf("Primary account credential is with %s:%s", credentialType, credentialValue))
|
166
|
232
|
|
167
|
233
|
return false
|
168
|
234
|
}
|