Documentation
¶
Index ¶
- Constants
- Variables
- func CtxWithToken(ctx context.Context, t Token) context.Context
- func ResourceStrFromRef(ref dsref.Ref) string
- type Action
- type Actions
- type CtxKey
- type Effect
- type Policy
- type RawToken
- type RawTokens
- type Resource
- type Resources
- type Rule
- type Token
- type TokenClaims
- type TokenSource
- type TokenStore
Examples ¶
Constants ¶
const ( // EffectAllow describes a rule that adds permissions EffectAllow = Effect("allow") // EffectDeny describes a rule that removes permissions EffectDeny = Effect("deny") )
Variables ¶
var ( // ErrAccessDenied is returned by policy enforce ErrAccessDenied = fmt.Errorf("access denied") // DefaultAccessControlPolicyFilename is the file name for the policy // expected file is format yaml DefaultAccessControlPolicyFilename = "access_control_policy.yaml" )
var ( // Timestamp is a replacable function for getting the current time, // can be overridden for tests Timestamp = func() time.Time { return time.Now() } // ErrTokenNotFound is returned by stores that cannot find an access token // for a given key ErrTokenNotFound = errors.New("access token not found") // ErrInvalidToken indicates an access token is invalid ErrInvalidToken = errors.New("invalid access token") // DefaultTokenTTL is the default DefaultTokenTTL = time.Hour * 24 * 14 )
Functions ¶
func CtxWithToken ¶ added in v0.9.13
CtxWithToken adds a token value to a context
func ResourceStrFromRef ¶
ResourceStrFromRef takes a dsref.Ref and returns a string that can be parsed as a resource
Types ¶
type Action ¶
type Action []string
Action is a description of the action the Subject is attempting to take on the Resource
func MustParseAction ¶
MustParseAction parses a string into an Action. It panics if the string cannot be parsed correctly
func ParseAction ¶
ParseAction parses a string into an Action
func (Action) Contains ¶
Contains determines if the given action is described in the rule's Action it returns true if the action matches using the glob `*` pattern
func (Action) MarshalJSON ¶
MarshalJSON marshals the Action into a string separated by ":"
func (*Action) UnmarshalJSON ¶
UnmarshalJSON unmarshals the given slice of bytes into an Action
type CtxKey ¶ added in v0.9.13
type CtxKey string
CtxKey defines a distinct type for context keys used by the access package
const TokenCtxKey CtxKey = "Token"
TokenCtxKey is the key for adding an access token to a context.Context
type Policy ¶
type Policy []Rule
Policy is a set of rules
Example ¶
const examplePolicy = `
[
{
"title": "pull any dataset",
"effect": "allow",
"subject": "*",
"resources": [
"dataset:*"
],
"actions": [
"remote:pull"
]
},
{
"title": "push and delete user-owned datasets",
"effect": "allow",
"subject": "*",
"resources": [
"dataset:_subject:*"
],
"actions": [
"remote:push",
"remote:remove"
]
}
]
`
p := &Policy{}
if err := json.Unmarshal([]byte(examplePolicy), p); err != nil {
panic(err)
}
bob := &profile.Profile{
ID: profile.IDB58DecodeOrEmpty("QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt"),
Peername: "bob",
}
if err := p.Enforce(bob, "dataset:someone_else:world_bank_population", "remote:pull"); err == nil {
fmt.Println("bob can pull someone_else/world_bank_population")
}
if err := p.Enforce(bob, "dataset:bob:bobs_dataset", "remote:remove"); err == nil {
fmt.Println("bob can remote-delete his own dataset")
}
if err := p.Enforce(bob, "dataset:someone_else:dataset", "remote:remove"); err == ErrAccessDenied {
fmt.Println("bob can't remote-delete someone else's dataset")
}
Output: bob can pull someone_else/world_bank_population bob can remote-delete his own dataset bob can't remote-delete someone else's dataset
type RawTokens ¶ added in v0.9.13
type RawTokens []RawToken
RawTokens is a list of tokens that implements sorting by keys
type Resource ¶
type Resource []string
Resource is a stateful thing in qri
func MustParseResource ¶
MustParseResource wraps ParseResource, panics on error. Useful for tests
func ParseResource ¶
ParseResource constructs a resource from a string
func (Resource) Contains ¶
Contains determins if the subject is referenced in the resource returns true if the rule's resource contains the `matchAll` symbol and returns true if the rule's resource contains the `matchSubject` and the subjectUsername is in the given resource (allows us to create rules that say, "only allow subjects to do this action, if the resource matches the subject's name"
func (Resource) MarshalJSON ¶
MarshalJSON marshals the resource into a string separated by ":"
func (*Resource) UnmarshalJSON ¶
UnmarshalJSON unmarshals a slice of bytes into a Resource
type Rule ¶
type Rule struct {
Title string // human-legible title for the rule, informative only
Subject string // User this rule is about
Resources Resources // Thing being accessed. eg: a dataset,
Actions Actions // Thing user can do
Effect Effect // "allow" or "deny"
}
Rule is a permissions statement. It determines who (subject) can/can't (effect) do something (actions) to things (resources)
func (*Rule) UnmarshalJSON ¶
UnmarshalJSON unmarshals the slice of bytes into a Rule
type Token ¶ added in v0.9.13
Token abstracts a json web token
func ParseToken ¶ added in v0.9.13
func ParseToken(tokenString string, tokens TokenSource) (*Token, error)
ParseToken will parse, validate and return a token
func TokenFromCtx ¶ added in v0.9.13
TokenFromCtx extracts the JWT from a given context if one is set, returning nil otherwise
type TokenClaims ¶ added in v0.9.13
type TokenClaims struct {
*jwt.StandardClaims
Username string `json:"username"`
}
TokenClaims is a JWT Claims object
type TokenSource ¶ added in v0.9.13
type TokenSource interface {
CreateToken(pro *profile.Profile, ttl time.Duration) (string, error)
CreateTokenWithClaims(claims jwt.MapClaims, ttl time.Duration) (string, error)
// VerifyKey returns the verification key for a given token
VerificationKey(t *Token) (interface{}, error)
}
TokenSource creates tokens, and provides a verification key for all tokens it creates
implementations of TokenSource must conform to the assertion test defined in the spec subpackage
func NewPrivKeyTokenSource ¶ added in v0.9.13
func NewPrivKeyTokenSource(privKey crypto.PrivKey) (TokenSource, error)
NewPrivKeyTokenSource creates an authentication interface backed by a single private key. Intended for a node running as remote, or providing a public API
type TokenStore ¶ added in v0.9.13
type TokenStore interface {
PutToken(ctx context.Context, key, rawToken string) error
RawToken(ctx context.Context, key string) (rawToken string, err error)
DeleteToken(ctx context.Context, key string) (err error)
ListTokens(ctx context.Context, offset, limit int) (results []RawToken, err error)
}
TokenStore is a store intended for clients, who need to persist secret jwts given to them by other remotes for API access. It deals in raw, string-formatted json web tokens, which are more useful when working with APIs, but validates the tokens are well-formed when placed in the store
implementations of TokenStore must conform to the assertion test defined in the spec subpackage
func NewTokenStore ¶ added in v0.9.13
func NewTokenStore(filepath string, fs qfs.Filesystem) (TokenStore, error)
NewTokenStore creates a token store with a qfs.Filesystem