Documentation ¶
Index ¶
- Variables
- func EndShastream(length int) []byte
- type Alternative
- type Restriction
- type Rune
- func NewMasterRune(secret []byte, id, version string) (*Rune, error)
- func NewRuneFromAuthbase(authbase []byte, uniqueId, version string, restrictions []Restriction) (*Rune, error)
- func RuneFromAuthcode(authcode []byte, restrictions []Restriction) (*Rune, error)
- func RuneFromEncodedString(encodedString string) (*Rune, error)
- func RuneFromString(runeString string) (*Rune, error)
- func (r *Rune) AddRestriction(restriction Restriction) error
- func (r *Rune) AreRestrictionsMet(tests map[string]Test) error
- func (r *Rune) Authcode() []byte
- func (r *Rune) Check(encodedRune string, tests map[string]Test) error
- func (r *Rune) Clone() (*Rune, error)
- func (r *Rune) Encode() string
- func (r *Rune) IsRuneAuthorized(otherRune *Rune) bool
- func (r *Rune) String() string
- type Test
- type TestFunc
Constants ¶
This section is empty.
Variables ¶
var ( Punctuation = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~" ErrInvalidField = errors.New("field not valid") ErrInvalidCondition = errors.New("condition not valid") ErrMissingField = errors.New("missing field in test") ErrFieldIsPresent = errors.New("field is present") ErrForbiddenValue = errors.New("forbidden value") ErrInvalidValuePrefix = errors.New("value has invalid perfix") ErrInvalidValueSuffix = errors.New("value has invalid suffix") ErrValueDoesntContain = errors.New("value does not contain substring") ErrValueTooLarge = errors.New("value too large") ErrValueTooSmall = errors.New("value too small") ErrWrongLexicOrder = errors.New("wrong lexicographical order") ErrNoRestrictions = errors.New("no restrictions") ErrNoOperator = errors.New("restriction contains no operator") ErrIdFieldHasAlts = errors.New("unique_id field can't have alternatives") ErrExtraChars = errors.New("restriction has extra ending characters") ErrInvalidRunePrefix = errors.New("rune strings must start with 64 hex digits then '-'") ErrSecretTooLarge = errors.New("secret too large") ErrCondValueTypeMismatch = errors.New("condition and test value type mismatch") ErrInvalidUniqueIdCond = errors.New("unique_id condition must be '='") ErrIdUknownVersion = errors.New("unique_id unknown version") ErrIdHasHyphens = errors.New("hyphen not allowed in unique_id") ErrIdFieldForbidden = errors.New("unique_id fiield not valid here") )
Functions ¶
func EndShastream ¶
EndShastream simulates a SHA-256 ending pad
Types ¶
type Alternative ¶
func NewAlternative ¶
func NewAlternative(field, cond, value string, allowIdField bool) (*Alternative, error)
NewAlternative creates a new Alternative
func (*Alternative) String ¶
func (a *Alternative) String() string
String formats the alternative into a string
type Restriction ¶
type Restriction []*Alternative
func RestrictionFromString ¶
func RestrictionFromString(encodedString string, allowIdField bool) (Restriction, error)
RestrictionFromString creates restrictions from an escaped string
func UniqueIdRestriction ¶
func UniqueIdRestriction(id, version string) (Restriction, error)
UniqueIdRestriction creates a unique Id restriction
func (Restriction) String ¶
func (r Restriction) String() string
String formats the restriction into a string
type Rune ¶
type Rune struct { Restrictions []Restriction // contains filtered or unexported fields }
func NewMasterRune ¶
NewMasterRune creates a new master rune
func NewRuneFromAuthbase ¶
func NewRuneFromAuthbase(authbase []byte, uniqueId, version string, restrictions []Restriction) (*Rune, error)
NewRuneFromAuthbase creates a new rune from a given authbase and list of restrictions
func RuneFromAuthcode ¶
func RuneFromAuthcode(authcode []byte, restrictions []Restriction) (*Rune, error)
RuneFromAuthcode parses a rune from a given authcode and a list of restrictions
func RuneFromEncodedString ¶
RuneFromEncodedString parses a rune from an encoded rune string
func RuneFromString ¶
RuneFromString parses a rune from a rune string
func (*Rune) AddRestriction ¶
func (r *Rune) AddRestriction(restriction Restriction) error
AddRestrictions adds new restrictions to the rune
func (*Rune) AreRestrictionsMet ¶
AreRestrictionsMet tests the rune restrictions. If any fail, returns an error
func (*Rune) Check ¶
Check checks if a given rune is authorized by the parent rune and passes the given tests
func (*Rune) IsRuneAuthorized ¶
IsRuneAuthorized checks whether or not a given rune has been authorized by the master rune
type Test ¶
type Test struct { Value interface{} TestFunc TestFunc }
Test is a struct made for passing a value and a test function for Restriction testing
type TestFunc ¶
type TestFunc func(alt *Alternative, v interface{}) error
TestFunc is a type for creating custom tests for restrictions
var StandardTestFunc TestFunc = func(alt *Alternative, v interface{}) error { switch value := v.(type) { case string: switch alt.Condition { case "!": return fmt.Errorf("%s: %w", alt.Field, ErrFieldIsPresent) case "=": if alt.Value != value { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "/": if alt.Value == value { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "^": if !strings.HasPrefix(value, alt.Value) { return fmt.Errorf("%s: %w", alt.Value, ErrInvalidValuePrefix) } case "$": if !strings.HasSuffix(value, alt.Value) { return fmt.Errorf("%s: %w", alt.Value, ErrInvalidValueSuffix) } case "~": if !strings.Contains(value, alt.Value) { return fmt.Errorf("%s: %w", alt.Value, ErrValueDoesntContain) } case "{": if !(value < alt.Value) { return fmt.Errorf("%s: %w", alt.Value, ErrWrongLexicOrder) } case "}": if !(value > alt.Value) { return fmt.Errorf("%s: %w", alt.Value, ErrWrongLexicOrder) } case "<": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } vInt, err := strconv.ParseInt(value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(vInt < valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooLarge) } case ">": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } vInt, err := strconv.ParseInt(value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(vInt > valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooSmall) } default: return fmt.Errorf("%s & %T: %w", alt.Condition, value, ErrCondValueTypeMismatch) } case int: switch alt.Condition { case "=": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if int64(value) != valueAsInt { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "/": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if int64(value) == valueAsInt { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "<": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(int64(value) < valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooLarge) } case ">": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(int64(value) > valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooSmall) } default: return fmt.Errorf("%s & %T: %w", alt.Condition, value, ErrCondValueTypeMismatch) } case int64: switch alt.Condition { case "=": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if value != valueAsInt { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "/": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if value == valueAsInt { return fmt.Errorf("%s: %w", alt.Value, ErrForbiddenValue) } case "<": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(value < valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooLarge) } case ">": valueAsInt, err := strconv.ParseInt(alt.Value, 10, 64) if err != nil { return fmt.Errorf("%s: %w", alt.Value, err) } if !(value > valueAsInt) { return fmt.Errorf("%s: %w", alt.Value, ErrValueTooSmall) } default: return fmt.Errorf("%s & %T: %w", alt.Condition, value, ErrCondValueTypeMismatch) } } return nil }
TODO - Finish covering all base type cases