forked from stellar/go
/
challenge.go
117 lines (103 loc) · 2.71 KB
/
challenge.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
package serve
import (
"net/http"
"strconv"
"strings"
"time"
"github.com/pownieh/stellar_go/keypair"
"github.com/pownieh/stellar_go/strkey"
supportlog "github.com/pownieh/stellar_go/support/log"
"github.com/pownieh/stellar_go/support/render/httpjson"
"github.com/pownieh/stellar_go/txnbuild"
)
// ChallengeHandler implements the SEP-10 challenge endpoint and handles
// requests for a new challenge transaction.
type challengeHandler struct {
Logger *supportlog.Entry
NetworkPassphrase string
SigningKey *keypair.Full
ChallengeExpiresIn time.Duration
Domain string
HomeDomains []string
}
type challengeResponse struct {
Transaction string `json:"transaction"`
NetworkPassphrase string `json:"network_passphrase"`
}
func (h challengeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
queryValues := r.URL.Query()
account := queryValues.Get("account")
isStellarAccount := strkey.IsValidEd25519PublicKey(account)
isMuxedAccount := strkey.IsValidMuxedAccountEd25519PublicKey(account)
if !isStellarAccount && !isMuxedAccount {
badRequest.Render(w)
return
}
homeDomain := queryValues.Get("home_domain")
if homeDomain != "" {
// In some cases the full stop (period) character is used at the end of a FQDN.
homeDomain = strings.TrimSuffix(homeDomain, ".")
matched := false
for _, supportedDomain := range h.HomeDomains {
if homeDomain == supportedDomain {
matched = true
break
}
}
if !matched {
badRequest.Render(w)
return
}
} else {
homeDomain = h.HomeDomains[0]
}
var memo *txnbuild.MemoID
memoParam := queryValues.Get("memo")
if memoParam != "" {
memoInt, err := strconv.ParseUint(memoParam, 10, 64)
if err != nil {
badRequest.Render(w)
return
}
memoId := txnbuild.MemoID(memoInt)
memo = &memoId
}
tx, err := txnbuild.BuildChallengeTx(
h.SigningKey.Seed(),
account,
h.Domain,
homeDomain,
h.NetworkPassphrase,
h.ChallengeExpiresIn,
memo,
)
if err != nil {
h.Logger.Ctx(ctx).WithStack(err).Error(err)
badRequest.Render(w)
return
}
hash, err := tx.HashHex(h.NetworkPassphrase)
if err != nil {
h.Logger.Ctx(ctx).WithStack(err).Error(err)
serverError.Render(w)
return
}
l := h.Logger.Ctx(ctx).
WithField("tx", hash).
WithField("account", account).
WithField("serversigner", h.SigningKey.Address()).
WithField("homedomain", homeDomain)
l.Info("Generated challenge transaction for account.")
txeBase64, err := tx.Base64()
if err != nil {
h.Logger.Ctx(ctx).WithStack(err).Error(err)
serverError.Render(w)
return
}
res := challengeResponse{
Transaction: txeBase64,
NetworkPassphrase: h.NetworkPassphrase,
}
httpjson.Render(w, res, httpjson.JSON)
}