add Updateboltcard()

This commit is contained in:
Peter Rounce 2023-02-22 08:11:52 +00:00
parent 0300a5aa30
commit dadc76f0d3
8 changed files with 66 additions and 49 deletions

View file

@ -834,3 +834,40 @@ func Wipe_card(card_name string) (*Card_wipe_info, error) {
return &card_wipe_info, nil return &card_wipe_info, nil
} }
func Update_card(card_name string, tx_max int, lnurlw_enable bool) error {
lnurlw_enable_yn := "N"
if lnurlw_enable {
lnurlw_enable_yn = "Y"
}
db, err := open()
if err != nil {
return err
}
defer db.Close()
sqlStatement := `UPDATE cards SET tx_max = $2, lnurlw_enable = $3 ` +
`WHERE card_name = $1;`
res, err := db.Exec(sqlStatement, card_name, tx_max, lnurlw_enable_yn)
if err != nil {
return err
}
count, err := res.RowsAffected()
if err != nil {
return err
}
if count != 1 {
return errors.New("not one card record updated")
}
return nil
}

View file

@ -1,4 +1,4 @@
package main package internalapi
import ( import (
"crypto/rand" "crypto/rand"
@ -22,7 +22,7 @@ func random_hex() string {
return hex.EncodeToString(b) return hex.EncodeToString(b)
} }
func createboltcard(w http.ResponseWriter, r *http.Request) { func Createboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" { if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "createboltcard: internal API function is not enabled" msg := "createboltcard: internal API function is not enabled"
log.Debug(msg) log.Debug(msg)

12
internalapi/ping.go Normal file
View file

@ -0,0 +1,12 @@
package internalapi
import (
"net/http"
)
func Internal_ping(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK","pong":"internal API"}`)
w.Write(jsonData)
}

View file

@ -1,17 +1,14 @@
package main package internalapi
import ( import (
"crypto/rand"
"encoding/hex"
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/resp_err" "github.com/boltcard/boltcard/resp_err"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
"strconv" "strconv"
"strings"
) )
func updateboltcard(w http.ResponseWriter, r *http.Request) { func Updateboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" { if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "updateboltcard: internal API function is not enabled" msg := "updateboltcard: internal API function is not enabled"
log.Debug(msg) log.Debug(msg)
@ -39,8 +36,8 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
card_name := r.URL.Query().Get("card_name") card_name := r.URL.Query().Get("card_name")
// check if card_name already exists // check if card_name exists
//TODO: allow multiple deactivated cards with the same card_name
card_count, err := db.Get_card_name_count(card_name) card_count, err := db.Get_card_name_count(card_name)
if err != nil { if err != nil {
log.Warn(err.Error()) log.Warn(err.Error())
@ -60,14 +57,6 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
"card_name": card_name, "tx_max": tx_max, "card_name": card_name, "tx_max": tx_max,
"enable": enable_flag}).Info("createboltcard API request") "enable": enable_flag}).Info("createboltcard API request")
// create the keys
one_time_code := random_hex()
k0_auth_key := random_hex()
k2_cmac_key := random_hex()
k3 := random_hex()
k4 := random_hex()
// update the card record // update the card record
err = db.Update_card(card_name, tx_max, enable_flag) err = db.Update_card(card_name, tx_max, enable_flag)
@ -76,24 +65,9 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
return return
} }
// return the URI + one_time_code // send a response
hostdomain := db.Get_setting("HOST_DOMAIN")
url := ""
if strings.HasSuffix(hostdomain, ".onion") {
url = "http://" + hostdomain + "/new?a=" + one_time_code
} else {
url = "https://" + hostdomain + "/new?a=" + one_time_code
}
// log the response
log.WithFields(log.Fields{
"card_name": card_name, "url": url}).Info("updateboltcard API response")
jsonData := []byte(`{"status":"OK",` +
`"url":"` + url + `"}`)
jsonData := []byte(`{"status":"OK"}`)
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
w.Write(jsonData) w.Write(jsonData)

View file

@ -1,4 +1,4 @@
package main package internalapi
import ( import (
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
@ -8,7 +8,7 @@ import (
"strconv" "strconv"
) )
func wipeboltcard(w http.ResponseWriter, r *http.Request) { func Wipeboltcard(w http.ResponseWriter, r *http.Request) {
if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" { if db.Get_setting("FUNCTION_INTERNAL_API") != "ENABLE" {
msg := "wipeboltcard: internal API function is not enabled" msg := "wipeboltcard: internal API function is not enabled"
log.Debug(msg) log.Debug(msg)

View file

@ -4,6 +4,7 @@ import (
"github.com/boltcard/boltcard/db" "github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/lnurlp" "github.com/boltcard/boltcard/lnurlp"
"github.com/boltcard/boltcard/lnurlw" "github.com/boltcard/boltcard/lnurlw"
"github.com/boltcard/boltcard/internalapi"
"github.com/gorilla/mux" "github.com/gorilla/mux"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
"net/http" "net/http"
@ -50,9 +51,10 @@ func main() {
// this has no authentication and is not to be exposed publicly // this has no authentication and is not to be exposed publicly
// it exists for use on a private virtual network within a docker container // it exists for use on a private virtual network within a docker container
internal_router.Path("/ping").Methods("GET").HandlerFunc(internal_ping) internal_router.Path("/ping").Methods("GET").HandlerFunc(internalapi.Internal_ping)
internal_router.Path("/createboltcard").Methods("GET").HandlerFunc(createboltcard) internal_router.Path("/createboltcard").Methods("GET").HandlerFunc(internalapi.Createboltcard)
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(wipeboltcard) internal_router.Path("/updateboltcard").Methods("GET").HandlerFunc(internalapi.Updateboltcard)
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(internalapi.Wipeboltcard)
port := db.Get_setting("HOST_PORT") port := db.Get_setting("HOST_PORT")
if port == "" { if port == "" {

10
ping.go
View file

@ -5,16 +5,8 @@ import (
) )
func external_ping(w http.ResponseWriter, req *http.Request) { func external_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "external API")
}
func internal_ping(w http.ResponseWriter, req *http.Request) {
ping(w, "internal API")
}
func ping(w http.ResponseWriter, message string) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
jsonData := []byte(`{"status":"OK","pong":"` + message + `"}`) jsonData := []byte(`{"status":"OK","pong":"external API"}`)
w.Write(jsonData) w.Write(jsonData)
} }

View file

@ -20,7 +20,7 @@ CREATE TABLE cards (
tx_limit_sats INT NOT NULL, tx_limit_sats INT NOT NULL,
day_limit_sats INT NOT NULL, day_limit_sats INT NOT NULL,
lnurlp_enable CHAR(1) NOT NULL DEFAULT 'N', lnurlp_enable CHAR(1) NOT NULL DEFAULT 'N',
card_name VARCHAR(100) NOT NULL DEFAULT '', card_name VARCHAR(100) UNIQUE NOT NULL DEFAULT '',
email_address VARCHAR(100) DEFAULT '', email_address VARCHAR(100) DEFAULT '',
email_enable CHAR(1) NOT NULL DEFAULT 'N', email_enable CHAR(1) NOT NULL DEFAULT 'N',
uid_privacy CHAR(1) NOT NULL DEFAULT 'N', uid_privacy CHAR(1) NOT NULL DEFAULT 'N',