auth

Paddy 2014-10-15 Parent:73a9f7a6af54 Child:e45bfa2abc00

50:b620d32d9903 Go to Latest

auth/grant.go

Create the Context type and its helpers. Create a Context type that ties together all our Stores and other configuration-specific items. Create helper functions for the Context, to throw errors when something is used without first being set, as all possible Context values _can_ be nil. Basically, it's better to throw an error than panic.

History
1 package auth
3 import (
4 "errors"
5 "time"
7 "code.secondbit.org/uuid"
8 )
10 var (
11 ErrNoGrantStore = errors.New("no GrantStore was specified for the Context")
12 ErrGrantNotFound = errors.New("grant not found in GrantStore")
13 ErrGrantAlreadyExists = errors.New("grant already exists in GrantStore")
14 )
16 type Grant struct {
17 Code string
18 Created time.Time
19 ExpiresIn int32
20 ClientID uuid.ID
21 Scope string
22 RedirectURI string
23 State string
24 }
26 type GrantStore interface {
27 GetGrant(code string) (Grant, error)
28 SaveGrant(grant Grant) error
29 DeleteGrant(code string) error
30 }
32 func (m *Memstore) GetGrant(code string) (Grant, error) {
33 m.grantLock.RLock()
34 defer m.grantLock.RUnlock()
35 grant, ok := m.grants[code]
36 if !ok {
37 return Grant{}, ErrGrantNotFound
38 }
39 return grant, nil
40 }
42 func (m *Memstore) SaveGrant(grant Grant) error {
43 m.grantLock.Lock()
44 defer m.grantLock.Unlock()
45 _, ok := m.grants[grant.Code]
46 if ok {
47 return ErrGrantAlreadyExists
48 }
49 m.grants[grant.Code] = grant
50 return nil
51 }
53 func (m *Memstore) DeleteGrant(code string) error {
54 m.grantLock.Lock()
55 defer m.grantLock.Unlock()
56 _, ok := m.grants[code]
57 if !ok {
58 return ErrGrantNotFound
59 }
60 delete(m.grants, code)
61 return nil
62 }