auth

Paddy 2014-09-01 Parent:e0b3064daf02 Child:09471ee7aab3

31:88523dab00a5 Browse Files

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.

client.go client_test.go memstore.go

     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 +}
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/client_test.go	Mon Sep 01 11:54:49 2014 -0400
     2.3 @@ -0,0 +1,56 @@
     2.4 +package auth
     2.5 +
     2.6 +import (
     2.7 +	"testing"
     2.8 +
     2.9 +	"secondbit.org/uuid"
    2.10 +)
    2.11 +
    2.12 +var clientStores = []ClientStore{NewMemstore()}
    2.13 +
    2.14 +func TestClientStoreSuccess(t *testing.T) {
    2.15 +	client := Client{
    2.16 +		ID:          uuid.NewID(),
    2.17 +		Secret:      "secret",
    2.18 +		RedirectURI: "redirectURI",
    2.19 +		OwnerID:     uuid.NewID(),
    2.20 +		Name:        "name",
    2.21 +		Logo:        "logo",
    2.22 +		Website:     "website",
    2.23 +	}
    2.24 +	for _, store := range clientStores {
    2.25 +		err := store.SaveClient(client)
    2.26 +		if err != nil {
    2.27 +			t.Errorf("Error saving client to %T: %s", store, err)
    2.28 +		}
    2.29 +		retrieved, err := store.GetClient(client.ID)
    2.30 +		if err != nil {
    2.31 +			t.Errorf("Error retrieving client from %T: %s", store, err)
    2.32 +		}
    2.33 +		t.Log(retrieved)
    2.34 +		// TODO: compare retrieved to client
    2.35 +		clients, err := store.ListClientsByOwner(client.OwnerID, 25, 0)
    2.36 +		if err != nil {
    2.37 +			t.Errorf("Error retrieving clients by owner from %T: %s", store, err)
    2.38 +		}
    2.39 +		if len(clients) != 1 {
    2.40 +			t.Errorf("Expected 1 client in response from %T, got %+v", store, clients)
    2.41 +		}
    2.42 +		// TODO: compare clients[0] to client
    2.43 +		err = store.DeleteClient(client.ID)
    2.44 +		if err != nil {
    2.45 +			t.Errorf("Error deleting client from %T: %s", store, err)
    2.46 +		}
    2.47 +		retrieved, err = store.GetClient(client.ID)
    2.48 +		if err != ErrClientNotFound {
    2.49 +			t.Errorf("Expected ErrClientNotFound from %T, got %+v and %s", store, retrieved, err)
    2.50 +		}
    2.51 +		clients, err = store.ListClientsByOwner(client.OwnerID, 25, 0)
    2.52 +		if err != nil {
    2.53 +			t.Errorf("Error listing clients by owner from %T: %s", store, err)
    2.54 +		}
    2.55 +		if len(clients) != 0 {
    2.56 +			t.Errorf("Expected 0 clients in response from %T, got %+v", store, clients)
    2.57 +		}
    2.58 +	}
    2.59 +}
     3.1 --- a/memstore.go	Mon Sep 01 09:52:28 2014 -0400
     3.2 +++ b/memstore.go	Mon Sep 01 11:54:49 2014 -0400
     3.3 @@ -1,6 +1,10 @@
     3.4  package auth
     3.5  
     3.6 -import "sync"
     3.7 +import (
     3.8 +	"sync"
     3.9 +
    3.10 +	"secondbit.org/uuid"
    3.11 +)
    3.12  
    3.13  type Memstore struct {
    3.14  	tokens             map[string]Token
    3.15 @@ -10,14 +14,20 @@
    3.16  
    3.17  	grants    map[string]Grant
    3.18  	grantLock sync.RWMutex
    3.19 +
    3.20 +	clients             map[string]Client
    3.21 +	profileClientLookup map[string][]uuid.ID
    3.22 +	clientLock          sync.RWMutex
    3.23  }
    3.24  
    3.25  func NewMemstore() *Memstore {
    3.26  	return &Memstore{
    3.27 -		tokens:             map[string]Token{},
    3.28 -		refreshTokenLookup: map[string]string{},
    3.29 -		profileTokenLookup: map[string][]string{},
    3.30 -		grants:             map[string]Grant{},
    3.31 +		tokens:              map[string]Token{},
    3.32 +		refreshTokenLookup:  map[string]string{},
    3.33 +		profileTokenLookup:  map[string][]string{},
    3.34 +		grants:              map[string]Grant{},
    3.35 +		clients:             map[string]Client{},
    3.36 +		profileClientLookup: map[string][]uuid.ID{},
    3.37  	}
    3.38  }
    3.39  
    3.40 @@ -36,3 +46,9 @@
    3.41  	defer m.tokenLock.RUnlock()
    3.42  	return m.profileTokenLookup[id], nil
    3.43  }
    3.44 +
    3.45 +func (m *Memstore) lookupClientsByProfileID(id string) ([]uuid.ID, error) {
    3.46 +	m.clientLock.RLock()
    3.47 +	defer m.clientLock.RUnlock()
    3.48 +	return m.profileClientLookup[id], nil
    3.49 +}