-
Notifications
You must be signed in to change notification settings - Fork 24
/
account.go
96 lines (87 loc) · 2.25 KB
/
account.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
// Copyright (c) 2016 Company 0, LLC.
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
package main
import (
"fmt"
"net"
"github.com/companyzero/zkc/rpc"
"github.com/davecgh/go-xdr/xdr2"
)
// replyAccountFailure marshals and sends a CreateAccountReply with
// Error set.
func (z *ZKS) accountReplyFailure(msg string, conn net.Conn,
ca rpc.CreateAccount) {
z.T(idApp, "accountReplyFailure: %v %v %v",
conn.RemoteAddr(),
msg,
ca.PublicIdentity.Fingerprint())
car := rpc.CreateAccountReply{
Error: rpc.ErrCreateDisallowed.Error(),
}
_, err := xdr.Marshal(conn, car)
if err != nil {
z.Error(idApp, "could not marshal CreateAccountReply")
return
}
}
func (z *ZKS) handleAccountCreate(conn net.Conn, ca rpc.CreateAccount) error {
z.T(idApp, "handleAccountCreate: %v %v",
conn.RemoteAddr(),
ca.PublicIdentity.Fingerprint())
// check policy
switch z.settings.CreatePolicy {
default:
fallthrough
case "no":
z.accountReplyFailure("disallowing account create", conn, ca)
return fmt.Errorf("disallowing account create")
case "token":
if !z.validToken(ca.Token, conn) {
z.accountReplyFailure("invalid account create token",
conn, ca)
return fmt.Errorf("invalid account create token")
}
case "yes":
}
// try to create account
err := z.account.Create(ca.PublicIdentity, false)
if err != nil {
z.Error(idApp, "%v could not create account: %v",
conn.RemoteAddr(),
err)
// fallthrough to answer
} else {
z.Info(idApp, "created account %v: %v",
conn.RemoteAddr(),
ca.PublicIdentity.Fingerprint())
}
// send reply
car := rpc.CreateAccountReply{}
if err != nil {
car.Error = rpc.ErrInternalError.Error()
}
_, err = xdr.Marshal(conn, car)
if err != nil {
return fmt.Errorf("could not marshal CreateAccountReply")
}
return nil
}
func (z *ZKS) handleIdentityFind(writer chan *RPCWrapper, msg rpc.Message, nick string) error {
reply := RPCWrapper{
Message: rpc.Message{
Command: rpc.TaggedCmdIdentityFindReply,
Tag: msg.Tag,
},
}
payload := new(rpc.IdentityFindReply)
id, err := z.account.Find(nick)
if err != nil {
payload.Error = fmt.Sprintf("%v", err) // XXX
} else {
payload.Identity = *id
}
reply.Payload = payload
writer <- &reply
return nil
}