auth

Paddy 2014-10-26 Parent:73a9f7a6af54 Child:42bc3e44f4fe

57:e45bfa2abc00 Go to Latest

auth/token.go

The great documentation and exported interface cleanup. Modify all our *Store interfaces to be unexported, as there's no real good reason they need to be exported, especially as they can be implemented without being exported. The interfaces shouldn't matter to 99% of users of the package, so let's not pollute our package API. Further, all methods of the interfaces are now unexported, for pretty much the same reasoning. Add a doc.go file with documentation explaining the choices the package is making and what it provides. Implement documentation on all our exported types and methods and functions, which makes golint happy. The only remaining golint warning is about NewMemstore, which will stay the way it is. The memstore type is useful outside tests for things like standing up a server quickly when we don't care about the storage, and because the type is unexported, we _need_ a New function to create an instance that can be passed to the Context.

History
     1.1 --- a/token.go	Wed Oct 22 00:30:28 2014 -0400
     1.2 +++ b/token.go	Sun Oct 26 00:53:36 2014 -0400
     1.3 @@ -8,11 +8,17 @@
     1.4  )
     1.5  
     1.6  var (
     1.7 -	ErrNoTokenStore       = errors.New("no TokenStore was specified for the Context")
     1.8 -	ErrTokenNotFound      = errors.New("token not found in TokenStore")
     1.9 -	ErrTokenAlreadyExists = errors.New("token already exists in TokenStore")
    1.10 +	// ErrNoTokenStore is returned when a Context tries to act on a tokenStore without setting one first.
    1.11 +	ErrNoTokenStore = errors.New("no tokenStore was specified for the Context")
    1.12 +	// ErrTokenNotFound is returned when a Token is requested but not found in a tokenStore.
    1.13 +	ErrTokenNotFound = errors.New("token not found in tokenStore")
    1.14 +	// ErrTokenAlreadyExists is returned when a Token is added to a tokenStore, but another Token with
    1.15 +	// the same AccessToken property already exists in the tokenStore.
    1.16 +	ErrTokenAlreadyExists = errors.New("token already exists in tokenStore")
    1.17  )
    1.18  
    1.19 +// Token represents an access and/or refresh token that the Client can use to access user data
    1.20 +// or obtain a new access token.
    1.21  type Token struct {
    1.22  	AccessToken  string
    1.23  	RefreshToken string
    1.24 @@ -23,14 +29,14 @@
    1.25  	ProfileID    uuid.ID
    1.26  }
    1.27  
    1.28 -type TokenStore interface {
    1.29 -	GetToken(token string, refresh bool) (Token, error)
    1.30 -	SaveToken(token Token) error
    1.31 -	RemoveToken(token string) error
    1.32 -	GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error)
    1.33 +type tokenStore interface {
    1.34 +	getToken(token string, refresh bool) (Token, error)
    1.35 +	saveToken(token Token) error
    1.36 +	removeToken(token string) error
    1.37 +	getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error)
    1.38  }
    1.39  
    1.40 -func (m *Memstore) GetToken(token string, refresh bool) (Token, error) {
    1.41 +func (m *memstore) getToken(token string, refresh bool) (Token, error) {
    1.42  	if refresh {
    1.43  		t, err := m.lookupTokenByRefresh(token)
    1.44  		if err != nil {
    1.45 @@ -47,7 +53,7 @@
    1.46  	return result, nil
    1.47  }
    1.48  
    1.49 -func (m *Memstore) SaveToken(token Token) error {
    1.50 +func (m *memstore) saveToken(token Token) error {
    1.51  	m.tokenLock.Lock()
    1.52  	defer m.tokenLock.Unlock()
    1.53  	_, ok := m.tokens[token.AccessToken]
    1.54 @@ -66,7 +72,7 @@
    1.55  	return nil
    1.56  }
    1.57  
    1.58 -func (m *Memstore) RemoveToken(token string) error {
    1.59 +func (m *memstore) removeToken(token string) error {
    1.60  	m.tokenLock.Lock()
    1.61  	defer m.tokenLock.Unlock()
    1.62  	t, ok := m.tokens[token]
    1.63 @@ -90,7 +96,7 @@
    1.64  	return nil
    1.65  }
    1.66  
    1.67 -func (m *Memstore) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
    1.68 +func (m *memstore) getTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
    1.69  	ids, err := m.lookupTokensByProfileID(profileID.String())
    1.70  	if err != nil {
    1.71  		return []Token{}, err
    1.72 @@ -104,7 +110,7 @@
    1.73  	}
    1.74  	tokens := []Token{}
    1.75  	for _, id := range ids {
    1.76 -		token, err := m.GetToken(id, false)
    1.77 +		token, err := m.getToken(id, false)
    1.78  		if err != nil {
    1.79  			return []Token{}, err
    1.80  		}