added easy api

brutally deletes users and gives them a new pin on call.
This commit is contained in:
Dr. Tobias Baur
2023-03-03 15:22:01 +01:00
parent b8e2b7298b
commit 2df4def9e2
3 changed files with 69 additions and 3 deletions
+7 -2
View File
@@ -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
}
+9
View File
@@ -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
+53 -1
View File
@@ -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, &params, 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)