Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: make linters happy #732

Merged
merged 1 commit into from Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions authorization.go
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
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
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
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
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
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
}