Skip to content

Commit

Permalink
separate rules and auth
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Dec 12, 2020
1 parent 202338b commit de4f3ee
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 22 deletions.
16 changes: 10 additions & 6 deletions auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,24 @@ type Auth interface {
Options() Options
// Generate a new account
Generate(id string, opts ...GenerateOption) (*Account, error)
// Verify an account has access to a resource using the rules
Verify(acc *Account, res *Resource, opts ...VerifyOption) error
// Inspect a token
Inspect(token string) (*Account, error)
// Token generated using refresh token or credentials
Token(opts ...TokenOption) (*Token, error)
// String returns the name of the implementation
String() string
}

// Rules manages access to resources
type Rules interface {
// Verify an account has access to a resource using the rules
Verify(acc *Account, res *Resource, opts ...VerifyOption) error
// Grant access to a resource
Grant(rule *Rule) error
// Revoke access to a resource
Revoke(rule *Rule) error
// Rules returns all the rules used to verify requests
Rules(...RulesOption) ([]*Rule, error)
// String returns the name of the implementation
String() string
// List returns all the rules used to verify requests
List(...ListOption) ([]*Rule, error)
}

// Account provided by an auth provider
Expand Down
20 changes: 13 additions & 7 deletions auth/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,16 @@ func NewAuth(opts ...Option) Auth {
}
}

func NewRules() Rules {
return new(noopRules)
}

type noop struct {
opts Options
}

type noopRules struct{}

// String returns the name of the implementation
func (n *noop) String() string {
return "noop"
Expand Down Expand Up @@ -55,25 +61,25 @@ func (n *noop) Generate(id string, opts ...GenerateOption) (*Account, error) {
}

// Grant access to a resource
func (n *noop) Grant(rule *Rule) error {
func (n *noopRules) Grant(rule *Rule) error {
return nil
}

// Revoke access to a resource
func (n *noop) Revoke(rule *Rule) error {
func (n *noopRules) Revoke(rule *Rule) error {
return nil
}

// Rules used to verify requests
func (n *noop) Rules(opts ...RulesOption) ([]*Rule, error) {
return []*Rule{}, nil
}

// Verify an account has access to a resource
func (n *noop) Verify(acc *Account, res *Resource, opts ...VerifyOption) error {
func (n *noopRules) Verify(acc *Account, res *Resource, opts ...VerifyOption) error {
return nil
}

func (n *noopRules) List(opts ...ListOption) ([]*Rule, error) {
return []*Rule{}, nil
}

// Inspect a token
func (n *noop) Inspect(token string) (*Account, error) {
return &Account{ID: uuid.New().String(), Issuer: n.Options().Namespace}, nil
Expand Down
17 changes: 12 additions & 5 deletions auth/jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ func NewAuth(opts ...auth.Option) auth.Auth {
return j
}

func NewRules() auth.Rules {
return new(jwtRules)
}

type jwt struct {
sync.Mutex
options auth.Options
jwt token.Provider
rules []*auth.Rule
}

type jwtRules struct {
sync.Mutex
rules []*auth.Rule
}

func (j *jwt) String() string {
Expand Down Expand Up @@ -70,14 +77,14 @@ func (j *jwt) Generate(id string, opts ...auth.GenerateOption) (*auth.Account, e
return account, nil
}

func (j *jwt) Grant(rule *auth.Rule) error {
func (j *jwtRules) Grant(rule *auth.Rule) error {
j.Lock()
defer j.Unlock()
j.rules = append(j.rules, rule)
return nil
}

func (j *jwt) Revoke(rule *auth.Rule) error {
func (j *jwtRules) Revoke(rule *auth.Rule) error {
j.Lock()
defer j.Unlock()

Expand All @@ -92,7 +99,7 @@ func (j *jwt) Revoke(rule *auth.Rule) error {
return nil
}

func (j *jwt) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error {
func (j *jwtRules) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyOption) error {
j.Lock()
defer j.Unlock()

Expand All @@ -104,7 +111,7 @@ func (j *jwt) Verify(acc *auth.Account, res *auth.Resource, opts ...auth.VerifyO
return auth.Verify(j.rules, acc, res)
}

func (j *jwt) Rules(opts ...auth.RulesOption) ([]*auth.Rule, error) {
func (j *jwtRules) List(opts ...auth.ListOption) ([]*auth.Rule, error) {
j.Lock()
defer j.Unlock()
return j.rules, nil
Expand Down
8 changes: 4 additions & 4 deletions auth/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,14 +219,14 @@ func VerifyContext(ctx context.Context) VerifyOption {
}
}

type RulesOptions struct {
type ListOptions struct {
Context context.Context
}

type RulesOption func(o *RulesOptions)
type ListOption func(o *ListOptions)

func RulesContext(ctx context.Context) RulesOption {
return func(o *RulesOptions) {
func RulesContext(ctx context.Context) ListOption {
return func(o *ListOptions) {
o.Context = ctx
}
}

0 comments on commit de4f3ee

Please sign in to comment.