Documentation
¶
Overview ¶
Package okay defines an OK type, which can be used to gate access to arbitrary resources.
An OK is composed of three elements, used for authentication, authorization, and expiration.
Index ¶
- Variables
- func Check(ctx context.Context, resource interface{}, ok ...OK) (bool, error)
- func WithCancel(ok OK) (OK, CancelFunc)
- type CancelFunc
- type OK
- func Allow(ok OK, allow func(resource interface{}) (allowed bool, err error)) OK
- func New() OK
- func Validate(ok OK, valid func() bool) OK
- func Verify(ok OK, verify func(context.Context) (valid bool, err error)) OK
- func WithContext(ok OK, ctx context.Context) OK
- func WithDeadline(ok OK, deadline time.Time) OK
- func WithTimeout(ok OK, timeout time.Duration) OK
Constants ¶
This section is empty.
Variables ¶
var Invalid = errors.New("OK not valid")
Functions ¶
func Check ¶
Check returns true if the given OKs are valid, verify the context, and allow the resource. It returns true if *any* of the passed OKs is valid.
func WithCancel ¶
func WithCancel(ok OK) (OK, CancelFunc)
WithCancel returns an OK that will expire when CancelFunc is called.
Types ¶
type CancelFunc ¶
type CancelFunc func()
CancelFunc immediately marks the associated OK invalid. Calls after the first have no effect.
type OK ¶
type OK interface {
// Valid reports whether this OK is still valid. Once an OK has been marked
// invalid (e.g. if it has been canceled) it must not become valid again.
Valid() bool
// Verify reports whether the given Context has a valid credential. If the
// given context has invalid credentials. ok must be false if either err is
// non-nil, but it is valid for ok to be false when err is nil.
Verify(ctx context.Context) (ok bool, err error)
// Allows reports whether this OK gates access to a given asset represented
// by the argument, such as a file path. If err is non-nil, ok must be
// false, but ok may be false while err is nil.
Allows(resource interface{}) (ok bool, err error)
}
An OK represents both an authentication and an authorization guarding some resource.
func Allow ¶
Allow returns an OK that calls the provided function whenever OK.Allow() is called. Multiple such functions may be attached by successive calls to this function. The functions are called in reverse order. If *any* such function returns allowed=true, Allow() will return (true, nil). The first function to return a non-nil error will have that error returned if no function returns true. It is valid for all functions to return (false, nil).
func New ¶
func New() OK
New returns an empty OK, which is always valid but allows nothing and verifies nobody.
func Validate ¶
Validate returns a new OK that will call the given function every time Valid() is called. It is possible to attach many such functions by repeated application of this function. All such functions must return true for Valid() to return true.
func Verify ¶
Verify returns a new OK that will call the given function when OK.Verify() is called. It is possible to attach multiple such functions by repeated calls to this function. Functions are called in reverse order. The first function to return valid=true will end the call chain and Valid() will return (true, nil). The first function to return a non-nil err will have that error returned if no subsequent function returns true. If *any* verify function returns true, Verify() will return (true, nil).
func WithContext ¶
WithContext returns an OK that will expire when the context is canceled.
func WithDeadline ¶
WithDeadline returns an OK that will expire once the deadline has passed.