Skip to content

Commit

Permalink
authorize topics with + wildcard:
Browse files Browse the repository at this point in the history
  • Loading branch information
henrod committed Jul 19, 2017
1 parent 358187e commit b5fc70f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 25 deletions.
Binary file added app/.helpers.go.swp
Binary file not shown.
Binary file added app/.histories.go.swp
Binary file not shown.
Binary file added app/.histories_test.go.swp
Binary file not shown.
Binary file added app/.history_test.go.swp
Binary file not shown.
31 changes: 31 additions & 0 deletions app/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
package app

import (
"fmt"
"strings"

"github.com/labstack/echo"
newrelic "github.com/newrelic/go-agent"
)
Expand All @@ -32,3 +35,31 @@ func WithSegment(name string, c echo.Context, f func() error) error {
defer segment.End()
return f()
}

func authenticate(app *App, userID string, topics ...string) (bool, []interface{}, error) {
rc := app.RedisClient.Pool.Get()
defer rc.Close()
rc.Send("MULTI")
rc.Send("GET", userID)
for _, topic := range topics {
rc.Send("GET", fmt.Sprintf("%s-%s", userID, topic))

pieces := strings.Split(topic, "/")
pieces[len(pieces)-1] = "+"
wildtopic := strings.Join(pieces, "/")
rc.Send("GET", fmt.Sprintf("%s-%s", userID, wildtopic))
}
r, err := rc.Do("EXEC")
if err != nil {
return false, nil, err
}
authorizedTopics := []interface{}{}
redisResults := (r.([]interface{}))
for i, redisResp := range redisResults[1:] {
if redisResp != nil {
authorizedTopics = append(authorizedTopics, topics[i/2])
}
}

return redisResults[0] != nil && len(authorizedTopics) > 0, authorizedTopics, nil
}
20 changes: 4 additions & 16 deletions app/histories.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func HistoriesHandler(app *App) func(c echo.Context) error {
esclient := es.GetESClient()
c.Set("route", "Histories")
topicPrefix := c.ParamValues()[0]
authorizedTopics := []interface{}{}
userID := c.QueryParam("userid")
topicsSuffix := strings.Split(c.QueryParam("topics"), ",")
topics := make([]string, len(topicsSuffix))
Expand All @@ -34,25 +33,14 @@ func HistoriesHandler(app *App) func(c echo.Context) error {
}

logger.Logger.Debugf("user %s is asking for histories for topicPrefix %s with args topics=%s from=%d and limit=%d", userID, topicPrefix, topics, from, limit)
rc := app.RedisClient.Pool.Get()
defer rc.Close()
rc.Send("MULTI")
rc.Send("GET", userID)
for _, topic := range topics {
rc.Send("GET", fmt.Sprintf("%s-%s", userID, topic))
}
r, err := rc.Do("EXEC")
authenticated, authorizedTopics, err := authenticate(app, userID, topics...)
if err != nil {
return err
}
redisResults := (r.([]interface{}))
for i, redisResp := range redisResults[1:] {
if redisResp != nil {
authorizedTopics = append(authorizedTopics, topics[i])
}
}

if redisResults[0] != nil && len(authorizedTopics) > 0 {
fmt.Println("henrod", authorizedTopics)

if authenticated {
boolQuery := elastic.NewBoolQuery()
topicBoolQuery := elastic.NewBoolQuery()
topicBoolQuery.Should(elastic.NewTermsQuery("topic", authorizedTopics...))
Expand Down
13 changes: 4 additions & 9 deletions app/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,13 @@ func HistoryHandler(app *App) func(c echo.Context) error {
limit = 10
}

logger.Logger.Debugf("user %s is asking for history for topic %s with args from=%d and limit=%d", userID, topic, from, limit)
rc := app.RedisClient.Pool.Get()
defer rc.Close()
rc.Send("MULTI")
rc.Send("GET", userID)
rc.Send("GET", fmt.Sprintf("%s-%s", userID, topic))
r, err := rc.Do("EXEC")
authenticated, _, err := authenticate(app, userID, topic)
if err != nil {
return err
}
redisResults := (r.([]interface{}))
if redisResults[0] != nil && redisResults[1] != nil {

logger.Logger.Debugf("user %s is asking for history for topic %s with args from=%d and limit=%d", userID, topic, from, limit)
if authenticated {
boolQuery := elastic.NewBoolQuery()
termQuery := elastic.NewTermQuery("topic", topic)
boolQuery.Must(termQuery)
Expand All @@ -75,6 +69,7 @@ func HistoryHandler(app *App) func(c echo.Context) error {
}
return c.JSON(http.StatusOK, messages)
}

return c.String(echo.ErrUnauthorized.Code, echo.ErrUnauthorized.Message)
}
}
Expand Down

0 comments on commit b5fc70f

Please sign in to comment.