auth

Paddy 2014-10-26 Parent:cfab12566289 Child:b3cd7765a7c8

57:e45bfa2abc00 Go to Latest

auth/context.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/context.go	Wed Oct 22 00:30:28 2014 -0400
     1.2 +++ b/context.go	Sun Oct 26 00:53:36 2014 -0400
     1.3 @@ -1,7 +1,6 @@
     1.4  package auth
     1.5  
     1.6  import (
     1.7 -	"errors"
     1.8  	"html/template"
     1.9  	"io"
    1.10  	"log"
    1.11 @@ -10,21 +9,23 @@
    1.12  	"code.secondbit.org/uuid"
    1.13  )
    1.14  
    1.15 -var (
    1.16 -	ErrNoTemplate = errors.New("no Template specified for the Context")
    1.17 -)
    1.18 -
    1.19 +// Context wraps the different storage interfaces and should
    1.20 +// be used as the main point of interaction for the data storage
    1.21 +// layer.
    1.22  type Context struct {
    1.23  	template *template.Template
    1.24 -	clients  ClientStore
    1.25 -	grants   GrantStore
    1.26 -	profiles ProfileStore
    1.27 -	tokens   TokenStore
    1.28 +	clients  clientStore
    1.29 +	grants   grantStore
    1.30 +	profiles profileStore
    1.31 +	tokens   tokenStore
    1.32  }
    1.33  
    1.34 +// Render uses the HTML templates associated with the Context to render the
    1.35 +// template specified by name to out using data to fill any template variables.
    1.36  func (c Context) Render(out io.Writer, name string, data interface{}) {
    1.37  	if c.template == nil {
    1.38 -		log.Println(ErrNoTemplate)
    1.39 +		log.Println("No template set on Context, can't render anything!")
    1.40 +		return
    1.41  	}
    1.42  	err := c.template.ExecuteTemplate(out, name, data)
    1.43  	if err != nil {
    1.44 @@ -32,191 +33,248 @@
    1.45  	}
    1.46  }
    1.47  
    1.48 +// GetClient returns a single Client by its ID from the
    1.49 +// clientStore associated with the Context.
    1.50  func (c Context) GetClient(id uuid.ID) (Client, error) {
    1.51  	if c.clients == nil {
    1.52  		return Client{}, ErrNoClientStore
    1.53  	}
    1.54 -	return c.clients.GetClient(id)
    1.55 +	return c.clients.getClient(id)
    1.56  }
    1.57  
    1.58 +// SaveClient stores the passed Client in the clientStore
    1.59 +// associated with the Context.
    1.60  func (c Context) SaveClient(client Client) error {
    1.61  	if c.clients == nil {
    1.62  		return ErrNoClientStore
    1.63  	}
    1.64 -	return c.clients.SaveClient(client)
    1.65 +	return c.clients.saveClient(client)
    1.66  }
    1.67  
    1.68 +// UpdateClient applies the specified ClientChange to the Client
    1.69 +// with the specified ID in the clientStore associated with the
    1.70 +// Context.
    1.71  func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
    1.72  	if c.clients == nil {
    1.73  		return ErrNoClientStore
    1.74  	}
    1.75 -	return c.clients.UpdateClient(id, change)
    1.76 +	return c.clients.updateClient(id, change)
    1.77  }
    1.78  
    1.79 +// DeleteClient removes the client with the specified ID from the
    1.80 +// clientStore associated with the Context.
    1.81  func (c Context) DeleteClient(id uuid.ID) error {
    1.82  	if c.clients == nil {
    1.83  		return ErrNoClientStore
    1.84  	}
    1.85 -	return c.clients.DeleteClient(id)
    1.86 +	return c.clients.deleteClient(id)
    1.87  }
    1.88  
    1.89 +// ListClientsByOwner returns a slice of up to num Clients, starting at offset (inclusive)
    1.90 +// that have the specified OwnerID in the clientStore associated with the Context.
    1.91  func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
    1.92  	if c.clients == nil {
    1.93  		return []Client{}, ErrNoClientStore
    1.94  	}
    1.95 -	return c.clients.ListClientsByOwner(ownerID, num, offset)
    1.96 +	return c.clients.listClientsByOwner(ownerID, num, offset)
    1.97  }
    1.98  
    1.99 +// AddEndpoint stores the specified Endpoint in the clientStore associated with the Context,
   1.100 +// and associates the newly-stored Endpoint with the Client specified by the passed ID.
   1.101  func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
   1.102  	if c.clients == nil {
   1.103  		return ErrNoClientStore
   1.104  	}
   1.105 -	return c.clients.AddEndpoint(client, endpoint)
   1.106 +	return c.clients.addEndpoint(client, endpoint)
   1.107  }
   1.108  
   1.109 +// RemoveEndpoint deletes the Endpoint with the specified ID from the clientStore associated
   1.110 +// with the Context, and disassociates the Endpoint from the specified Client.
   1.111  func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
   1.112  	if c.clients == nil {
   1.113  		return ErrNoClientStore
   1.114  	}
   1.115 -	return c.clients.RemoveEndpoint(client, endpoint)
   1.116 +	return c.clients.removeEndpoint(client, endpoint)
   1.117  }
   1.118  
   1.119 +// CheckEndpoint finds Endpoints in the clientStore associated with the Context that belong
   1.120 +// to the Client specified by the passed ID and match the URI passed. If strict is true, only
   1.121 +// exact matches for the URI will be returned. If it is false, matches are performed according
   1.122 +// to RFC 3986 Section 6.
   1.123  func (c Context) CheckEndpoint(client uuid.ID, URI string, strict bool) (bool, error) {
   1.124  	if c.clients == nil {
   1.125  		return false, ErrNoClientStore
   1.126  	}
   1.127 -	return c.clients.CheckEndpoint(client, URI, strict)
   1.128 +	return c.clients.checkEndpoint(client, URI, strict)
   1.129  }
   1.130  
   1.131 +// ListEndpoints finds Endpoints in the clientStore associated with the Context that belong
   1.132 +// to the Client specified by the passed ID. It returns up to num endpoints, starting at offset,
   1.133 +// exclusive.
   1.134  func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
   1.135  	if c.clients == nil {
   1.136  		return []Endpoint{}, ErrNoClientStore
   1.137  	}
   1.138 -	return c.clients.ListEndpoints(client, num, offset)
   1.139 +	return c.clients.listEndpoints(client, num, offset)
   1.140  }
   1.141  
   1.142 +// CountEndpoints returns the number of Endpoints the are associated with the Client specified by the
   1.143 +// passed ID in the clientStore associated with the Context.
   1.144  func (c Context) CountEndpoints(client uuid.ID) (int64, error) {
   1.145  	if c.clients == nil {
   1.146  		return 0, ErrNoClientStore
   1.147  	}
   1.148 -	return c.clients.CountEndpoints(client)
   1.149 +	return c.clients.countEndpoints(client)
   1.150  }
   1.151  
   1.152 +// GetGrant returns the Grant specified by the provided code from the grantStore associated with the
   1.153 +// Context.
   1.154  func (c Context) GetGrant(code string) (Grant, error) {
   1.155  	if c.grants == nil {
   1.156  		return Grant{}, ErrNoGrantStore
   1.157  	}
   1.158 -	return c.grants.GetGrant(code)
   1.159 +	return c.grants.getGrant(code)
   1.160  }
   1.161  
   1.162 +// SaveGrant stores the passed Grant in the grantStore associated with the Context.
   1.163  func (c Context) SaveGrant(grant Grant) error {
   1.164  	if c.grants == nil {
   1.165  		return ErrNoGrantStore
   1.166  	}
   1.167 -	return c.grants.SaveGrant(grant)
   1.168 +	return c.grants.saveGrant(grant)
   1.169  }
   1.170  
   1.171 +// DeleteGrant removes the Grant specified by the provided code from the grantStore associated with
   1.172 +// the Context.
   1.173  func (c Context) DeleteGrant(code string) error {
   1.174  	if c.grants == nil {
   1.175  		return ErrNoGrantStore
   1.176  	}
   1.177 -	return c.grants.DeleteGrant(code)
   1.178 +	return c.grants.deleteGrant(code)
   1.179  }
   1.180  
   1.181 +// GetProfileByID returns the Profile specified by the provided ID from the profileStore associated with
   1.182 +// the Context.
   1.183  func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
   1.184  	if c.profiles == nil {
   1.185  		return Profile{}, ErrNoProfileStore
   1.186  	}
   1.187 -	return c.profiles.GetProfileByID(id)
   1.188 +	return c.profiles.getProfileByID(id)
   1.189  }
   1.190  
   1.191 +// GetProfileByLogin returns the Profile associated with the specified Login from the profileStore associated
   1.192 +// with the Context.
   1.193  func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) {
   1.194  	if c.profiles == nil {
   1.195  		return Profile{}, ErrNoProfileStore
   1.196  	}
   1.197 -	return c.profiles.GetProfileByLogin(loginType, value)
   1.198 +	return c.profiles.getProfileByLogin(loginType, value)
   1.199  }
   1.200  
   1.201 +// SaveProfile inserts the passed Profile into the profileStore associated with the Context.
   1.202  func (c Context) SaveProfile(profile Profile) error {
   1.203  	if c.profiles == nil {
   1.204  		return ErrNoProfileStore
   1.205  	}
   1.206 -	return c.profiles.SaveProfile(profile)
   1.207 +	return c.profiles.saveProfile(profile)
   1.208  }
   1.209  
   1.210 +// UpdateProfile applies the supplied ProfileChange to the Profile that matches the specified ID
   1.211 +// in the profileStore associated with the Context.
   1.212  func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
   1.213  	if c.profiles == nil {
   1.214  		return ErrNoProfileStore
   1.215  	}
   1.216 -	return c.profiles.UpdateProfile(id, change)
   1.217 +	return c.profiles.updateProfile(id, change)
   1.218  }
   1.219  
   1.220 +// UpdateProfiles applies the supplied BulkProfileChange to every Profile that matches one of the
   1.221 +// specified IDs in the profileStore associated with the Context.
   1.222  func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
   1.223  	if c.profiles == nil {
   1.224  		return ErrNoProfileStore
   1.225  	}
   1.226 -	return c.profiles.UpdateProfiles(ids, change)
   1.227 +	return c.profiles.updateProfiles(ids, change)
   1.228  }
   1.229  
   1.230 +// DeleteProfile removes the Profile specified by the passed ID from the profileStore associated
   1.231 +// with the Context.
   1.232  func (c Context) DeleteProfile(id uuid.ID) error {
   1.233  	if c.profiles == nil {
   1.234  		return ErrNoProfileStore
   1.235  	}
   1.236 -	return c.profiles.DeleteProfile(id)
   1.237 +	return c.profiles.deleteProfile(id)
   1.238  }
   1.239  
   1.240 +// AddLogin stores the passed Login in the profileStore associated with the Context. It also associates
   1.241 +// the newly-created Login with the Orofile in login.ProfileID.
   1.242  func (c Context) AddLogin(login Login) error {
   1.243  	if c.profiles == nil {
   1.244  		return ErrNoProfileStore
   1.245  	}
   1.246 -	return c.profiles.AddLogin(login)
   1.247 +	return c.profiles.addLogin(login)
   1.248  }
   1.249  
   1.250 +// RemoveLogin removes the specified Login from the profileStore associated with the Context, provided
   1.251 +// the Login has a ProfileID property that matches the profile ID passed in. It also disassociates the
   1.252 +// deleted Login from the Profile in login.ProfileID.
   1.253  func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error {
   1.254  	if c.profiles == nil {
   1.255  		return ErrNoProfileStore
   1.256  	}
   1.257 -	return c.profiles.RemoveLogin(loginType, value, profile)
   1.258 +	return c.profiles.removeLogin(loginType, value, profile)
   1.259  }
   1.260  
   1.261 +// RecordLoginUse sets the LastUsed property of the Login specified in the profileStore associated with
   1.262 +// the Context to the value passed in as when.
   1.263  func (c Context) RecordLoginUse(loginType, value string, when time.Time) error {
   1.264  	if c.profiles == nil {
   1.265  		return ErrNoProfileStore
   1.266  	}
   1.267 -	return c.profiles.RecordLoginUse(loginType, value, when)
   1.268 +	return c.profiles.recordLoginUse(loginType, value, when)
   1.269  }
   1.270  
   1.271 +// ListLogins returns a slice of up to num Logins associated with the specified Profile from the profileStore
   1.272 +// associated with the Context, skipping offset Profiles.
   1.273  func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
   1.274  	if c.profiles == nil {
   1.275  		return []Login{}, ErrNoProfileStore
   1.276  	}
   1.277 -	return c.profiles.ListLogins(profile, num, offset)
   1.278 +	return c.profiles.listLogins(profile, num, offset)
   1.279  }
   1.280  
   1.281 +// GetToken returns the Token specified from the tokenStore associated with the Context.
   1.282 +// If refresh is true, the token input should be compared against the refresh tokens, not the
   1.283 +// access tokens.
   1.284  func (c Context) GetToken(token string, refresh bool) (Token, error) {
   1.285  	if c.tokens == nil {
   1.286  		return Token{}, ErrNoTokenStore
   1.287  	}
   1.288 -	return c.tokens.GetToken(token, refresh)
   1.289 +	return c.tokens.getToken(token, refresh)
   1.290  }
   1.291  
   1.292 +// SaveToken stores the passed Token in the tokenStore associated with the Context.
   1.293  func (c Context) SaveToken(token Token) error {
   1.294  	if c.tokens == nil {
   1.295  		return ErrNoTokenStore
   1.296  	}
   1.297 -	return c.tokens.SaveToken(token)
   1.298 +	return c.tokens.saveToken(token)
   1.299  }
   1.300  
   1.301 +// RemoveToken removes the Token identified by the passed token string from the tokenStore associated
   1.302 +// with the Context.
   1.303  func (c Context) RemoveToken(token string) error {
   1.304  	if c.tokens == nil {
   1.305  		return ErrNoTokenStore
   1.306  	}
   1.307 -	return c.tokens.RemoveToken(token)
   1.308 +	return c.tokens.removeToken(token)
   1.309  }
   1.310  
   1.311 +// GetTokensByProfileID returns a slice of up to num Tokens with a ProfileID that matches the specified
   1.312 +// profileID from the tokenStore associated with the Context, skipping offset Tokens.
   1.313  func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
   1.314  	if c.tokens == nil {
   1.315  		return []Token{}, ErrNoTokenStore
   1.316  	}
   1.317 -	return c.tokens.GetTokensByProfileID(profileID, num, offset)
   1.318 +	return c.tokens.getTokensByProfileID(profileID, num, offset)
   1.319  }