auth
auth/grant.go
Implement ClientStore in Memstore. Add the ClientStore interface implementation to Memstore. Change the ClientStore interface's UpdateClient function to take a new type, ClientChange, rather than enumerating the arguments in the function signature, which is a much cleaner interface. Add tests for the successful (works-as-intended) scenarios involving ClientStores. Ignore UpdateClient for now--I want to do tests that test every combination of ClientUpdate attributes being specified, to be sure that only the attributes specified are updated and that all the attributes specified are updated.
| paddy@26 | 1 package auth |
| paddy@26 | 2 |
| paddy@26 | 3 import ( |
| paddy@29 | 4 "errors" |
| paddy@26 | 5 "time" |
| paddy@26 | 6 |
| paddy@26 | 7 "secondbit.org/uuid" |
| paddy@26 | 8 ) |
| paddy@26 | 9 |
| paddy@29 | 10 var ( |
| paddy@29 | 11 ErrGrantNotFound = errors.New("Grant not found in GrantStore.") |
| paddy@29 | 12 ErrGrantAlreadyExists = errors.New("Grant already exists in GrantStore.") |
| paddy@29 | 13 ) |
| paddy@29 | 14 |
| paddy@26 | 15 type Grant struct { |
| paddy@26 | 16 Code string |
| paddy@26 | 17 Created time.Time |
| paddy@26 | 18 ExpiresIn int32 |
| paddy@26 | 19 ClientID uuid.ID |
| paddy@26 | 20 Scope string |
| paddy@26 | 21 RedirectURI string |
| paddy@26 | 22 State string |
| paddy@26 | 23 } |
| paddy@26 | 24 |
| paddy@26 | 25 type GrantStore interface { |
| paddy@26 | 26 GetGrant(code string) (Grant, error) |
| paddy@26 | 27 SaveGrant(grant Grant) error |
| paddy@29 | 28 DeleteGrant(code string) error |
| paddy@26 | 29 } |
| paddy@29 | 30 |
| paddy@29 | 31 func (m *Memstore) GetGrant(code string) (Grant, error) { |
| paddy@29 | 32 m.grantLock.RLock() |
| paddy@29 | 33 defer m.grantLock.RUnlock() |
| paddy@29 | 34 grant, ok := m.grants[code] |
| paddy@29 | 35 if !ok { |
| paddy@29 | 36 return Grant{}, ErrGrantNotFound |
| paddy@29 | 37 } |
| paddy@29 | 38 return grant, nil |
| paddy@29 | 39 } |
| paddy@29 | 40 |
| paddy@29 | 41 func (m *Memstore) SaveGrant(grant Grant) error { |
| paddy@29 | 42 m.grantLock.Lock() |
| paddy@29 | 43 defer m.grantLock.Unlock() |
| paddy@29 | 44 _, ok := m.grants[grant.Code] |
| paddy@29 | 45 if ok { |
| paddy@29 | 46 return ErrGrantAlreadyExists |
| paddy@29 | 47 } |
| paddy@29 | 48 m.grants[grant.Code] = grant |
| paddy@29 | 49 return nil |
| paddy@29 | 50 } |
| paddy@29 | 51 |
| paddy@29 | 52 func (m *Memstore) DeleteGrant(code string) error { |
| paddy@29 | 53 m.grantLock.Lock() |
| paddy@29 | 54 defer m.grantLock.Unlock() |
| paddy@29 | 55 _, ok := m.grants[code] |
| paddy@29 | 56 if !ok { |
| paddy@29 | 57 return ErrGrantNotFound |
| paddy@29 | 58 } |
| paddy@29 | 59 delete(m.grants, code) |
| paddy@29 | 60 return nil |
| paddy@29 | 61 } |