-
Notifications
You must be signed in to change notification settings - Fork 16
/
receiver_registration.go
103 lines (90 loc) · 3.2 KB
/
receiver_registration.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
package httphandler
import (
"errors"
"fmt"
"net/http"
"github.com/stellar/go/support/log"
"github.com/stellar/stellar-disbursement-platform-backend/internal/anchorplatform"
"github.com/stellar/stellar-disbursement-platform-backend/internal/data"
htmlTpl "github.com/stellar/stellar-disbursement-platform-backend/internal/htmltemplate"
"github.com/stellar/stellar-disbursement-platform-backend/internal/serve/httperror"
)
type ReceiverRegistrationHandler struct {
Models *data.Models
ReceiverWalletModel *data.ReceiverWalletModel
ReCAPTCHASiteKey string
}
type ReceiverRegistrationData struct {
StellarAccount string
JWTToken string
Title string
Message string
ReCAPTCHASiteKey string
PrivacyPolicyLink string
OrganizationName string
}
// ServeHTTP will serve the SEP-24 deposit page needed to register users.
func (h ReceiverRegistrationHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
sep24Claims := anchorplatform.GetSEP24Claims(ctx)
if sep24Claims == nil {
err := fmt.Errorf("no SEP-24 claims found in the request context")
log.Ctx(ctx).Error(err)
httperror.Unauthorized("", err, nil).Render(w)
return
}
token := r.URL.Query().Get("token")
if token == "" {
err := fmt.Errorf("no token was provided in the request")
log.Ctx(ctx).Error(err)
httperror.Unauthorized("", err, nil).Render(w)
return
}
err := sep24Claims.Valid()
if err != nil {
err = fmt.Errorf("SEP-24 claims are invalid: %w", err)
log.Ctx(ctx).Error(err)
httperror.Unauthorized("", err, nil).Render(w)
return
}
rw, err := h.ReceiverWalletModel.GetByStellarAccountAndMemo(ctx, sep24Claims.SEP10StellarAccount(), sep24Claims.SEP10StellarMemo(), sep24Claims.ClientDomain())
if err != nil && !errors.Is(err, data.ErrRecordNotFound) {
httperror.InternalError(ctx, "Cannot register receiver wallet", err, nil).Render(w)
return
}
organization, err := h.Models.Organizations.Get(ctx)
if err != nil {
httperror.InternalError(ctx, "Cannot get organization", err, nil).Render(w)
return
}
privacyPolicyLink := ""
if organization.PrivacyPolicyLink != nil {
privacyPolicyLink = *organization.PrivacyPolicyLink
}
tmplData := ReceiverRegistrationData{
StellarAccount: sep24Claims.SEP10StellarAccount(),
JWTToken: token,
ReCAPTCHASiteKey: h.ReCAPTCHASiteKey,
PrivacyPolicyLink: privacyPolicyLink,
OrganizationName: organization.Name,
}
htmlTemplateName := "receiver_register.tmpl"
if rw != nil {
// If the user was previously registered successfully, load a different template.
htmlTemplateName = "receiver_registered_successfully.tmpl"
tmplData.Title = "Registration Complete 🎉"
tmplData.Message = "Your Stellar wallet has been registered successfully!"
}
registerPage, err := htmlTpl.ExecuteHTMLTemplate(htmlTemplateName, tmplData)
if err != nil {
httperror.InternalError(ctx, "Cannot process the html template for request", err, nil).Render(w)
return
}
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
_, err = w.Write([]byte(registerPage))
if err != nil {
httperror.InternalError(ctx, "Cannot write html content to response", err, nil).Render(w)
return
}
}