taldir

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

commit 2c22b54ad3d144363a37da15f1ecca70894ee14b
parent f300b5f542afc68da4b07e35a307a7e1e70e28f1
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Wed,  6 Jul 2022 17:22:31 +0200

refactor; config flag

Diffstat:
Mcmd/taldir-server/main.go | 30+++++++++++-------------------
Mutil/helper.go | 11++++++++++-
2 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/cmd/taldir-server/main.go b/cmd/taldir-server/main.go @@ -42,7 +42,6 @@ import ( "gorm.io/gorm" "encoding/base64" "taler.net/taldir/util" - "math/rand" "crypto/sha512" "gorm.io/driver/postgres" "gopkg.in/ini.v1" @@ -214,11 +213,10 @@ var validationTpl *template.Template // Primary lookup function. // Allows the caller to query a wallet key using the hash(!) of the -// identity, e.g. sha256(<email address>) +// identity, e.g. SHA512(<email address>) func getSingleEntry(w http.ResponseWriter, r *http.Request){ vars := mux.Vars(r) var entry Entry - //identityKeyHash := hashIdentityKey(vars["identity_key"]) hs_address := saltHAddress(vars["h_address"]) var err = db.First(&entry, "hs_address = ?", hs_address).Error if err == nil { @@ -300,16 +298,6 @@ func validationRequest(w http.ResponseWriter, r *http.Request){ } -// Generates random reference token used in the validation flow. -func generateToken() string { - randBytes := make([]byte, 32) - _, err := rand.Read(randBytes) - if err != nil { - panic(err) - } - return util.EncodeBinaryToString(randBytes) -} - func registerRequest(w http.ResponseWriter, r *http.Request){ vars := mux.Vars(r) var req RegisterMessage @@ -376,7 +364,7 @@ func registerRequest(w http.ResponseWriter, r *http.Request){ w.WriteHeader(202) return } else { - validation.Code = generateToken() + validation.Code = util.GenerateCode() validation.Inbox = req.Inbox validation.Duration = req.Duration validation.PublicKey = req.PublicKey @@ -464,8 +452,6 @@ func handleRequests() { /* Registration API */ - //myRouter.HandleFunc("/directory/{identity_key}", returnSingleEntry).Methods("GET") - //myRouter.HandleFunc("/validation/{reference}", validateSingleEntry).Methods("GET") myRouter.HandleFunc("/{h_address}", getSingleEntry).Methods("GET") myRouter.HandleFunc("/register/{method}", registerRequest).Methods("POST") myRouter.HandleFunc("/register/{h_address}/{validation_code}", validationPage).Methods("GET") @@ -475,7 +461,14 @@ func handleRequests() { } func main() { - _cfg, err := ini.Load("taldir.conf") + var dropFlag = flag.Bool("D", false, "Drop all data in table (DANGEROUS!)") + var cfgFlag = flag.String("c", "", "Configuration file to use") + flag.Parse() + cfgfile := "taldir.conf" + if len(*cfgFlag) != 0 { + cfgfile = *cfgFlag + } + _cfg, err := ini.Load(cfgfile) if err != nil { fmt.Printf("Failed to read config: %v", err) os.Exit(1) @@ -484,8 +477,7 @@ func main() { if cfg.Section("taldir").Key("production").MustBool(false) { fmt.Println("Production mode enabled") } - var dropFlag = flag.Bool("D", false, "Drop all data in table (DANGEROUS!)") - flag.Parse() + validators = make(map[string]bool) for _, a := range strings.Split(cfg.Section("taldir").Key("validators").String(), " ") { validators[a] = true diff --git a/util/helper.go b/util/helper.go @@ -22,6 +22,7 @@ package util import ( "fmt" "crypto/sha512" + "math/rand" ) @@ -38,4 +39,12 @@ func GenerateSolution(pubkeyEncoded string, code string) string { return EncodeBinaryToString(h.Sum(nil)) } - +// Generates random reference token used in the validation flow. +func GenerateCode() string { + randBytes := make([]byte, 32) + _, err := rand.Read(randBytes) + if err != nil { + panic(err) + } + return EncodeBinaryToString(randBytes) +}