taldir

Directory service to resolve wallet mailboxes by messenger addresses
Log | Files | Refs | Submodules | README | LICENSE

commit c4fff82b71e613069ac7a36575dbb1cb5baa09f2
parent a26183b63343ca8bab5531390a3486bcb47ade65
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Mon, 11 Jul 2022 21:30:02 +0200

parse order id; return payment required if fee configured

Diffstat:
Mcmd/taldir-server/main_test.go | 11+++++++++++
Mcmd/taldir-server/testdata/taldir-test.conf | 9+++++++--
Mpkg/rest/taldir.go | 20++++++++++++++++----
Mpkg/util/helper.go | 15+++++++++++++++
4 files changed, 49 insertions(+), 6 deletions(-)

diff --git a/cmd/taldir-server/main_test.go b/cmd/taldir-server/main_test.go @@ -290,3 +290,14 @@ func TestUnsupportedMethod(s *testing.T) { s.Errorf("Expected response code %d. Got %d\n", http.StatusNotFound, response.Code) } } + +func TestPaymentRequiredMethod(s *testing.T) { + t.ClearDatabase() + + req, _ := http.NewRequest("POST", "/register/test-cost", bytes.NewBuffer(validRegisterRequest)) + response := executeRequest(req) + + if http.StatusPaymentRequired != response.Code { + s.Errorf("Expected response code %d. Got %d\n", http.StatusPaymentRequired, response.Code) + } +} diff --git a/cmd/taldir-server/testdata/taldir-test.conf b/cmd/taldir-server/testdata/taldir-test.conf @@ -1,17 +1,22 @@ [taldir] production = false -validators = "twitter test" +validators = "test-cost test" host = "https://taldir.net" bind_to = "localhost:11000" salt = "ChangeMe" -monthly_fee = KUDOS:1 +monthly_fee = KUDOS:0 request_frequency_microseconds = 10 validation_landing = testdata/templates/validation_landing.html [taldir-test] +challenge_fee = KUDOS:0 +command = testdata/taldir-validate-test + +[taldir-test-cost] challenge_fee = KUDOS:23 command = testdata/taldir-validate-test + [taldir-pq] host = "localhost" port = 5432 diff --git a/pkg/rest/taldir.go b/pkg/rest/taldir.go @@ -141,11 +141,11 @@ type RegisterMessage struct { // For how long should the registration last Duration int64 `json:"duration"` +} +type Order struct { // Order ID, if the client recently paid for this registration - // FIXME: As an optional field, maybe we want to parse this separately - // instead? - // Order_id string `json:"order_id"` + Id string `json:"order_id"` } // A mappind entry from the identity key hash to a wallet key @@ -357,6 +357,7 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ var errDetail ErrorDetail var validation Validation var entry Entry + var order Order if r.Body == nil { http.Error(w, "No request body", 400) return @@ -370,6 +371,8 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ w.Write(resp) return } + json.NewDecoder(r.Body).Decode(&order) + // Check if this validation method is supported or not. if !t.Validators[vars["method"]] { errDetail.Code = gana.TALDIR_METHOD_NOT_SUPPORTED @@ -380,7 +383,16 @@ func (t *Taldir) registerRequest(w http.ResponseWriter, r *http.Request){ w.Write(resp) return } - + if util.AmountIsNonZero(t.Cfg.Section("taldir-" + vars["method"]).Key("challenge_fee").MustString("KUDOS:0")) || + util.AmountIsNonZero(t.Cfg.Section("taldir").Key("monthly_fee").MustString("KUDOS:0")) { + if len(order.Id) == 0 { + w.WriteHeader(http.StatusPaymentRequired) + return + } + // FIXME process order_id + w.WriteHeader(http.StatusNotImplemented) + return + } // Setup validation object. Retrieve object from DB if it already // exists. h := sha512.New() diff --git a/pkg/util/helper.go b/pkg/util/helper.go @@ -23,6 +23,8 @@ import ( "fmt" "crypto/sha512" "math/rand" + "strings" + "strconv" ) @@ -48,3 +50,16 @@ func GenerateChallenge(bytes int) string { } return EncodeBinaryToString(randBytes) } + +// Check if this is a non-zero, positive amount +func AmountIsNonZero(amount string) bool { + s := strings.Split(amount, ":") + if len(s) != 2 { + return false + } + amountFloat, err := strconv.ParseFloat(s[1], 64) + if err != nil { + return false + } + return amountFloat > 0 +}