Merge pull request #29 from boltcard/internal-api
an API option for internal/private use (no authentication) this covers the createboltcard & wipeboltcard console options have been left in but are a duplicate and should be reconsidered
This commit is contained in:
commit
91f6d388eb
5 changed files with 393 additions and 10 deletions
139
createboltcard.go
Normal file
139
createboltcard.go
Normal file
|
|
@ -0,0 +1,139 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func random_hex() string {
|
||||||
|
b := make([]byte, 16)
|
||||||
|
_, err := rand.Read(b)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
return hex.EncodeToString(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tx_max_str := r.URL.Query().Get("tx_max")
|
||||||
|
tx_max, err := strconv.Atoi(tx_max_str)
|
||||||
|
if err != nil {
|
||||||
|
msg := "createboltcard: tx_max is not a valid integer"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
day_max_str := r.URL.Query().Get("day_max")
|
||||||
|
day_max, err := strconv.Atoi(day_max_str)
|
||||||
|
if err != nil {
|
||||||
|
msg := "createboltcard: day_max is not a valid integer"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
enable_flag_str := r.URL.Query().Get("enable")
|
||||||
|
enable_flag, err := strconv.ParseBool(enable_flag_str)
|
||||||
|
if err != nil {
|
||||||
|
msg := "createboltcard: enable is not a valid boolean"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
card_name := r.URL.Query().Get("card_name")
|
||||||
|
|
||||||
|
uid_privacy_flag_str := r.URL.Query().Get("uid_privacy")
|
||||||
|
uid_privacy_flag, err := strconv.ParseBool(uid_privacy_flag_str)
|
||||||
|
if err != nil {
|
||||||
|
msg := "createboltcard: uid_privacy is not a valid boolean"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
allow_neg_bal_flag_str := r.URL.Query().Get("allow_neg_bal")
|
||||||
|
allow_neg_bal_flag, err := strconv.ParseBool(allow_neg_bal_flag_str)
|
||||||
|
if err != nil {
|
||||||
|
msg := "createboltcard: allow_neg_bal is not a valid boolean"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if card_name already exists
|
||||||
|
|
||||||
|
card_count, err := db_get_card_name_count(card_name)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if card_count > 0 {
|
||||||
|
msg := "createboltcard: the card name already exists in the database"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// log the request
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"card_name": card_name, "tx_max": tx_max, "day_max": day_max,
|
||||||
|
"enable": enable_flag, "uid_privacy": uid_privacy_flag,
|
||||||
|
"allow_neg_bal": allow_neg_bal_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()
|
||||||
|
|
||||||
|
// create the new card record
|
||||||
|
|
||||||
|
err = db_insert_card(one_time_code, k0_auth_key, k2_cmac_key, k3, k4,
|
||||||
|
tx_max, day_max, enable_flag, card_name,
|
||||||
|
uid_privacy_flag, allow_neg_bal_flag)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
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("createboltcard API response")
|
||||||
|
|
||||||
|
jsonData := []byte(`{"status":"OK",` +
|
||||||
|
`"url":"` + url + `"}`)
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write(jsonData)
|
||||||
|
}
|
||||||
118
database.go
118
database.go
|
|
@ -706,3 +706,121 @@ func db_get_card_total_sats(card_id int) (int, error) {
|
||||||
|
|
||||||
return card_total_sats, nil
|
return card_total_sats, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func db_get_card_name_count(card_name string) (card_count int, err error) {
|
||||||
|
|
||||||
|
card_count = 0
|
||||||
|
|
||||||
|
db, err := db_open()
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
sqlStatement := `SELECT COUNT(card_id) FROM cards WHERE card_name = $1;`
|
||||||
|
|
||||||
|
row := db.QueryRow(sqlStatement, card_name)
|
||||||
|
err = row.Scan(&card_count)
|
||||||
|
if err != nil {
|
||||||
|
return 0, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return card_count, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func db_insert_card(one_time_code string, k0_auth_key string, k2_cmac_key string, k3 string, k4 string,
|
||||||
|
tx_max_sats int, day_max_sats int, lnurlw_enable bool, card_name string, uid_privacy bool,
|
||||||
|
allow_neg_bal_ptr bool) error {
|
||||||
|
|
||||||
|
lnurlw_enable_yn := "N"
|
||||||
|
if lnurlw_enable {
|
||||||
|
lnurlw_enable_yn = "Y"
|
||||||
|
}
|
||||||
|
|
||||||
|
uid_privacy_yn := "N"
|
||||||
|
if uid_privacy {
|
||||||
|
uid_privacy_yn = "Y"
|
||||||
|
}
|
||||||
|
|
||||||
|
allow_neg_bal_yn := "N"
|
||||||
|
if allow_neg_bal_ptr {
|
||||||
|
allow_neg_bal_yn = "Y"
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := db_open()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// insert a new record into cards
|
||||||
|
|
||||||
|
sqlStatement := `INSERT INTO cards` +
|
||||||
|
` (one_time_code, k0_auth_key, k2_cmac_key, k3, k4, uid, last_counter_value,` +
|
||||||
|
` lnurlw_request_timeout_sec, tx_limit_sats, day_limit_sats, lnurlw_enable,` +
|
||||||
|
` one_time_code_used, card_name, uid_privacy, allow_negative_balance)` +
|
||||||
|
` VALUES ($1, $2, $3, $4, $5, '', 0, 60, $6, $7, $8, 'N', $9, $10, $11);`
|
||||||
|
res, err := db.Exec(sqlStatement, one_time_code, k0_auth_key, k2_cmac_key, k3, k4,
|
||||||
|
tx_max_sats, day_max_sats, lnurlw_enable_yn, card_name, uid_privacy_yn,
|
||||||
|
allow_neg_bal_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 inserted")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func db_wipe_card(card_name string) (*card_wipe_info, error) {
|
||||||
|
|
||||||
|
card_wipe_info := card_wipe_info{}
|
||||||
|
|
||||||
|
db, err := db_open()
|
||||||
|
if err != nil {
|
||||||
|
return &card_wipe_info, err
|
||||||
|
}
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
// set card as wiped and disabled
|
||||||
|
|
||||||
|
sqlStatement := `UPDATE cards SET` +
|
||||||
|
` lnurlw_enable = 'N', lnurlp_enable = 'N', email_enable = 'N', wiped = 'Y'` +
|
||||||
|
` WHERE card_name = $1;`
|
||||||
|
res, err := db.Exec(sqlStatement, card_name)
|
||||||
|
if err != nil {
|
||||||
|
return &card_wipe_info, err
|
||||||
|
}
|
||||||
|
count, err := res.RowsAffected()
|
||||||
|
if err != nil {
|
||||||
|
return &card_wipe_info, err
|
||||||
|
}
|
||||||
|
if count != 1 {
|
||||||
|
return &card_wipe_info, errors.New("not one card record updated")
|
||||||
|
}
|
||||||
|
|
||||||
|
// get card keys
|
||||||
|
|
||||||
|
sqlStatement = `SELECT card_id, uid, k0_auth_key, k2_cmac_key, k3, k4` +
|
||||||
|
` FROM cards WHERE card_name = $1;`
|
||||||
|
row := db.QueryRow(sqlStatement, card_name)
|
||||||
|
err = row.Scan(
|
||||||
|
&card_wipe_info.id,
|
||||||
|
&card_wipe_info.uid,
|
||||||
|
&card_wipe_info.k0,
|
||||||
|
&card_wipe_info.k2,
|
||||||
|
&card_wipe_info.k3,
|
||||||
|
&card_wipe_info.k4)
|
||||||
|
if err != nil {
|
||||||
|
return &card_wipe_info, err
|
||||||
|
}
|
||||||
|
|
||||||
|
card_wipe_info.k1 = db_get_setting("AES_DECRYPT_KEY")
|
||||||
|
|
||||||
|
return &card_wipe_info, nil
|
||||||
|
}
|
||||||
|
|
|
||||||
43
main.go
43
main.go
|
|
@ -7,8 +7,6 @@ import (
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
var router = mux.NewRouter()
|
|
||||||
|
|
||||||
func write_error(w http.ResponseWriter) {
|
func write_error(w http.ResponseWriter) {
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.WriteHeader(http.StatusOK)
|
w.WriteHeader(http.StatusOK)
|
||||||
|
|
@ -41,26 +39,51 @@ func main() {
|
||||||
DisableHTMLEscape: true,
|
DisableHTMLEscape: true,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
var external_router = mux.NewRouter()
|
||||||
|
var internal_router = mux.NewRouter()
|
||||||
|
|
||||||
|
// external API
|
||||||
|
|
||||||
|
// ping
|
||||||
|
external_router.Path("/ping").Methods("GET").HandlerFunc(external_ping)
|
||||||
// createboltcard
|
// createboltcard
|
||||||
router.Path("/new").Methods("GET").HandlerFunc(new_card_request)
|
external_router.Path("/new").Methods("GET").HandlerFunc(new_card_request)
|
||||||
// lnurlw for pos
|
// lnurlw for pos
|
||||||
router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response)
|
external_router.Path("/ln").Methods("GET").HandlerFunc(lnurlw_response)
|
||||||
router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback)
|
external_router.Path("/cb").Methods("GET").HandlerFunc(lnurlw_callback)
|
||||||
// lnurlp for lightning address
|
// lnurlp for lightning address
|
||||||
router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response)
|
external_router.Path("/.well-known/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_response)
|
||||||
router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback)
|
external_router.Path("/lnurlp/{name}").Methods("GET").HandlerFunc(lnurlp_callback)
|
||||||
|
|
||||||
|
// internal API
|
||||||
|
// this has no authentication and is not to be exposed publicly
|
||||||
|
// 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("/createboltcard").Methods("GET").HandlerFunc(createboltcard)
|
||||||
|
internal_router.Path("/wipeboltcard").Methods("GET").HandlerFunc(wipeboltcard)
|
||||||
|
|
||||||
port := db_get_setting("HOST_PORT")
|
port := db_get_setting("HOST_PORT")
|
||||||
if port == "" {
|
if port == "" {
|
||||||
port = "9000"
|
port = "9000"
|
||||||
}
|
}
|
||||||
|
|
||||||
srv := &http.Server{
|
external_server := &http.Server{
|
||||||
Handler: router,
|
Handler: external_router,
|
||||||
Addr: ":" + port, // consider adding host
|
Addr: ":" + port, // consider adding host
|
||||||
WriteTimeout: 30 * time.Second,
|
WriteTimeout: 30 * time.Second,
|
||||||
ReadTimeout: 30 * time.Second,
|
ReadTimeout: 30 * time.Second,
|
||||||
}
|
}
|
||||||
|
|
||||||
srv.ListenAndServe()
|
internal_server := &http.Server{
|
||||||
|
Handler: internal_router,
|
||||||
|
Addr: ":9001",
|
||||||
|
WriteTimeout: 5 * time.Second,
|
||||||
|
ReadTimeout: 5 * time.Second,
|
||||||
|
}
|
||||||
|
|
||||||
|
go external_server.ListenAndServe()
|
||||||
|
go internal_server.ListenAndServe()
|
||||||
|
|
||||||
|
select {}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
20
ping.go
Normal file
20
ping.go
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
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.WriteHeader(http.StatusOK)
|
||||||
|
jsonData := []byte(`{"status":"OK","pong":"` + message + `"}`)
|
||||||
|
w.Write(jsonData)
|
||||||
|
}
|
||||||
83
wipeboltcard.go
Normal file
83
wipeboltcard.go
Normal file
|
|
@ -0,0 +1,83 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
log "github.com/sirupsen/logrus"
|
||||||
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
)
|
||||||
|
|
||||||
|
type card_wipe_info struct {
|
||||||
|
id int
|
||||||
|
k0 string
|
||||||
|
k1 string
|
||||||
|
k2 string
|
||||||
|
k3 string
|
||||||
|
k4 string
|
||||||
|
uid string
|
||||||
|
}
|
||||||
|
|
||||||
|
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)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
card_name := r.URL.Query().Get("card_name")
|
||||||
|
|
||||||
|
// check if card_name has been given
|
||||||
|
|
||||||
|
if card_name == "" {
|
||||||
|
msg := "wipeboltcard: the card name must be set"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if card_name exists
|
||||||
|
|
||||||
|
card_count, err := db_get_card_name_count(card_name)
|
||||||
|
|
||||||
|
if card_count == 0 {
|
||||||
|
msg := "the card name does not exist in the database"
|
||||||
|
log.Warn(msg)
|
||||||
|
write_error_message(w, msg)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// set the card as wiped and disabled, get the keys
|
||||||
|
|
||||||
|
card_wipe_info_values, err := db_wipe_card(card_name)
|
||||||
|
if err != nil {
|
||||||
|
log.Warn(err.Error())
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// log the request
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"card_name": card_name}).Info("wipeboltcard API request")
|
||||||
|
|
||||||
|
// generate a response
|
||||||
|
|
||||||
|
jsonData := `{"status":"OK",` +
|
||||||
|
`"action": "wipe",` +
|
||||||
|
`"id": ` + strconv.Itoa(card_wipe_info_values.id) + `,` +
|
||||||
|
`"k0": "` + card_wipe_info_values.k0 + `",` +
|
||||||
|
`"k1": "` + card_wipe_info_values.k1 + `",` +
|
||||||
|
`"k2": "` + card_wipe_info_values.k2 + `",` +
|
||||||
|
`"k3": "` + card_wipe_info_values.k3 + `",` +
|
||||||
|
`"k4": "` + card_wipe_info_values.k4 + `",` +
|
||||||
|
`"uid": "` + card_wipe_info_values.uid + `",` +
|
||||||
|
`"version": 1}`
|
||||||
|
|
||||||
|
// log the response
|
||||||
|
|
||||||
|
log.WithFields(log.Fields{
|
||||||
|
"card_name": card_name, "response": jsonData}).Info("wipeboltcard API response")
|
||||||
|
|
||||||
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
w.Write([]byte(jsonData))
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue