auth
auth/grant.go
Update Context with new CheckEndpoint and CountEndpoints. Update the Context.CheckEndpoint method to pass through the new "strict" parameter that controls how URLs are checked for validation. Add a Context.CountEndpoints method to safely access the number of endpoints in stored for the client in the associent ClientStore. Finally, remove the error return from Context.Render, as we'd only ever want to log that, anyways. So we may as well just log it from the central function.
| paddy@26 | 1 package auth |
| paddy@26 | 2 |
| paddy@26 | 3 import ( |
| paddy@29 | 4 "errors" |
| paddy@26 | 5 "time" |
| paddy@26 | 6 |
| paddy@45 | 7 "code.secondbit.org/uuid" |
| paddy@26 | 8 ) |
| paddy@26 | 9 |
| paddy@29 | 10 var ( |
| paddy@49 | 11 ErrNoGrantStore = errors.New("no GrantStore was specified for the Context") |
| paddy@49 | 12 ErrGrantNotFound = errors.New("grant not found in GrantStore") |
| paddy@49 | 13 ErrGrantAlreadyExists = errors.New("grant already exists in GrantStore") |
| paddy@29 | 14 ) |
| paddy@29 | 15 |
| paddy@26 | 16 type Grant struct { |
| paddy@26 | 17 Code string |
| paddy@26 | 18 Created time.Time |
| paddy@26 | 19 ExpiresIn int32 |
| paddy@26 | 20 ClientID uuid.ID |
| paddy@26 | 21 Scope string |
| paddy@26 | 22 RedirectURI string |
| paddy@26 | 23 State string |
| paddy@26 | 24 } |
| paddy@26 | 25 |
| paddy@26 | 26 type GrantStore interface { |
| paddy@26 | 27 GetGrant(code string) (Grant, error) |
| paddy@26 | 28 SaveGrant(grant Grant) error |
| paddy@29 | 29 DeleteGrant(code string) error |
| paddy@26 | 30 } |
| paddy@29 | 31 |
| paddy@29 | 32 func (m *Memstore) GetGrant(code string) (Grant, error) { |
| paddy@29 | 33 m.grantLock.RLock() |
| paddy@29 | 34 defer m.grantLock.RUnlock() |
| paddy@29 | 35 grant, ok := m.grants[code] |
| paddy@29 | 36 if !ok { |
| paddy@29 | 37 return Grant{}, ErrGrantNotFound |
| paddy@29 | 38 } |
| paddy@29 | 39 return grant, nil |
| paddy@29 | 40 } |
| paddy@29 | 41 |
| paddy@29 | 42 func (m *Memstore) SaveGrant(grant Grant) error { |
| paddy@29 | 43 m.grantLock.Lock() |
| paddy@29 | 44 defer m.grantLock.Unlock() |
| paddy@29 | 45 _, ok := m.grants[grant.Code] |
| paddy@29 | 46 if ok { |
| paddy@29 | 47 return ErrGrantAlreadyExists |
| paddy@29 | 48 } |
| paddy@29 | 49 m.grants[grant.Code] = grant |
| paddy@29 | 50 return nil |
| paddy@29 | 51 } |
| paddy@29 | 52 |
| paddy@29 | 53 func (m *Memstore) DeleteGrant(code string) error { |
| paddy@29 | 54 m.grantLock.Lock() |
| paddy@29 | 55 defer m.grantLock.Unlock() |
| paddy@29 | 56 _, ok := m.grants[code] |
| paddy@29 | 57 if !ok { |
| paddy@29 | 58 return ErrGrantNotFound |
| paddy@29 | 59 } |
| paddy@29 | 60 delete(m.grants, code) |
| paddy@29 | 61 return nil |
| paddy@29 | 62 } |