auth
auth/client.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.
1.1 --- a/client.go Mon Sep 01 09:52:28 2014 -0400 1.2 +++ b/client.go Mon Sep 01 11:54:49 2014 -0400 1.3 @@ -1,9 +1,16 @@ 1.4 package auth 1.5 1.6 import ( 1.7 + "errors" 1.8 + 1.9 "secondbit.org/uuid" 1.10 ) 1.11 1.12 +var ( 1.13 + ErrClientNotFound = errors.New("Client not found in ClientStore.") 1.14 + ErrClientAlreadyExists = errors.New("Client already exists in ClientStore.") 1.15 +) 1.16 + 1.17 // Client represents a client that grants access 1.18 // to the auth server, exchanging grants for tokens, 1.19 // and tokens for access. 1.20 @@ -17,12 +24,90 @@ 1.21 Website string 1.22 } 1.23 1.24 +type ClientChange struct { 1.25 + Secret *string 1.26 + RedirectURI *string 1.27 + OwnerID uuid.ID 1.28 + Name *string 1.29 + Logo *string 1.30 + Website *string 1.31 +} 1.32 + 1.33 // ClientStore abstracts the storage interface for 1.34 // storing and retrieving Clients. 1.35 type ClientStore interface { 1.36 GetClient(id uuid.ID) (Client, error) 1.37 SaveClient(client Client) error 1.38 - UpdateClient(id uuid.ID, secret, redirectURI *string, ownerID uuid.ID, name, logo, website *string) error 1.39 + UpdateClient(id uuid.ID, change ClientChange) error 1.40 DeleteClient(id uuid.ID) error 1.41 - ListClientsByOwner(ownerID uuid.ID, page, num int) ([]Client, error) 1.42 + ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) 1.43 } 1.44 + 1.45 +func (m *Memstore) GetClient(id uuid.ID) (Client, error) { 1.46 + m.clientLock.RLock() 1.47 + defer m.clientLock.RUnlock() 1.48 + c, ok := m.clients[id.String()] 1.49 + if !ok { 1.50 + return Client{}, ErrClientNotFound 1.51 + } 1.52 + return c, nil 1.53 +} 1.54 + 1.55 +func (m *Memstore) SaveClient(client Client) error { 1.56 + m.clientLock.Lock() 1.57 + defer m.clientLock.Unlock() 1.58 + if _, ok := m.clients[client.ID.String()]; ok { 1.59 + return ErrClientAlreadyExists 1.60 + } 1.61 + m.clients[client.ID.String()] = client 1.62 + m.profileClientLookup[client.OwnerID.String()] = append(m.profileClientLookup[client.OwnerID.String()], client.ID) 1.63 + return nil 1.64 +} 1.65 + 1.66 +func (m *Memstore) UpdateClient(id uuid.ID, change ClientChange) error { 1.67 + return nil 1.68 +} 1.69 + 1.70 +func (m *Memstore) DeleteClient(id uuid.ID) error { 1.71 + client, err := m.GetClient(id) 1.72 + if err != nil { 1.73 + return err 1.74 + } 1.75 + m.clientLock.Lock() 1.76 + defer m.clientLock.Unlock() 1.77 + delete(m.clients, id.String()) 1.78 + pos := -1 1.79 + for p, item := range m.profileClientLookup[client.OwnerID.String()] { 1.80 + if item.Equal(id) { 1.81 + pos = p 1.82 + break 1.83 + } 1.84 + } 1.85 + if pos >= 0 { 1.86 + m.profileClientLookup[client.OwnerID.String()] = append(m.profileClientLookup[client.OwnerID.String()][:pos], m.profileClientLookup[client.OwnerID.String()][pos+1:]...) 1.87 + } 1.88 + return nil 1.89 +} 1.90 + 1.91 +func (m *Memstore) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) { 1.92 + ids, err := m.lookupClientsByProfileID(ownerID.String()) 1.93 + if err != nil { 1.94 + return []Client{}, err 1.95 + } 1.96 + if len(ids) > num+offset { 1.97 + ids = ids[offset : num+offset] 1.98 + } else if len(ids) > offset { 1.99 + ids = ids[offset:] 1.100 + } else { 1.101 + return []Client{}, nil 1.102 + } 1.103 + clients := []Client{} 1.104 + for _, id := range ids { 1.105 + client, err := m.GetClient(id) 1.106 + if err != nil { 1.107 + return []Client{}, err 1.108 + } 1.109 + clients = append(clients, client) 1.110 + } 1.111 + return clients, nil 1.112 +}