Compare commits

...

2 commits

Author SHA1 Message Date
bfee9ce183 Add lnd env vars 2023-05-02 22:12:18 -04:00
3415b89c39 Fix up error responses 2023-05-02 22:11:53 -04:00
2 changed files with 30 additions and 4 deletions

View file

@ -7,6 +7,9 @@ import (
type (
Config struct {
ListenAddr string
LndCertB64 string
LndAddr string
LndMacaroonHex string
LogLevel string
MatrixIdentityServer string
MatrixWellKnownAddress string
@ -17,6 +20,9 @@ type (
func GetConfig() Config {
return Config{
ListenAddr: getEnv("LISTEN_ADDR", ":8090"),
LndCertB64: getEnv("LND_CERT_B64", ""),
LndAddr: getEnv("LND_ADDR", ""),
LndMacaroonHex: getEnv("LND_MACAROON_HEX", ""),
LogLevel: getEnv("LOG_LEVEL", "INFO"),
MatrixIdentityServer: getEnv("MATRIX_IDENTITY_SERVER", ""),
MatrixWellKnownAddress: getEnv("MATRIX_WELL_KNOWN_ADDRESS", ""),

View file

@ -21,6 +21,10 @@ type nostrWellKnown struct {
Relays map[string][]string `json:"relays,omitempty"`
}
type jsonErrorMessage struct {
Error string `json:"error"`
}
type NostrRequest struct {
Name string `json:"name"`
Key string `json:"key"`
@ -33,7 +37,6 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
l := logger.Get()
switch r.Method {
case "GET":
l.Debug().Msg("get nostr register form")
http.ServeFile(w, r, "html/nostr_form.html")
case "POST":
r.ParseForm()
@ -43,20 +46,28 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
exists := redisCli.Exists(ctx, rKey)
if exists.Val() == 1 {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username already registered",
})
return
}
rKey = getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
exists = redisCli.Exists(ctx, rKey)
if exists.Val() == 1 {
w.WriteHeader(http.StatusConflict)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username already requested, try again in a few minutes",
})
return
}
// get the hexkey
hexKey, err := convertNpubToHex(r.FormValue("Key"))
if err != nil {
l.Error().Msg("unable to convert npub to hex")
w.WriteHeader(http.StatusBadRequest)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: err.Error(),
})
return
}
@ -81,6 +92,9 @@ func RequestNostrAddr(w http.ResponseWriter, r *http.Request) {
_, err = lnd.Request(rKey, jsonUser, exp)
if err != nil {
w.WriteHeader(http.StatusServiceUnavailable)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "service unavailable",
})
return
}
requestKey := getRkey("requested", r.FormValue("Name"), getHostname(r.Host))
@ -105,11 +119,17 @@ func GetNostrAddr(w http.ResponseWriter, r *http.Request) {
addr, err := redisCli.Get(ctx, search).Result()
if err != nil {
w.WriteHeader(http.StatusNotFound)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "username not registered",
})
return
}
dec, err := b64.StdEncoding.DecodeString(addr)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
json.NewEncoder(w).Encode(&jsonErrorMessage{
Error: "internal server error",
})
return
}
@ -154,11 +174,11 @@ func getHostname(hostname string) string {
func convertNpubToHex(npub string) (string, error) {
if !strings.HasPrefix(npub, "npub1") {
return "", errors.New("Not an npub key")
return "", errors.New("key does not start with npub1 prefix")
}
_, hex, err := nip19.Decode(npub)
if err != nil {
return "", errors.New("Unable to decode npub")
return "", errors.New("unable to decode npub key")
}
return hex.(string), nil