Skip to content

Commit

Permalink
Merge pull request #11 from Comcast/default-authorize
Browse files Browse the repository at this point in the history
Default authorize
  • Loading branch information
johnabass authored Apr 11, 2019
2 parents 3fd9069 + 9705427 commit f2c8d76
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 8 deletions.
42 changes: 34 additions & 8 deletions bascule/basculehttp/enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ import (
"github.com/Comcast/comcast-bascule/bascule"
)

//go:generate stringer -type=NotFoundBehavior

type NotFoundBehavior int

// Behavior on not found
const (
Forbid NotFoundBehavior = iota
Allow
)

type enforcer struct {
rules map[bascule.Authorization]bascule.Validators
notFoundBehavior NotFoundBehavior
rules map[bascule.Authorization]bascule.Validators
}

func (e *enforcer) decorate(next http.Handler) http.Handler {
Expand All @@ -20,20 +31,35 @@ func (e *enforcer) decorate(next http.Handler) http.Handler {
}
rules, ok := e.rules[auth.Authorization]
if !ok {
response.WriteHeader(http.StatusForbidden)
return
}
err := rules.Check(ctx, auth.Token)
if err != nil {
WriteResponse(response, http.StatusUnauthorized, err)
return
switch e.notFoundBehavior {
case Forbid:
response.WriteHeader(http.StatusForbidden)
return
case Allow:
// continue
default:
response.WriteHeader(http.StatusForbidden)
return
}
} else {
err := rules.Check(ctx, auth.Token)
if err != nil {
WriteResponse(response, http.StatusUnauthorized, err)
return
}
}
next.ServeHTTP(response, request)
})
}

type EOption func(*enforcer)

func WithNotFoundBehavior(behavior NotFoundBehavior) EOption {
return func(e *enforcer) {
e.notFoundBehavior = behavior
}
}

func WithRules(key bascule.Authorization, v bascule.Validators) EOption {
return func(e *enforcer) {
e.rules[key] = v
Expand Down
16 changes: 16 additions & 0 deletions bascule/basculehttp/notfoundbehavior_string.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions bascule/checks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ const (
capabilitiesKey = "capabilities"
)

func CreateAllowAllCheck() ValidatorFunc {
return func(_ context.Context, _ Token) error {
return nil
}
}

func CreateValidTypeCheck(validTypes []string) ValidatorFunc {
return func(_ context.Context, token Token) error {
tt := token.Type()
Expand Down Expand Up @@ -65,3 +71,19 @@ func CreateListAttributeCheck(key string, checks ...func(context.Context, []inte
return errs
}
}

func NonEmptyStringListCheck(ctx context.Context, vals []interface{}) error {
if len(vals) == 0 {
return errors.New("expected at least one value")
}
for _, val := range vals {
str, ok := val.(string)
if !ok {
return errors.New("expected value to be a string")
}
if len(str) == 0 {
return errors.New("expected string to be nonempty")
}
}
return nil
}

0 comments on commit f2c8d76

Please sign in to comment.