Skip to content

Commit

Permalink
refactor: make linters happy (#732)
Browse files Browse the repository at this point in the history
  • Loading branch information
dunglas committed Jan 10, 2023
1 parent d12225c commit a65f175
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 24 deletions.
8 changes: 8 additions & 0 deletions authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/url"

"github.com/golang-jwt/jwt/v4"
"go.uber.org/zap"
)

// claims contains Mercure's JWT claims.
Expand Down Expand Up @@ -170,3 +171,10 @@ func canDispatch(s *TopicSelectorStore, topics, topicSelectors []string) bool {

return true
}

func (h *Hub) httpAuthorizationError(w http.ResponseWriter, r *http.Request, err error) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
if c := h.logger.Check(zap.InfoLevel, "Topic selectors not matched, not provided or authorization error"); c != nil {
c.Write(zap.String("remote_addr", r.RemoteAddr), zap.Error(err))
}
}
9 changes: 1 addition & 8 deletions bolt_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,14 +213,7 @@ func (t *BoltTransport) GetSubscribers() (string, []*Subscriber, error) {
t.RLock()
defer t.RUnlock()

var subscribers []*Subscriber
t.subscribers.Walk(0, func(s *Subscriber) bool {
subscribers = append(subscribers, s)

return true
})

return t.lastEventID, subscribers, nil
return t.lastEventID, getSubscribers(t.subscribers), nil
}

func (t *BoltTransport) dispatchHistory(s *Subscriber, toSeq uint64) {
Expand Down
9 changes: 1 addition & 8 deletions local_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,7 @@ func (t *LocalTransport) GetSubscribers() (string, []*Subscriber, error) {
t.RLock()
defer t.RUnlock()

var subscribers []*Subscriber
t.subscribers.Walk(0, func(s *Subscriber) bool {
subscribers = append(subscribers, s)

return true
})

return t.lastEventID, subscribers, nil
return t.lastEventID, getSubscribers(t.subscribers), nil
}

// Close closes the Transport.
Expand Down
5 changes: 1 addition & 4 deletions publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ func (h *Hub) PublishHandler(w http.ResponseWriter, r *http.Request) {
if h.publisherJWT != nil {
claims, err = authorize(r, h.publisherJWT, h.publishOrigins, h.cookieName)
if err != nil || claims == nil || claims.Mercure.Publish == nil {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
if c := h.logger.Check(zap.InfoLevel, "Topic selectors not matched, not provided or authorization error"); c != nil {
c.Write(zap.String("remote_addr", r.RemoteAddr), zap.Error(err))
}
h.httpAuthorizationError(w, r, err)

return
}
Expand Down
5 changes: 1 addition & 4 deletions subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,7 @@ func (h *Hub) initSubscription(currentURL string, w http.ResponseWriter, r *http
if h.subscriberJWT != nil {
claims, err := authorize(r, h.subscriberJWT, nil, h.cookieName)
if err != nil || claims == nil || claims.Mercure.Subscribe == nil || !canReceive(h.topicSelectorStore, []string{currentURL}, claims.Mercure.Subscribe) {
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusUnauthorized)
if c := h.logger.Check(zap.InfoLevel, "Topic selectors not matched, not provided or authorization error"); c != nil {
c.Write(zap.String("remote_addr", r.RemoteAddr), zap.Error(err))
}
h.httpAuthorizationError(w, r, err)

return "", nil, false
}
Expand Down
10 changes: 10 additions & 0 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,13 @@ func (e *TransportError) Error() string {
func (e *TransportError) Unwrap() error {
return e.err
}

func getSubscribers(sl *SubscriberList) (subscribers []*Subscriber) {
sl.Walk(0, func(s *Subscriber) bool {
subscribers = append(subscribers, s)

return true
})

return
}

0 comments on commit a65f175

Please sign in to comment.