auth
auth/grant.go
Implement GrantStore for Memstore. Fix bug in GrantStore that asks for a UUID when it really wants a string. Implement GrantStore interface in Memstore, including unit tests for successful (i.e., works-as-intended) scenarios.
1.1 --- a/grant.go Mon Sep 01 09:21:31 2014 -0400 1.2 +++ b/grant.go Mon Sep 01 09:49:23 2014 -0400 1.3 @@ -1,11 +1,17 @@ 1.4 package auth 1.5 1.6 import ( 1.7 + "errors" 1.8 "time" 1.9 1.10 "secondbit.org/uuid" 1.11 ) 1.12 1.13 +var ( 1.14 + ErrGrantNotFound = errors.New("Grant not found in GrantStore.") 1.15 + ErrGrantAlreadyExists = errors.New("Grant already exists in GrantStore.") 1.16 +) 1.17 + 1.18 type Grant struct { 1.19 Code string 1.20 Created time.Time 1.21 @@ -19,5 +25,37 @@ 1.22 type GrantStore interface { 1.23 GetGrant(code string) (Grant, error) 1.24 SaveGrant(grant Grant) error 1.25 - DeleteGrant(id uuid.ID) error 1.26 + DeleteGrant(code string) error 1.27 } 1.28 + 1.29 +func (m *Memstore) GetGrant(code string) (Grant, error) { 1.30 + m.grantLock.RLock() 1.31 + defer m.grantLock.RUnlock() 1.32 + grant, ok := m.grants[code] 1.33 + if !ok { 1.34 + return Grant{}, ErrGrantNotFound 1.35 + } 1.36 + return grant, nil 1.37 +} 1.38 + 1.39 +func (m *Memstore) SaveGrant(grant Grant) error { 1.40 + m.grantLock.Lock() 1.41 + defer m.grantLock.Unlock() 1.42 + _, ok := m.grants[grant.Code] 1.43 + if ok { 1.44 + return ErrGrantAlreadyExists 1.45 + } 1.46 + m.grants[grant.Code] = grant 1.47 + return nil 1.48 +} 1.49 + 1.50 +func (m *Memstore) DeleteGrant(code string) error { 1.51 + m.grantLock.Lock() 1.52 + defer m.grantLock.Unlock() 1.53 + _, ok := m.grants[code] 1.54 + if !ok { 1.55 + return ErrGrantNotFound 1.56 + } 1.57 + delete(m.grants, code) 1.58 + return nil 1.59 +}