forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_delete.go
114 lines (95 loc) · 2.9 KB
/
account_delete.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
package serve
import (
"net/http"
"github.com/stellar/go/exp/services/recoverysigner/internal/account"
"github.com/stellar/go/exp/services/recoverysigner/internal/serve/auth"
"github.com/stellar/go/keypair"
"github.com/stellar/go/support/http/httpdecode"
supportlog "github.com/stellar/go/support/log"
"github.com/stellar/go/support/render/httpjson"
)
type accountDeleteHandler struct {
Logger *supportlog.Entry
SigningAddresses []*keypair.FromAddress
AccountStore account.Store
}
type accountDeleteRequest struct {
Address *keypair.FromAddress `path:"address"`
}
func (h accountDeleteHandler) 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 := accountDeleteRequest{}
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 delete account.")
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
}
signers := []accountResponseSigner{}
for _, signingAddress := range h.SigningAddresses {
signers = append(signers, accountResponseSigner{
Key: signingAddress.Address(),
})
}
resp := accountResponse{
Address: acc.Address,
Signer: h.SigningAddresses[0].Address(),
Signers: signers,
}
// 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 {
respIdentity := accountResponseIdentity{
Role: i.Role,
}
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)) {
respIdentity.Authenticated = true
authorized = true
l.Infof("Authorized with %s.", m.Type)
break
}
}
resp.Identities = append(resp.Identities, respIdentity)
}
l.Infof("Authorized: %v.", authorized)
if !authorized {
notFound.Render(w)
return
}
l.Info("Deleting account.")
err = h.AccountStore.Delete(req.Address.Address())
if err == account.ErrNotFound {
// It can happen if two authorized users are trying to delete the account at the same time.
l.Info("Account not found.")
notFound.Render(w)
return
} else if err != nil {
l.Error("Error deleting account:", err)
serverError.Render(w)
return
}
l.Info("Deleted account.")
httpjson.Render(w, resp, httpjson.JSON)
}