auth

Paddy 2014-09-07 Parent:5bf0a5fd1d01 Child:3a6a65ed380c

33:607708cd8829 Go to Latest

auth/grant.go

Add more tests for ClientStores. Test that deleting a client that doesn't exist throws an ErrClientNotFound. Test that adding a client twice returns an ErrClientAlreadyExists. Test that clients retrieve have properties that all match the client that was stored. Remove the unused error return from the internal MemoryStore helper for getting a list of clients by their OwnerID. If a profile doesn't exist, return an empty list of clients, as we're technically returning any client that has that OwnerID. There's no guarantee that that OwnerID is actually valid.

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