scopes

Paddy 2015-12-05 Child:b93938562a17

0:b2ab1ab8f157 Go to Latest

scopes/scope.go

First pass at a scopes package. We need a minimal scopes package that we'll be able to share around. I don't want it embedded in auth, because then all the requirements of auth have to be pulled into whatever package wants to import type Scope, which seems silly. So this is the bare minimum of the beginning of a new service that separates Scopes out from auth.

History
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/scope.go	Sat Dec 05 14:54:57 2015 -0800
     1.3 @@ -0,0 +1,86 @@
     1.4 +package scopes
     1.5 +
     1.6 +import (
     1.7 +	"errors"
     1.8 +	"fmt"
     1.9 +
    1.10 +	"golang.org/x/net/context"
    1.11 +)
    1.12 +
    1.13 +var (
    1.14 +	ErrNoStorer      = errors.New("Storer not set in Context")
    1.15 +	ErrScopeNotFound = errors.New("Scope not found")
    1.16 +)
    1.17 +
    1.18 +type ErrScopeAlreadyExists string
    1.19 +
    1.20 +func (e ErrScopeAlreadyExists) Error() string {
    1.21 +	return fmt.Sprintf("scope %s already exists", string(e))
    1.22 +}
    1.23 +
    1.24 +// Scope represents a limit on the access that a grant provides.
    1.25 +type Scope struct {
    1.26 +	ID          string
    1.27 +	Name        string
    1.28 +	Description string
    1.29 +}
    1.30 +
    1.31 +func ApplyChange(change ScopeChange, scope Scope) Scope {
    1.32 +	changed := scope
    1.33 +	if change.Name != nil {
    1.34 +		changed.Name = *change.Name
    1.35 +	}
    1.36 +	if change.Description != nil {
    1.37 +		changed.Description = *change.Description
    1.38 +	}
    1.39 +	return changed
    1.40 +}
    1.41 +
    1.42 +type Scopes []Scope
    1.43 +
    1.44 +func (s Scopes) Len() int {
    1.45 +	return len(s)
    1.46 +}
    1.47 +
    1.48 +func (s Scopes) Swap(i, j int) {
    1.49 +	s[i], s[j] = s[j], s[i]
    1.50 +}
    1.51 +
    1.52 +func (s Scopes) Less(i, j int) bool {
    1.53 +	return s[i].ID < s[j].ID
    1.54 +}
    1.55 +
    1.56 +func (s Scopes) Strings() []string {
    1.57 +	res := make([]string, len(s))
    1.58 +	for pos, scope := range s {
    1.59 +		res[pos] = scope.ID
    1.60 +	}
    1.61 +	return res
    1.62 +}
    1.63 +
    1.64 +func stringsToScopes(s []string) Scopes {
    1.65 +	res := make(Scopes, len(s))
    1.66 +	for pos, scope := range s {
    1.67 +		res[pos] = Scope{ID: scope}
    1.68 +	}
    1.69 +	return res
    1.70 +}
    1.71 +
    1.72 +// ScopeChange represents a change to a Scope.
    1.73 +type ScopeChange struct {
    1.74 +	ScopeID     string
    1.75 +	Name        *string
    1.76 +	Description *string
    1.77 +}
    1.78 +
    1.79 +func (s ScopeChange) Empty() bool {
    1.80 +	return s.Name == nil && s.Description == nil
    1.81 +}
    1.82 +
    1.83 +type Storer interface {
    1.84 +	CreateScopes(scopes []Scope, ctx context.Context) error
    1.85 +	GetScopes(ids []string, ctx context.Context) (map[string]Scope, error)
    1.86 +	UpdateScope(change ScopeChange, ctx context.Context) error
    1.87 +	RemoveScopes(ids []string, ctx context.Context) error
    1.88 +	ListScopes(ctx context.Context) ([]Scope, error)
    1.89 +}