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

@ -1,4 +1,4 @@
package main
package internalapi
import (
"crypto/rand"
@ -22,7 +22,7 @@ func random_hex() string {
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" {
msg := "createboltcard: internal API function is not enabled"
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 (
"crypto/rand"
"encoding/hex"
"github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/resp_err"
log "github.com/sirupsen/logrus"
"net/http"
"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" {
msg := "updateboltcard: internal API function is not enabled"
log.Debug(msg)
@ -39,8 +36,8 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
card_name := r.URL.Query().Get("card_name")
// check if card_name already exists
//TODO: allow multiple deactivated cards with the same card_name
// check if card_name exists
card_count, err := db.Get_card_name_count(card_name)
if err != nil {
log.Warn(err.Error())
@ -60,14 +57,6 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
"card_name": card_name, "tx_max": tx_max,
"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
err = db.Update_card(card_name, tx_max, enable_flag)
@ -76,24 +65,9 @@ func updateboltcard(w http.ResponseWriter, r *http.Request) {
return
}
// return the URI + one_time_code
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 + `"}`)
// send a response
jsonData := []byte(`{"status":"OK"}`)
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)

View file

@ -1,4 +1,4 @@
package main
package internalapi
import (
"github.com/boltcard/boltcard/db"
@ -8,7 +8,7 @@ import (
"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" {
msg := "wipeboltcard: internal API function is not enabled"
log.Debug(msg)