commit a309abdb1388143575956cabd2b03306029a40da
parent 879ec5c27464363731de492b7e9ba446fdae75fe
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date: Sun, 10 Dec 2023 19:13:10 +0100
Add gnunet basic auth; update taler-go
Diffstat:
3 files changed, 55 insertions(+), 10 deletions(-)
diff --git a/gns-registrar.conf b/gns-registrar.conf
@@ -2,7 +2,9 @@
production = false
base_url = "http://localhost:11000"
base_url_gnunet = "http://localhost:7776"
-gnunet_auth = ""
+basic_auth_gnunet_enabled = true
+basic_auth_gnunet_username = "schanzen"
+basic_auth_gnunet_password = "01KYT94APDV14"
base_url_merchant = https://backend.demo.taler.net
merchant_token = sandbox
bind_to = "localhost:11000"
diff --git a/go.mod b/go.mod
@@ -4,7 +4,7 @@ go 1.18
require (
github.com/gorilla/mux v1.8.1
- github.com/schanzen/taler-go v0.0.3
+ github.com/schanzen/taler-go v0.0.4
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e
golang.org/x/text v0.14.0
gopkg.in/ini.v1 v1.67.0
diff --git a/pkg/rest/gnsregistrar.go b/pkg/rest/gnsregistrar.go
@@ -123,6 +123,15 @@ type Registrar struct {
// Gnunet REST API basename
GnunetUrl string
+ // Gnunet basic auth on/off
+ GnunetBasicAuthEnabled bool
+
+ // Gnunet basic auth
+ GnunetUsername string
+
+ // Gnunet basic auth
+ GnunetPassword string
+
// Registrar base URL
BaseUrl string
@@ -216,7 +225,12 @@ func (t *Registrar) registerName(w http.ResponseWriter, r *http.Request) {
reqString, _ := json.Marshal(namestoreRequest)
// FIXME handle errors here
fmt.Println(namestoreRequest)
- resp, err := http.Post(t.GnunetUrl+"/namestore/" + t.RootZoneName, "application/json", bytes.NewBuffer(reqString))
+ client := &http.Client{}
+ req, _ := http.NewRequest(http.MethodPost, t.GnunetUrl+"/namestore/" + t.RootZoneName, bytes.NewBuffer(reqString))
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
+ resp, err := client.Do(req)
resp.Body.Close()
if http.StatusNoContent != resp.StatusCode {
fmt.Printf("Got error: %d\n", resp.StatusCode)
@@ -240,6 +254,9 @@ func (t *Registrar) expireRegistration(label string) (error) {
var gnunetError GnunetError
client := &http.Client{}
req, _ := http.NewRequest(http.MethodDelete,t.GnunetUrl+"/namestore/" + t.RootZoneName + "/" + label, nil)
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
resp, err := client.Do(req)
resp.Body.Close()
if err != nil {
@@ -262,6 +279,9 @@ func (t *Registrar) createOrUpdateRegistration(nsRecord *NamestoreRecord) (error
fmt.Println(nsRecord)
client := &http.Client{}
req, _ := http.NewRequest(http.MethodPut,t.GnunetUrl+"/namestore/" + t.RootZoneName, bytes.NewBuffer(reqString))
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
resp, err := client.Do(req)
if nil != err {
return err
@@ -317,7 +337,12 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) {
var namestoreResponse NamestoreRecord
var regMetadata *RegistrationMetadata
w.Header().Set("Content-Type", "text/html; charset=utf-8")
- resp, err := http.Get(t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes")
+ client := &http.Client{}
+ req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes", nil)
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
+ resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to get zone contents")
return
@@ -355,7 +380,7 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Unable to create order", http.StatusSeeOther)
return
}
- payto, paytoErr := t.Merchant.IsOrderPaid(orderID)
+ _, payto, paytoErr := t.Merchant.IsOrderPaid(orderID)
if paytoErr != nil {
http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Error getting payment data", http.StatusSeeOther)
return
@@ -403,9 +428,14 @@ func (t *Registrar) getCurrentRegistrationMetadata(label string, nsRecord *Names
return nil, nil
}
if !regMetadata.Paid {
- payto, paytoErr := t.Merchant.IsOrderPaid(regMetadata.OrderID)
+ rc, payto, paytoErr := t.Merchant.IsOrderPaid(regMetadata.OrderID)
if nil != paytoErr {
- return nil, errors.New("Error determining payment status")
+ if rc == http.StatusNotFound {
+ fmt.Printf("Registration for %s not found, removing\n", label)
+ t.expireRegistration(label)
+ return nil, nil
+ }
+ return nil, errors.New("Error determining payment status: " + paytoErr.Error())
}
if "" == payto {
// Order was paid!
@@ -466,7 +496,12 @@ func (t *Registrar) namePage(w http.ResponseWriter, r *http.Request) {
var regMetadata *RegistrationMetadata
var registered = r.URL.Query().Get("registered") == "true"
// FIXME redirect back if label empty
- resp, err := http.Get(t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes")
+ client := &http.Client{}
+ req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes", nil)
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
+ resp, err := client.Do(req)
if err != nil {
http.Redirect(w, r, "/" + "?error=Failed to get zone contents.", http.StatusSeeOther)
fmt.Printf("Failed to get zone contents")
@@ -582,7 +617,15 @@ func (t *Registrar) Initialize(cfgfile string) {
t.SummaryTemplateString = t.Cfg.Section("gns-registrar").Key("order_summary_template").MustString("Registration of `${NAME}' at GNUnet FCFS registrar")
t.RootZoneName = t.Cfg.Section("gns-registrar").Key("root_zone_name").MustString("test")
t.GnunetUrl = t.Cfg.Section("gns-registrar").Key("base_url_gnunet").MustString("http://localhost:7776")
- resp, err := http.Get(t.GnunetUrl + "/identity/name/" + t.RootZoneName)
+ t.GnunetBasicAuthEnabled = t.Cfg.Section("gns-registrar").Key("basic_auth_gnunet_enabled").MustBool(true)
+ t.GnunetUsername = t.Cfg.Section("gns-registrar").Key("basic_auth_gnunet_username").MustString("jdoe")
+ t.GnunetPassword = t.Cfg.Section("gns-registrar").Key("basic_auth_gnunet_password").MustString("secret")
+ client := &http.Client{}
+ req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/identity/name/" + t.RootZoneName, nil)
+ if t.GnunetBasicAuthEnabled {
+ req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword)
+ }
+ resp, err := client.Do(req)
if err != nil {
fmt.Printf("Failed to get zone key")
return
@@ -604,7 +647,7 @@ func (t *Registrar) Initialize(cfgfile string) {
}
t.RootZoneKey = identityResponse.Pubkey
} else {
- fmt.Printf("Failed to get zone contents" + err.Error())
+ fmt.Printf("Failed to get zone contents")
os.Exit(1)
}
merchURL := t.Cfg.Section("gns-registrar").Key("base_url_merchant").MustString("https://backend.demo.taler.net")