auth
57:e45bfa2abc00
Go to Latest
auth/grant.go
The great documentation and exported interface cleanup.
Modify all our *Store interfaces to be unexported, as there's no real good
reason they need to be exported, especially as they can be implemented without
being exported. The interfaces shouldn't matter to 99% of users of the package,
so let's not pollute our package API.
Further, all methods of the interfaces are now unexported, for pretty much the
same reasoning.
Add a doc.go file with documentation explaining the choices the package is
making and what it provides.
Implement documentation on all our exported types and methods and functions,
which makes golint happy. The only remaining golint warning is about
NewMemstore, which will stay the way it is. The memstore type is useful outside
tests for things like standing up a server quickly when we don't care about the
storage, and because the type is unexported, we _need_ a New function to create
an instance that can be passed to the Context.
7 "code.secondbit.org/uuid"
11 // ErrNoGrantStore is returned when a Context tries to act on a grantStore without setting one first.
12 ErrNoGrantStore = errors.New("no grantStore was specified for the Context")
13 // ErrGrantNotFound is returned when a Grant is requested but not found in the grantStore.
14 ErrGrantNotFound = errors.New("grant not found in grantStore")
15 // ErrGrantAlreadyExists is returned when a Grant is added to a grantStore, but another Grant with the
16 // same Code already exists in the grantStore.
17 ErrGrantAlreadyExists = errors.New("grant already exists in grantStore")
20 // Grant represents an authorization grant made by a user to a Client, to
21 // access user data within a defined Scope for a limited amount of time.
32 type grantStore interface {
33 getGrant(code string) (Grant, error)
34 saveGrant(grant Grant) error
35 deleteGrant(code string) error
38 func (m *memstore) getGrant(code string) (Grant, error) {
40 defer m.grantLock.RUnlock()
41 grant, ok := m.grants[code]
43 return Grant{}, ErrGrantNotFound
48 func (m *memstore) saveGrant(grant Grant) error {
50 defer m.grantLock.Unlock()
51 _, ok := m.grants[grant.Code]
53 return ErrGrantAlreadyExists
55 m.grants[grant.Code] = grant
59 func (m *memstore) deleteGrant(code string) error {
61 defer m.grantLock.Unlock()
62 _, ok := m.grants[code]
64 return ErrGrantNotFound
66 delete(m.grants, code)