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 package auth
3 import (
4 "html/template"
5 "io"
6 "log"
7 "time"
9 "code.secondbit.org/uuid"
10 )
12 // Context wraps the different storage interfaces and should
13 // be used as the main point of interaction for the data storage
14 // layer.
15 type Context struct {
16 template *template.Template
17 clients clientStore
18 grants grantStore
19 profiles profileStore
20 tokens tokenStore
21 }
23 // Render uses the HTML templates associated with the Context to render the
24 // template specified by name to out using data to fill any template variables.
25 func (c Context) Render(out io.Writer, name string, data interface{}) {
26 if c.template == nil {
27 log.Println("No template set on Context, can't render anything!")
28 return
29 }
30 err := c.template.ExecuteTemplate(out, name, data)
31 if err != nil {
32 log.Println("Error executing template", name, ":", err)
33 }
34 }
36 // GetClient returns a single Client by its ID from the
37 // clientStore associated with the Context.
38 func (c Context) GetClient(id uuid.ID) (Client, error) {
39 if c.clients == nil {
40 return Client{}, ErrNoClientStore
41 }
42 return c.clients.getClient(id)
43 }
45 // SaveClient stores the passed Client in the clientStore
46 // associated with the Context.
47 func (c Context) SaveClient(client Client) error {
48 if c.clients == nil {
49 return ErrNoClientStore
50 }
51 return c.clients.saveClient(client)
52 }
54 // UpdateClient applies the specified ClientChange to the Client
55 // with the specified ID in the clientStore associated with the
56 // Context.
57 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
58 if c.clients == nil {
59 return ErrNoClientStore
60 }
61 return c.clients.updateClient(id, change)
62 }
64 // DeleteClient removes the client with the specified ID from the
65 // clientStore associated with the Context.
66 func (c Context) DeleteClient(id uuid.ID) error {
67 if c.clients == nil {
68 return ErrNoClientStore
69 }
70 return c.clients.deleteClient(id)
71 }
73 // ListClientsByOwner returns a slice of up to num Clients, starting at offset (inclusive)
74 // that have the specified OwnerID in the clientStore associated with the Context.
75 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
76 if c.clients == nil {
77 return []Client{}, ErrNoClientStore
78 }
79 return c.clients.listClientsByOwner(ownerID, num, offset)
80 }
82 // AddEndpoint stores the specified Endpoint in the clientStore associated with the Context,
83 // and associates the newly-stored Endpoint with the Client specified by the passed ID.
84 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
85 if c.clients == nil {
86 return ErrNoClientStore
87 }
88 return c.clients.addEndpoint(client, endpoint)
89 }
91 // RemoveEndpoint deletes the Endpoint with the specified ID from the clientStore associated
92 // with the Context, and disassociates the Endpoint from the specified Client.
93 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
94 if c.clients == nil {
95 return ErrNoClientStore
96 }
97 return c.clients.removeEndpoint(client, endpoint)
98 }
100 // CheckEndpoint finds Endpoints in the clientStore associated with the Context that belong
101 // to the Client specified by the passed ID and match the URI passed. If strict is true, only
102 // exact matches for the URI will be returned. If it is false, matches are performed according
103 // to RFC 3986 Section 6.
104 func (c Context) CheckEndpoint(client uuid.ID, URI string, strict bool) (bool, error) {
105 if c.clients == nil {
106 return false, ErrNoClientStore
107 }
108 return c.clients.checkEndpoint(client, URI, strict)
109 }
111 // ListEndpoints finds Endpoints in the clientStore associated with the Context that belong
112 // to the Client specified by the passed ID. It returns up to num endpoints, starting at offset,
113 // exclusive.
114 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
115 if c.clients == nil {
116 return []Endpoint{}, ErrNoClientStore
117 }
118 return c.clients.listEndpoints(client, num, offset)
119 }
121 // CountEndpoints returns the number of Endpoints the are associated with the Client specified by the
122 // passed ID in the clientStore associated with the Context.
123 func (c Context) CountEndpoints(client uuid.ID) (int64, error) {
124 if c.clients == nil {
125 return 0, ErrNoClientStore
126 }
127 return c.clients.countEndpoints(client)
128 }
130 // GetGrant returns the Grant specified by the provided code from the grantStore associated with the
131 // Context.
132 func (c Context) GetGrant(code string) (Grant, error) {
133 if c.grants == nil {
134 return Grant{}, ErrNoGrantStore
135 }
136 return c.grants.getGrant(code)
137 }
139 // SaveGrant stores the passed Grant in the grantStore associated with the Context.
140 func (c Context) SaveGrant(grant Grant) error {
141 if c.grants == nil {
142 return ErrNoGrantStore
143 }
144 return c.grants.saveGrant(grant)
145 }
147 // DeleteGrant removes the Grant specified by the provided code from the grantStore associated with
148 // the Context.
149 func (c Context) DeleteGrant(code string) error {
150 if c.grants == nil {
151 return ErrNoGrantStore
152 }
153 return c.grants.deleteGrant(code)
154 }
156 // GetProfileByID returns the Profile specified by the provided ID from the profileStore associated with
157 // the Context.
158 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
159 if c.profiles == nil {
160 return Profile{}, ErrNoProfileStore
161 }
162 return c.profiles.getProfileByID(id)
163 }
165 // GetProfileByLogin returns the Profile associated with the specified Login from the profileStore associated
166 // with the Context.
167 func (c Context) GetProfileByLogin(loginType, value string) (Profile, error) {
168 if c.profiles == nil {
169 return Profile{}, ErrNoProfileStore
170 }
171 return c.profiles.getProfileByLogin(loginType, value)
172 }
174 // SaveProfile inserts the passed Profile into the profileStore associated with the Context.
175 func (c Context) SaveProfile(profile Profile) error {
176 if c.profiles == nil {
177 return ErrNoProfileStore
178 }
179 return c.profiles.saveProfile(profile)
180 }
182 // UpdateProfile applies the supplied ProfileChange to the Profile that matches the specified ID
183 // in the profileStore associated with the Context.
184 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
185 if c.profiles == nil {
186 return ErrNoProfileStore
187 }
188 return c.profiles.updateProfile(id, change)
189 }
191 // UpdateProfiles applies the supplied BulkProfileChange to every Profile that matches one of the
192 // specified IDs in the profileStore associated with the Context.
193 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
194 if c.profiles == nil {
195 return ErrNoProfileStore
196 }
197 return c.profiles.updateProfiles(ids, change)
198 }
200 // DeleteProfile removes the Profile specified by the passed ID from the profileStore associated
201 // with the Context.
202 func (c Context) DeleteProfile(id uuid.ID) error {
203 if c.profiles == nil {
204 return ErrNoProfileStore
205 }
206 return c.profiles.deleteProfile(id)
207 }
209 // AddLogin stores the passed Login in the profileStore associated with the Context. It also associates
210 // the newly-created Login with the Orofile in login.ProfileID.
211 func (c Context) AddLogin(login Login) error {
212 if c.profiles == nil {
213 return ErrNoProfileStore
214 }
215 return c.profiles.addLogin(login)
216 }
218 // RemoveLogin removes the specified Login from the profileStore associated with the Context, provided
219 // the Login has a ProfileID property that matches the profile ID passed in. It also disassociates the
220 // deleted Login from the Profile in login.ProfileID.
221 func (c Context) RemoveLogin(loginType, value string, profile uuid.ID) error {
222 if c.profiles == nil {
223 return ErrNoProfileStore
224 }
225 return c.profiles.removeLogin(loginType, value, profile)
226 }
228 // RecordLoginUse sets the LastUsed property of the Login specified in the profileStore associated with
229 // the Context to the value passed in as when.
230 func (c Context) RecordLoginUse(loginType, value string, when time.Time) error {
231 if c.profiles == nil {
232 return ErrNoProfileStore
233 }
234 return c.profiles.recordLoginUse(loginType, value, when)
235 }
237 // ListLogins returns a slice of up to num Logins associated with the specified Profile from the profileStore
238 // associated with the Context, skipping offset Profiles.
239 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
240 if c.profiles == nil {
241 return []Login{}, ErrNoProfileStore
242 }
243 return c.profiles.listLogins(profile, num, offset)
244 }
246 // GetToken returns the Token specified from the tokenStore associated with the Context.
247 // If refresh is true, the token input should be compared against the refresh tokens, not the
248 // access tokens.
249 func (c Context) GetToken(token string, refresh bool) (Token, error) {
250 if c.tokens == nil {
251 return Token{}, ErrNoTokenStore
252 }
253 return c.tokens.getToken(token, refresh)
254 }
256 // SaveToken stores the passed Token in the tokenStore associated with the Context.
257 func (c Context) SaveToken(token Token) error {
258 if c.tokens == nil {
259 return ErrNoTokenStore
260 }
261 return c.tokens.saveToken(token)
262 }
264 // RemoveToken removes the Token identified by the passed token string from the tokenStore associated
265 // with the Context.
266 func (c Context) RemoveToken(token string) error {
267 if c.tokens == nil {
268 return ErrNoTokenStore
269 }
270 return c.tokens.removeToken(token)
271 }
273 // GetTokensByProfileID returns a slice of up to num Tokens with a ProfileID that matches the specified
274 // profileID from the tokenStore associated with the Context, skipping offset Tokens.
275 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
276 if c.tokens == nil {
277 return []Token{}, ErrNoTokenStore
278 }
279 return c.tokens.getTokensByProfileID(profileID, num, offset)
280 }