taldir

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

commit 0cb37955d24f95c106bbb93552fc8db8f3ea2a46
parent c1fc528c65134d9eeeb4ff5550513dab00101245
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Tue,  5 Jul 2022 10:00:39 +0200

add validation scripts

Diffstat:
Mtaldir.conf | 11+++++++++--
Mtaldir.go | 56+++++++++++++++++++++++++++++++++++++++++---------------
Avalidate_test.sh | 2++
3 files changed, 52 insertions(+), 17 deletions(-)

diff --git a/taldir.conf b/taldir.conf @@ -1,17 +1,24 @@ [taldir] production = false -validators = "email phone" +validators = "email phone test" host = "https://taldir.net" bind_to = "localhost:11000" salt = "ChangeMe" monthly_fee = 1 Bazillion Kudos +request_frequency = 3 [taldir-email] sender = "taldir@taler.net" -challenge_fee = 0.5 Fantastillion Kudos +challenge_fee = 0.5 Fantastillion Kudos +command = validate_email.sh [taldir-phone] challenge_fee = 5 Kudos +command = validate_phone.sh + +[taldir-test] +challenge_fee = 23 Kudos +command = ./validate_test.sh [taldir-pq] host = "localhost" diff --git a/taldir.go b/taldir.go @@ -2,6 +2,7 @@ package main import ( "os" + "os/exec" "fmt" "log" "net/http" @@ -49,7 +50,7 @@ type RateLimitedResponse struct { Code int `json:"code"` // At what frequency are new registrations allowed. FIXME: In what? - Request_frequency uint64 `json:"request_frequency"` + Request_frequency uint `json:"request_frequency"` // The human readable error message. Hint string `json:"hint"` @@ -94,6 +95,7 @@ type Validation struct { Method string `json:"method"` ValidationReference string `json:"reference"` PublicKey string `json:"public_key"` + RetryCount uint } type ErrorDetail struct { @@ -271,26 +273,50 @@ func registerRequest(w http.ResponseWriter, r *http.Request){ h := sha256.New() h.Write([]byte(req.Address)) validation.HAddress = base32.StdEncoding.EncodeToString(h.Sum(nil)) - validation.ValidationReference = generateToken() err = db.First(&validation, "h_address = ?", validation.HAddress).Error if err == nil { - // FIXME: We need to handle this properly: - // Registration already exists for this address for the specified duration. - // Returns for how long this registration is paid for. - w.WriteHeader(200) - return - } - err = db.Create(&validation).Error - if err != nil { - // FIXME: API needs 400 error codes in such cases - w.WriteHeader(http.StatusInternalServerError) - return + reqFrequency := cfg.Section("taldir").Key("request_frequency").MustUint(1) + if validation.RetryCount >= reqFrequency - 1 { + w.WriteHeader(429) + rlResponse := RateLimitedResponse{ + Code: 23, //FIXME TALER_EC_TALDIR_REGISTER_RATE_LIMITED + Request_frequency: reqFrequency, + Hint: "Retry maximum reached. Deleting validation", + } + jsonResp, _ := json.Marshal(rlResponse) + w.Write(jsonResp) + db.Delete(&validation) + return + } + validation.RetryCount++ + validation.ValidationReference = generateToken() + db.Save(&validation) + fmt.Printf("Retrying validation send\n") + } else { + validation.ValidationReference = generateToken() + err = db.Create(&validation).Error + if err != nil { + // FIXME: API needs 400 error codes in such cases + w.WriteHeader(http.StatusInternalServerError) + return + } + fmt.Println("Address registration request created:", validation) } - fmt.Println("Address registration request created:", validation) w.WriteHeader(202) // FIXME: Here we should call the validator shell script with the // parsed parameters to initiate the validation. - sendEmail(vars["identity"], validation) + if !cfg.Section("taldir-" + vars["method"]).HasKey("command") { + log.Fatal(err) + // FIXME cleanup validation? + return + } + command := cfg.Section("taldir-" + vars["method"]).Key("command").String() + out, err := exec.Command(command, req.Address, validation.ValidationReference).Output() + if err != nil { + log.Fatal(err) + } + fmt.Printf("Output from method script is %s\n", out) + // sendEmail(vars["identity"], validation) } func notImplemented(w http.ResponseWriter, r *http.Request) { diff --git a/validate_test.sh b/validate_test.sh @@ -0,0 +1,2 @@ +#!/bin/bash +echo $1 $2