gnunet-gns-registrar

GNU Name System registrar
Log | Files | Refs | README

commit 76814b28d28d599970a174e00f3cdf845c176076
parent c8dcd347e240bd64b884bd618aaaf804266d83db
Author: Martin Schanzenbach <schanzen@gnunet.org>
Date:   Sun, 17 Dec 2023 09:20:55 +0100

some refactoring, cleanup

Diffstat:
Mpkg/rest/gnsregistrar.go | 82++++++++++++++++++++++++++++++++++++++++++++++++-------------------------------
1 file changed, 50 insertions(+), 32 deletions(-)

diff --git a/pkg/rest/gnsregistrar.go b/pkg/rest/gnsregistrar.go @@ -189,38 +189,42 @@ func (t *Registrar) landingPage(w http.ResponseWriter, r *http.Request) { return } -func (t *Registrar) searchPage(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "text/html; charset=utf-8") - label := r.URL.Query().Get("label") +func (t *Registrar) isNameValid(label string) error { if ("@" == label) { - http.Redirect(w, r, fmt.Sprintf("/?error=Label '%s' invalid: '@' not allowed", label), http.StatusSeeOther) - return + return errors.New(fmt.Sprintf("'%s' invalid: '@' not allowed", label)) } if (strings.Contains(label, ".")) { - http.Redirect(w, r, fmt.Sprintf("/?error=Label '%s' invalid: '.' not allowed", label), http.StatusSeeOther) - return + return errors.New(fmt.Sprintf("'%s' invalid: '.' not allowed", label)) } if t.ValidLabelRegex != "" { matched, _ := regexp.MatchString(t.ValidLabelRegex, label) if !matched { - http.Redirect(w, r, fmt.Sprintf("/?error=Label '%s' not allowed by policy", label), http.StatusSeeOther) - return + return errors.New(fmt.Sprintf("Label '%s' not allowed by policy", label)) } } if t.ValidLabelScript != "" { path, err := exec.LookPath(t.ValidLabelScript) if err != nil { fmt.Println(err) - http.Redirect(w, r, fmt.Sprintf("/?error=Internal error", label), http.StatusSeeOther) - return + return errors.New(fmt.Sprintf("Internal error", label)) } out, err := exec.Command(path, label).Output() if err != nil { fmt.Printf("%s, %w", out, err) - http.Redirect(w, r, fmt.Sprintf("/?error=Label '%s' not allowed by policy", label), http.StatusSeeOther) - return + return errors.New(fmt.Sprintf("Label '%s' not allowed by policy", label)) } } + return nil +} + +func (t *Registrar) searchPage(w http.ResponseWriter, r *http.Request) { + w.Header().Set("Content-Type", "text/html; charset=utf-8") + label := r.URL.Query().Get("label") + err := t.isNameValid(label) + if nil != err { + http.Redirect(w, r, fmt.Sprintf("/?error=%s", err), http.StatusSeeOther) + return + } http.Redirect(w, r, "/name/" + label, http.StatusSeeOther) return } @@ -265,6 +269,9 @@ func (t *Registrar) createOrUpdateRegistration(nsRecord *NamestoreRecord) (error if http.StatusNoContent != resp.StatusCode { fmt.Printf("Got error: %d\n", resp.StatusCode) err = json.NewDecoder(resp.Body).Decode(&gnunetError) + if nil != err { + return errors.New("GNUnet REST API error: " + err.Error()) + } return errors.New("GNUnet REST API error: " + gnunetError.Description) } return nil @@ -465,7 +472,13 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { var regMetadata *RegistrationMetadata w.Header().Set("Content-Type", "text/html; charset=utf-8") client := &http.Client{} - req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes", nil) + label := vars["label"] + err := t.isNameValid(label) + if nil != err { + http.Redirect(w, r, fmt.Sprintf("/?error=%s", err), http.StatusSeeOther) + return + } + req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + label + "?include_maintenance=yes", nil) if t.GnunetBasicAuthEnabled { req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword) } @@ -489,21 +502,21 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { fmt.Printf("Failed to get zone contents" + err.Error()) return } - regMetadata, err = t.getCurrentRegistrationMetadata(vars["label"], &namestoreResponse) + regMetadata, err = t.getCurrentRegistrationMetadata(label, &namestoreResponse) if err != nil { http.Redirect(w, r, "/" + "?error=Registration failed: Failed to get registration metadata", http.StatusSeeOther) fmt.Printf("Failed to get registration metadata" + err.Error()) return } } else if http.StatusNotFound != resp.StatusCode { - http.Redirect(w, r, "/name/" + vars["label"] + "?error=Registration failed: Error determining zone status", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Error determining zone status", http.StatusSeeOther) return } var errorMsg = "" var regId = "" if nil != regMetadata { if regMetadata.Paid == false { - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Pending buy order", http.StatusSeeOther) + http.Redirect(w, r, "/name/"+ label + "?error=Registration failed: Pending buy order", http.StatusSeeOther) return } regMetadata.Paid = false @@ -511,21 +524,21 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { } else { regId = generateRegistrationId() } - summaryMsg := strings.Replace(t.SummaryTemplateString, "${NAME}", vars["label"], 1) - orderID, newOrderErr := t.Merchant.AddNewOrder(*t.RegistrationCost, summaryMsg, t.BaseUrl + "/name/" + vars["label"] + "/edit?token=" + regId) + summaryMsg := strings.Replace(t.SummaryTemplateString, "${NAME}", label, 1) + orderID, newOrderErr := t.Merchant.AddNewOrder(*t.RegistrationCost, summaryMsg, t.BaseUrl + "/name/" + label + "/edit?token=" + regId) if newOrderErr != nil { fmt.Println(newOrderErr) - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Unable to create order", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Unable to create order", http.StatusSeeOther) return } _, 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) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Error getting payment data", http.StatusSeeOther) return } qrPng, qrErr := qrcode.Encode(payto, qrcode.Medium, 256) if qrErr != nil { - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Error generating QR code", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Error generating QR code", http.StatusSeeOther) return } paymentUntil := time.Now().Add(t.PaymentExpiration) @@ -545,7 +558,7 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { metadataRecordValue, err := json.Marshal(regMetadata) if nil != err { fmt.Println(err) - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Internal error", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Internal error", http.StatusSeeOther) return } metaRecord.Value = string(metadataRecordValue) @@ -553,14 +566,14 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { err = t.createOrUpdateRegistration(&namestoreResponse) if nil != err { fmt.Println(err) - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Internal error", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Internal error", http.StatusSeeOther) return } } else { - err = t.setupRegistrationMetadataBeforePayment(vars["label"], r.URL.Query().Get("zkey"), orderID, paymentUntil, regId) + err = t.setupRegistrationMetadataBeforePayment(label, r.URL.Query().Get("zkey"), orderID, paymentUntil, regId) if err != nil { fmt.Println(err) - http.Redirect(w, r, "/name/"+vars["label"] + "?error=Registration failed: Internal error", http.StatusSeeOther) + http.Redirect(w, r, "/name/" + label + "?error=Registration failed: Internal error", http.StatusSeeOther) return } } @@ -569,9 +582,9 @@ func (t *Registrar) buyPage(w http.ResponseWriter, r *http.Request) { fullData := map[string]interface{}{ "qrCode": template.URL("data:image/png;base64," + encodedPng), "payto": template.URL(payto), - "fulfillmentUrl": template.URL(t.BaseUrl + "/name/" + vars["label"] + "/edit?token=" + regId), + "fulfillmentUrl": template.URL(t.BaseUrl + "/name/" + label + "/edit?token=" + regId), "registrationId": regId, - "label": vars["label"], + "label": label, "error": errorMsg, "cost": cost, "suffixHint": t.SuffixHint, @@ -682,9 +695,14 @@ func (t *Registrar) namePage(w http.ResponseWriter, r *http.Request) { var regMetadata *RegistrationMetadata var remainingDays int64 var registered = r.URL.Query().Get("registered") == "true" - // FIXME redirect back if label empty + label := vars["label"] + err := t.isNameValid(label) + if nil != err { + http.Redirect(w, r, fmt.Sprintf("/?error=%s", err), http.StatusSeeOther) + return + } client := &http.Client{} - req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + vars["label"] + "?include_maintenance=yes", nil) + req, _ := http.NewRequest(http.MethodGet,t.GnunetUrl + "/namestore/" + t.RootZoneName + "/" + label + "?include_maintenance=yes", nil) if t.GnunetBasicAuthEnabled { req.SetBasicAuth(t.GnunetUsername, t.GnunetPassword) } @@ -708,7 +726,7 @@ func (t *Registrar) namePage(w http.ResponseWriter, r *http.Request) { http.Redirect(w, r, "/" + "?error=Failed to get zone contents.", http.StatusSeeOther) return } - regMetadata, err = t.getCurrentRegistrationMetadata(vars["label"], &namestoreResponse) + regMetadata, err = t.getCurrentRegistrationMetadata(label, &namestoreResponse) if err != nil { fmt.Println("Failed to get registration metadata: " + err.Error()) http.Redirect(w, r, "/" + "?error=Failed to get registration metadata.", http.StatusSeeOther) @@ -732,7 +750,7 @@ func (t *Registrar) namePage(w http.ResponseWriter, r *http.Request) { } cost, _ := t.RegistrationCost.FormatWithCurrencySpecification(t.CurrencySpec) fullData := map[string]interface{}{ - "label": vars["label"], + "label": label, "error": r.URL.Query().Get("error"), "cost": cost, "available": regMetadata == nil,