From 2df4def9e2ef04017811bbe003e20c9949a261fb Mon Sep 17 00:00:00 2001 From: "Dr. Tobias Baur" Date: Fri, 3 Mar 2023 15:22:01 +0100 Subject: [PATCH] added easy api brutally deletes users and gives them a new pin on call. --- api.go | 9 +++++++-- db.go | 9 +++++++++ main.go | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 69 insertions(+), 3 deletions(-) diff --git a/api.go b/api.go index 4ca9cc5..cbfdbac 100644 --- a/api.go +++ b/api.go @@ -16,6 +16,11 @@ type Response struct { Data interface{} `json:"data"` } +type ResponseEasy struct { + Ok bool `json:"ok"` + Pin string `json:"pin"` +} + type SuccessClaim struct { Name string `json:"name"` Domain string `json:"domain"` @@ -26,7 +31,7 @@ type SuccessClaim struct { // not authenticated, if correct pin is provided call returns the SuccessClaim func ClaimAddress(w http.ResponseWriter, r *http.Request) { params := parseParams(r) - pin, inv, err := SaveName(params.Name, params.Domain, params, params.Pin) + pin, inv, err := SaveName(params.Name, params.Domain, params, params.Pin, false, "") if err != nil { sendError(w, 400, "could not register name: %s", err.Error()) return @@ -78,7 +83,7 @@ func UpdateUser(w http.ResponseWriter, r *http.Request) { params.Pin = r.Header.Get("X-Pin") } - if _, _, err := SaveName(name, domain, params, params.Pin); err != nil { + if _, _, err := SaveName(name, domain, params, params.Pin, false, ""); err != nil { sendError(w, 500, err.Error()) return } diff --git a/db.go b/db.go index 1cd0efd..67778e4 100644 --- a/db.go +++ b/db.go @@ -34,6 +34,8 @@ func SaveName( domain string, params *Params, providedPin string, + overwrite bool, + previousname string, ) (pin string, inv string, err error) { name = strings.ToLower(name) domain = strings.ToLower(domain) @@ -52,6 +54,13 @@ func SaveName( return "", "", errors.New("that name does not exist") } + if overwrite { + previouskey := []byte(getID(previousname, domain)) + if err := db.Delete(previouskey, pebble.Sync); err != nil { + return "", "", fmt.Errorf("couldn't delete previous entry: %w", err) + } + } + params.Name = name params.Domain = domain diff --git a/main.go b/main.go index 6854c95..3a8d598 100644 --- a/main.go +++ b/main.go @@ -2,6 +2,7 @@ package main import ( "embed" + "encoding/json" "fmt" "net/http" "os" @@ -114,7 +115,7 @@ func main() { Waki: r.FormValue("waki"), NodeId: r.FormValue("nodeid"), Rune: r.FormValue("rune"), - }, r.FormValue("pin")) + }, r.FormValue("pin"), false, "") if err != nil { w.WriteHeader(500) fmt.Fprint(w, err.Error()) @@ -130,6 +131,57 @@ func main() { }, ) + //Alternative API function that brutally deletes previous user and gives them a new name and pin. + //Also works when user didn't exist before. + + //Returns Status ok (bool) and pin needed to authorize next call. Save this and the name e.g. in a DB for the user. + //http Post the following content to yourdomain/api/easy + //Expected input with lnbits example: + // { new StringContent(thecurrentnameyouwanttochange), "currentname" }, + // { new StringContent(thenewname), "name" }, + // { new StringContent(https://yoursatdressdomain.com), "domain" }, + // { new StringContent("lnbits"), "kind" }, + // { new StringContent("https://lnbits.yourdomain.com"), "host" }, + // { new StringContent(lnbitsapikey), "key" }, + // { new StringContent(pin), "pin" }, + + router.Path("/api/easy/").HandlerFunc( + func(w http.ResponseWriter, r *http.Request) { + + newname := r.FormValue("name") + currentName := r.FormValue("currentname") + domain := r.FormValue("domain") + currentPin := r.FormValue("pin") + + params := Params{ + Kind: r.FormValue("kind"), + Host: r.FormValue("host"), + Key: r.FormValue("key"), + Pak: r.FormValue("pak"), + Waki: r.FormValue("waki"), + NodeId: r.FormValue("nodeid"), + Rune: r.FormValue("rune"), + } + + pin, _, err := SaveName(newname, domain, ¶ms, currentPin, true, currentName) + if err != nil { + w.WriteHeader(500) + fmt.Fprint(w, err.Error()) + return + } + params.Pin = pin + + response := ResponseEasy{ + Ok: true, + Pin: pin, + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusCreated) + json.NewEncoder(w).Encode(response) + }, + ) + api := router.PathPrefix("/api/v1").Subrouter() api.Use(authenticate)