gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[taler-taldir] branch master updated: parse order id; return payment req


From: gnunet
Subject: [taler-taldir] branch master updated: parse order id; return payment required if fee configured
Date: Mon, 11 Jul 2022 21:30:04 +0200

This is an automated email from the git hooks/post-receive script.

martin-schanzenbach pushed a commit to branch master
in repository taldir.

The following commit(s) were added to refs/heads/master by this push:
     new c4fff82  parse order id; return payment required if fee configured
c4fff82 is described below

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

    parse order id; return payment required if fee configured
---
 cmd/taldir-server/main_test.go              | 11 +++++++++++
 cmd/taldir-server/testdata/taldir-test.conf |  9 +++++++--
 pkg/rest/taldir.go                          | 20 ++++++++++++++++----
 pkg/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
index d9371b0..13c68c0 100644
--- 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
index 3acf619..4cbfe6e 100644
--- 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
index 964aa4e..941b228 100644
--- 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
index e4fad51..2056a29 100644
--- 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
+}

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]