forked from boltcard/boltcard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew_card_request.go
101 lines (86 loc) · 2.69 KB
/
new_card_request.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package main
import (
"database/sql"
"encoding/json"
"github.com/boltcard/boltcard/db"
"github.com/boltcard/boltcard/resp_err"
log "github.com/sirupsen/logrus"
"net/http"
)
/**
* @api {get} /new/:a Request information to create a new bolt card
* @apiName NewBoltCard
* @apiGroup BoltCardService
*
* @apiParam {String} a one time authentication code
*
* @apiSuccess {String} protocol_name name of the protocol message
* @apiSuccess {Int} protocol_version version of the protocol message
* @apiSuccess {String} card_name user friendly card name
* @apiSuccess {String} lnurlw_base base for creating the lnurlw on the card
* @apiSuccess {String} k0 Key 0 - authorisation key
* @apiSuccess {String} k1 Key 1 - decryption key
* @apiSuccess {String} k2 Key 2 - authentication key
* @apiSuccess {String} k3 Key 3 - NXP documents say this must be set
* @apiSuccess {String} k4 Key 4 - NXP documents say this must be set
* @apiSuccess {String} uid_privacy - set up the card for the UID to be private
*/
type NewCardResponse struct {
PROTOCOL_NAME string `json:"protocol_name"`
PROTOCOL_VERSION int `json:"protocol_version"`
CARD_NAME string `json:"card_name"`
LNURLW_BASE string `json:"lnurlw_base"`
K0 string `json:"k0"`
K1 string `json:"k1"`
K2 string `json:"k2"`
K3 string `json:"k3"`
K4 string `json:"k4"`
UID_PRIVACY string `json:"uid_privacy"`
}
func new_card_request(w http.ResponseWriter, req *http.Request) {
url := req.URL.RequestURI()
log.Debug("new_card url: ", url)
params_a, ok := req.URL.Query()["a"]
if !ok || len(params_a[0]) < 1 {
log.Debug("a not found")
resp_err.Write(w)
return
}
a := params_a[0]
lnurlw_base := "lnurlw://" + db.Get_setting("HOST_DOMAIN") + "/ln"
c, err := db.Get_new_card(a)
if err == sql.ErrNoRows {
log.Debug(err)
resp_err.Write_message(w, "one time code was used or card was wiped or card does not exist")
return
}
if err != nil {
log.Warn(err)
resp_err.Write(w)
return
}
k1_decrypt_key := db.Get_setting("AES_DECRYPT_KEY")
response := NewCardResponse{}
response.PROTOCOL_NAME = "create_bolt_card_response"
response.PROTOCOL_VERSION = 2
response.CARD_NAME = c.Card_name
response.LNURLW_BASE = lnurlw_base
response.K0 = c.K0_auth_key
response.K1 = k1_decrypt_key
response.K2 = c.K2_cmac_key
response.K3 = c.K3
response.K4 = c.K4
response.UID_PRIVACY = c.Uid_privacy
log.SetFormatter(&log.JSONFormatter{
DisableHTMLEscape: true,
})
jsonData, err := json.Marshal(response)
if err != nil {
log.Warn(err)
resp_err.Write(w)
return
}
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
w.Write(jsonData)
}