permissions

package
v0.0.0-...-9818b01 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2017 License: AGPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// AppAudience is the audience for JWT used by client-side apps
	AppAudience = "app"

	// RegistrationTokenAudience is the audience field of JWT for registration tokens
	RegistrationTokenAudience = "registration"

	// AccessTokenAudience is the audience field of JWT for access tokens
	AccessTokenAudience = "access"

	// RefreshTokenAudience is the audience field of JWT for refresh tokens
	RefreshTokenAudience = "refresh"
)

#nosec

View Source
const (
	GET    = Verb("GET")
	POST   = Verb("POST")
	PUT    = Verb("PUT")
	PATCH  = Verb("PATCH")
	DELETE = Verb("DELETE")
)

All possible Verbs, a subset of http methods

Variables

View Source
var (
	// ErrInvalidToken is used when the token is invalid (the signature is not
	// correct, the domain is not the good one, etc.)
	ErrInvalidToken = echo.NewHTTPError(http.StatusBadRequest,
		"Invalid JWT token")

	// ErrInvalidAudience is used when the audience is not expected
	ErrInvalidAudience = echo.NewHTTPError(http.StatusBadRequest,
		"Invalid audience for JWT token")
)

ALL : the default VerbSet allows all Verbs

View Source
var Index = mango.IndexOnFields("application_id")

Index is the necessary index for this package used in instance creation

Functions

func Destroy

func Destroy(db couchdb.Database, slug string) error

Destroy removes Permission doc for a given app

Types

type Claims

type Claims struct {
	jwt.StandardClaims
	Scope string `json:"scope,omitempty"`
}

Claims is used for JWT used in OAuth2 flow and applications token

type Permission

type Permission struct {
	PID           string            `json:"_id,omitempty"`
	PRev          string            `json:"_rev,omitempty"`
	ApplicationID string            `json:"application_id"`
	Permissions   Set               `json:"permissions,omitempty"`
	ExpiresAt     int               `json:"expires_at,omitempty"`
	Codes         map[string]string `json:"codes,omitempty"`
}

Permission is a storable object containing a set of rules and several codes

func Create

func Create(db couchdb.Database, slug string, set Set) (*Permission, error)

Create creates a Permission doc for a given app

func GetForApp

func GetForApp(db couchdb.Database, slug string) (*Permission, error)

GetForApp retrieves the Permission doc for a given app

func (*Permission) DocType

func (p *Permission) DocType() string

DocType implements jsonapi.Doc

func (*Permission) ID

func (p *Permission) ID() string

ID implements jsonapi.Doc

func (*Permission) Included

func (p *Permission) Included() []jsonapi.Object

Included implements jsonapi.Doc

func (p *Permission) Links() *jsonapi.LinksList

Links implements jsonapi.Doc

func (*Permission) Relationships

func (p *Permission) Relationships() jsonapi.RelationshipMap

Relationships implements jsonapi.Doc

func (*Permission) Rev

func (p *Permission) Rev() string

Rev implements jsonapi.Doc

func (*Permission) SetID

func (p *Permission) SetID(id string)

SetID implements jsonapi.Doc

func (*Permission) SetRev

func (p *Permission) SetRev(rev string)

SetRev implements jsonapi.Doc

type Rule

type Rule struct {
	// Type is the JSON-API type or couchdb Doctype
	Type string `json:"type"`

	// Title is a human readable (i18n key) header for this rule
	Title string `json:"-"`

	// Description is a human readable (i18n key) purpose of this rule
	Description string `json:"description,omitempty"`

	// Verbs is a subset of http methods.
	Verbs VerbSet `json:"verbs,omitempty"`

	// Selector is the field which must be one of Values.
	Selector string   `json:"selector,omitempty"`
	Values   []string `json:"values,omitempty"`
}

Rule represent a single permissions rule, ie a Verb and a type

func UnmarshalRuleString

func UnmarshalRuleString(in string) (Rule, error)

UnmarshalRuleString parse a scope formated rule

func (Rule) MarshalScopeString

func (r Rule) MarshalScopeString() (string, error)

MarshalScopeString transform a Rule into a string of the shape io.cozy.files:GET:io.cozy.files.music-dir

func (Rule) SomeValue

func (r Rule) SomeValue(predicate func(v string) bool) bool

SomeValue returns true if any value statisfy the predicate

func (Rule) ValuesContain

func (r Rule) ValuesContain(value string) bool

ValuesContain returns true if the value is in r.Values

type Set

type Set []Rule

Set is a Set of rule

func UnmarshalScopeString

func UnmarshalScopeString(in string) (*Set, error)

UnmarshalScopeString parse a Scope string into a permission Set

func (Set) Allow

func (s Set) Allow(v Verb, o Validable) bool

Allow returns true if the set allows to apply verb to given doc

func (Set) AllowID

func (s Set) AllowID(v Verb, doctype, id string) bool

AllowID returns true if the set allows to apply verb to given type & id

func (Set) AllowWholeType

func (s Set) AllowWholeType(v Verb, doctype string) bool

AllowWholeType returns true if the set allows to apply verb to every document from the given doctypes (ie. r.values == 0)

func (Set) MarshalJSON

func (ps Set) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller on Set see docs/permission for structure

func (Set) MarshalScopeString

func (ps Set) MarshalScopeString() (string, error)

MarshalScopeString transforms a Set into a string for Oauth Scope (a space separated concatenation of its rules)

func (Set) Some

func (ps Set) Some(predicate func(Rule) bool) bool

Some returns true if the predicate return true for any of the rule.

func (*Set) UnmarshalJSON

func (ps *Set) UnmarshalJSON(j []byte) error

UnmarshalJSON parses a json formated permission set

type Validable

type Validable interface {
	ID() string
	DocType() string
	Valid(field, expected string) bool
}

Validable is an interface for a object than can be validated by a Set

type Verb

type Verb string

Verb is one of GET,POST,PUT,PATCH,DELETE

type VerbSet

type VerbSet map[Verb]struct{}

VerbSet is a Set of Verbs

func VerbSplit

func VerbSplit(in string) VerbSet

VerbSplit parse a string into a VerbSet

func Verbs

func Verbs(verbs ...Verb) VerbSet

Verbs is a utility function to create VerbSets

func (VerbSet) Contains

func (vs VerbSet) Contains(v Verb) bool

Contains check if VerbSet contains a Verb

func (VerbSet) MarshalJSON

func (vs VerbSet) MarshalJSON() ([]byte, error)

MarshalJSON implements json.Marshaller on VerbSet the VerbSet is converted to a json array

func (VerbSet) String

func (vs VerbSet) String() string

func (*VerbSet) UnmarshalJSON

func (vs *VerbSet) UnmarshalJSON(b []byte) error

UnmarshalJSON implements json.Unmarshaller on VerbSet it expects a json array

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL