Skip to content

Commit

Permalink
fully working implicit auth with owners set
Browse files Browse the repository at this point in the history
also now returning correct machine prefix for ok,false on mute list matches for add event
  • Loading branch information
mleku committed Dec 6, 2024
1 parent 1081455 commit 85eb4cf
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 37 deletions.
31 changes: 15 additions & 16 deletions cmd/realy/app/implementation.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,17 @@ func (r *Relay) Init() (err er) {
}
return fmt.Sprintf("%v", ownerIds)
})
r.ZeroLists()
r.CheckOwnerLists(context.Bg())
return nil
}

func (r *Relay) ZeroLists() {
r.Followed = make(map[st]struct{})
r.OwnersFollowed = make(map[st]struct{})
r.OwnersFollowLists = r.OwnersFollowLists[:0]
r.Muted = make(map[st]struct{})
r.CheckOwnerLists(context.Bg())
return nil
r.OwnersMuteLists = r.OwnersMuteLists[:0]
}

func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
Expand Down Expand Up @@ -92,11 +98,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
for o := range r.OwnersFollowed {
if equals(by(o), evt.PubKey) {
return true, "", func() {
r.Followed = make(map[st]struct{})
r.OwnersFollowed = make(map[st]struct{})
r.OwnersFollowLists = r.OwnersFollowLists[:0]
r.Muted = make(map[st]struct{})
r.OwnersMuteLists = r.OwnersMuteLists[:0]
r.ZeroLists()
r.CheckOwnerLists(context.Bg())
}
}
Expand All @@ -107,11 +109,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
for _, o := range r.Owners {
if equals(o, evt.PubKey) {
return true, "", func() {
r.Followed = make(map[st]struct{})
r.OwnersFollowed = make(map[st]struct{})
r.OwnersFollowLists = r.OwnersFollowLists[:0]
r.Muted = make(map[st]struct{})
r.OwnersMuteLists = r.OwnersMuteLists[:0]
r.ZeroLists()
r.CheckOwnerLists(context.Bg())
}
}
Expand All @@ -128,7 +126,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
tt := tag.New(append(r.OwnersFollowLists, r.OwnersMuteLists...)...)
if evt.Tags.ContainsAny(by("e"), tt) {
return false,
"cannot delete owner's follow, owners's follows follow or mute events",
"cannot delete owner's follow, owners' follows follow or mute events",
nil
}
// next, check all a tags present are not follow/mute lists of the owners
Expand Down Expand Up @@ -173,7 +171,7 @@ func (r *Relay) AcceptEvent(c cx, evt *event.T, hr *http.Request, origin st,
// they come from a pubkey that is on the follow list.
for pk := range r.Muted {
if equals(evt.PubKey, by(pk)) {
return false, "rejecting event with pubkey " + st(evt.PubKey) +
return false, "rejecting event with pubkey " + hex.Enc(evt.PubKey) +
" because on owner mute list", nil
}
}
Expand Down Expand Up @@ -201,6 +199,7 @@ func (r *Relay) AcceptReq(c cx, hr *http.Request, id by, ff *filters.T,
authedPubkey by) (allowed *filters.T, ok bo) {
// if the authenticator is enabled we require auth to process requests
if !r.AuthEnabled() {
allowed = ff
ok = true
return
}
Expand Down Expand Up @@ -356,12 +355,12 @@ func (r *Relay) CheckOwnerLists(c cx) {
}
}

func (r *Relay) AuthEnabled() bo { return r.C.AuthRequired }
func (r *Relay) AuthEnabled() bo { return r.AuthRequired || len(r.Owners) > 0 }

// ServiceUrl returns the address of the relay to send back in auth responses.
// If auth is disabled this returns an empty string.
func (r *Relay) ServiceUrl(req *http.Request) (s st) {
if !r.C.AuthRequired {
if !r.AuthEnabled() {
return
}
host := req.Header.Get("X-Forwarded-Host")
Expand Down
4 changes: 4 additions & 0 deletions number/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ import "fmt"

type List []no

func (l List) Len() int { return len(l) }
func (l List) Less(i, j int) bool { return l[i] < l[j] }
func (l List) Swap(i, j int) { l[i], l[j] = l[j], l[i] }

// HasNumber returns true if the list contains a given number
func (l List) HasNumber(n no) (idx no, has bo) {
for idx = range l {
Expand Down
4 changes: 4 additions & 0 deletions realy/handleAdmin.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ func (s *Server) handleAdmin(w http.ResponseWriter, r *http.Request) {
sto := s.relay.Storage(context.Bg())
read := io.LimitReader(r.Body, r.ContentLength)
sto.Import(read)
if realy, ok := s.relay.(*app.Relay); ok {
realy.ZeroLists()
realy.CheckOwnerLists(context.Bg())
}
case strings.HasPrefix(r.URL.Path, "/shutdown"):
if ok := s.auth(r); !ok {
s.unauthorized(w)
Expand Down
2 changes: 1 addition & 1 deletion realy/handleCount.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (s *Server) handleCount(c context.T, ws *web.Socket, req by, store store.I)
return normalize.Restricted.F("this relay does not support NIP-45")
}
if ws.AuthRequested() && len(ws.Authed()) == 0 {
return
return by("awaiting auth for count")
}
var err er
var rem by
Expand Down
46 changes: 28 additions & 18 deletions realy/handleEvent.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package realy

import (
"bytes"
"strings"

"realy.lol/envelopes/authenvelope"
"realy.lol/envelopes/eventenvelope"
Expand All @@ -21,6 +22,9 @@ import (

func (s *Server) handleEvent(c cx, ws *web.Socket, req by, sto store.I) (msg by) {
log.T.F("handleEvent %s %s", ws.RealRemote(), req)
if ws.AuthRequested() && len(ws.Authed()) == 0 {
return by("awaiting auth for event")
}
var err er
var ok bo
var rem by
Expand All @@ -35,27 +39,33 @@ func (s *Server) handleEvent(c cx, ws *web.Socket, req by, sto store.I) (msg by)
accept, notice, after := s.relay.AcceptEvent(c, env.T, ws.Req(), ws.RealRemote(),
by(ws.Authed()))
if !accept {
var auther relay.Authenticator
if auther, ok = s.relay.(relay.Authenticator); ok && auther.AuthEnabled() {
if !ws.AuthRequested() {
if err = okenvelope.NewFrom(env.ID, false,
normalize.AuthRequired.F("auth required for request processing")).Write(ws); chk.T(err) {
}
log.T.F("requesting auth from client %s", ws.RealRemote())
if err = authenvelope.NewChallengeWith(ws.Challenge()).Write(ws); chk.T(err) {
if strings.Contains(notice, "mute") {
if err = okenvelope.NewFrom(env.ID, false,
normalize.Blocked.F(notice)).Write(ws); chk.T(err) {
}
} else {
var auther relay.Authenticator
if auther, ok = s.relay.(relay.Authenticator); ok && auther.AuthEnabled() {
if !ws.AuthRequested() {
if err = okenvelope.NewFrom(env.ID, false,
normalize.AuthRequired.F("auth required for request processing")).Write(ws); chk.T(err) {
}
log.T.F("requesting auth from client %s", ws.RealRemote())
if err = authenvelope.NewChallengeWith(ws.Challenge()).Write(ws); chk.T(err) {
return
}
ws.RequestAuth()
return
}
ws.RequestAuth()
return
} else {
if err = okenvelope.NewFrom(env.ID, false,
normalize.AuthRequired.F("auth required for storing events")).Write(ws); chk.T(err) {
}
log.T.F("requesting auth again from client %s", ws.RealRemote())
if err = authenvelope.NewChallengeWith(ws.Challenge()).Write(ws); chk.T(err) {
} else {
if err = okenvelope.NewFrom(env.ID, false,
normalize.AuthRequired.F("auth required for storing events")).Write(ws); chk.T(err) {
}
log.T.F("requesting auth again from client %s", ws.RealRemote())
if err = authenvelope.NewChallengeWith(ws.Challenge()).Write(ws); chk.T(err) {
return
}
return
}
return
}
}
if err = okenvelope.NewFrom(env.ID, false,
Expand Down
2 changes: 1 addition & 1 deletion realy/handleReq.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (

func (s *Server) handleReq(c cx, ws *web.Socket, req by, sto store.I) (r by) {
if ws.AuthRequested() && len(ws.Authed()) == 0 {
return
return by("awaiting auth for req")
}
var err er
var rem by
Expand Down
2 changes: 1 addition & 1 deletion realy/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v1.2.37
v1.2.39
2 changes: 2 additions & 0 deletions relayinfo/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"os"
"sort"
"sync"

"realy.lol/kinds"
Expand All @@ -22,6 +23,7 @@ func GetList(items ...NIP) (n number.List) {
for _, item := range items {
n = append(n, item.N())
}
sort.Sort(n)
return
}

Expand Down

0 comments on commit 85eb4cf

Please sign in to comment.