forked from stellar/go
/
account_put.go
183 lines (160 loc) · 4.81 KB
/
account_put.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
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
package serve
import (
"net/http"
"github.com/aiblocks/go/exp/services/recoverysigner/internal/account"
"github.com/aiblocks/go/exp/services/recoverysigner/internal/serve/auth"
"github.com/aiblocks/go/keypair"
"github.com/aiblocks/go/support/errors"
"github.com/aiblocks/go/support/http/httpdecode"
supportlog "github.com/aiblocks/go/support/log"
"github.com/aiblocks/go/support/render/httpjson"
)
type accountPutHandler struct {
Logger *supportlog.Entry
SigningAddresses []*keypair.FromAddress
AccountStore account.Store
}
type accountPutRequest struct {
Address *keypair.FromAddress `path:"address"`
Identities []accountPutRequestIdentity `json:"identities" form:"identities"`
}
func (r accountPutRequest) Validate() error {
if len(r.Identities) == 0 {
return errors.Errorf("no identities provided but at least one is required")
}
for _, i := range r.Identities {
err := i.Validate()
if err != nil {
return err
}
}
return nil
}
type accountPutRequestIdentity struct {
Role string `json:"role" form:"role"`
AuthMethods []accountPutRequestIdentityAuthMethod `json:"auth_methods" form:"auth_methods"`
}
func (i accountPutRequestIdentity) Validate() error {
if i.Role == "" {
return errors.Errorf("role is not set but required")
}
if len(i.AuthMethods) == 0 {
return errors.Errorf("auth methods not provided for identity but required")
}
for _, am := range i.AuthMethods {
err := am.Validate()
if err != nil {
return err
}
}
return nil
}
type accountPutRequestIdentityAuthMethod struct {
Type string `json:"type" form:"type"`
Value string `json:"value" form:"value"`
}
func (am accountPutRequestIdentityAuthMethod) Validate() error {
if !account.AuthMethodType(am.Type).Valid() {
return errors.Errorf("auth method type %q unrecognized", am.Type)
}
// TODO: Validate auth method values: AiBlocks address, phone number and email.
return nil
}
func (h accountPutHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
claims, _ := auth.FromContext(ctx)
if claims.Address == "" && claims.PhoneNumber == "" && claims.Email == "" {
unauthorized.Render(w)
return
}
req := accountPutRequest{}
err := httpdecode.Decode(r, &req)
if err != nil || req.Address == nil {
badRequest.Render(w)
return
}
l := h.Logger.Ctx(ctx).
WithField("account", req.Address.Address())
l.Info("Request to update account.")
if req.Validate() != nil {
l.Info("Request validation failed.")
badRequest.Render(w)
return
}
acc, err := h.AccountStore.Get(req.Address.Address())
if err == account.ErrNotFound {
l.Info("Account not found.")
notFound.Render(w)
return
} else if err != nil {
l.Error(err)
serverError.Render(w)
return
}
// Authorized if authenticated as the account.
authorized := claims.Address == req.Address.Address()
l.Infof("Authorized with self: %v.", authorized)
// Authorized if authenticated as an identity registered with the account.
for _, i := range acc.Identities {
for _, m := range i.AuthMethods {
if m.Value != "" && ((m.Type == account.AuthMethodTypeAddress && m.Value == claims.Address) ||
(m.Type == account.AuthMethodTypePhoneNumber && m.Value == claims.PhoneNumber) ||
(m.Type == account.AuthMethodTypeEmail && m.Value == claims.Email)) {
authorized = true
l.Infof("Authorized with %s.", m.Type)
break
}
}
}
if !authorized {
notFound.Render(w)
return
}
authMethodCount := 0
accWithNewIdentiies := account.Account{
Address: req.Address.Address(),
Identities: []account.Identity{},
}
for _, i := range req.Identities {
accIdentity := account.Identity{
Role: i.Role,
}
for _, m := range i.AuthMethods {
accIdentity.AuthMethods = append(accIdentity.AuthMethods, account.AuthMethod{
Type: account.AuthMethodType(m.Type),
Value: m.Value,
})
authMethodCount++
}
accWithNewIdentiies.Identities = append(accWithNewIdentiies.Identities, accIdentity)
}
l = l.
WithField("identities_count", len(accWithNewIdentiies.Identities)).
WithField("auth_methods_count", authMethodCount)
err = h.AccountStore.Update(accWithNewIdentiies)
if err == account.ErrNotFound {
// It can happen if another authorized user is trying to delete the account at the same time.
l.Info("Account not found.")
notFound.Render(w)
return
} else if err != nil {
h.Logger.Error(err)
serverError.Render(w)
return
}
l.Info("Account updated.")
resp := accountResponse{
Address: accWithNewIdentiies.Address,
}
for _, signingAddress := range h.SigningAddresses {
resp.Signers = append(resp.Signers, accountResponseSigner{
Key: signingAddress.Address(),
})
}
for _, i := range accWithNewIdentiies.Identities {
resp.Identities = append(resp.Identities, accountResponseIdentity{
Role: i.Role,
})
}
httpjson.Render(w, resp, httpjson.JSON)
}