Write Session tests.
Add loginURI as a property to our Context, to keep track of where users should
be redirected to log in.
Implement the sessionStore in the memstore to let us test with Sessions.
Catch when the HTTP Basic Auth header doesn't include two parts, rather than
panicking. Return an ErrInvalidAuthFormat.
Clean up the error handling for checkCookie to be cleaner. Log unexpected errors
from request.Cookie.
Stop checking for cookie expiration times--those aren't sent to the server, so
we'll never get a valid session if we look for them.
Add a helper to build a login redirect URI--a URI the user can be redirected to
that has a URL-encoded URL to redirect the user back to after a successful
login.
Add a wrapper to wrap our Context into HTTP handlers.
Create a RegisterOAuth2 helper that adds the OAuth2 endpoints to a Gorilla/mux
router.
Redirect users to the login page when they have no session set or an invalid
session.
Return a server error when we can't check our cookie for whatever reason.
Log errors.
Add sessions to our OAuth2 tests so the tests stop failing--the session check
was interfering with them.
Add a test for our getBasicAuth helper to ensure that we're parsing basic auth
correctly.
Add an ErrSessionAlreadyExists error to be returned when a session has an ID
conflict.
Test that our memstore implementation of the sessionStore works as intended..
10 "code.secondbit.org/uuid"
13 // Context wraps the different storage interfaces and should
14 // be used as the main point of interaction for the data storage
17 template *template.Template
26 // Render uses the HTML templates associated with the Context to render the
27 // template specified by name to out using data to fill any template variables.
28 func (c Context) Render(out io.Writer, name string, data interface{}) {
29 if c.template == nil {
30 log.Println("No template set on Context, can't render anything!")
33 err := c.template.ExecuteTemplate(out, name, data)
35 log.Println("Error executing template", name, ":", err)
39 // GetClient returns a single Client by its ID from the
40 // clientStore associated with the Context.
41 func (c Context) GetClient(id uuid.ID) (Client, error) {
43 return Client{}, ErrNoClientStore
45 return c.clients.getClient(id)
48 // SaveClient stores the passed Client in the clientStore
49 // associated with the Context.
50 func (c Context) SaveClient(client Client) error {
52 return ErrNoClientStore
54 return c.clients.saveClient(client)
57 // UpdateClient applies the specified ClientChange to the Client
58 // with the specified ID in the clientStore associated with the
60 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
62 return ErrNoClientStore
64 return c.clients.updateClient(id, change)
67 // DeleteClient removes the client with the specified ID from the
68 // clientStore associated with the Context.
69 func (c Context) DeleteClient(id uuid.ID) error {
71 return ErrNoClientStore
73 return c.clients.deleteClient(id)
76 // ListClientsByOwner returns a slice of up to num Clients, starting at offset (inclusive)
77 // that have the specified OwnerID in the clientStore associated with the Context.
78 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
80 return []Client{}, ErrNoClientStore
82 return c.clients.listClientsByOwner(ownerID, num, offset)
85 // AddEndpoint stores the specified Endpoint in the clientStore associated with the Context,
86 // and associates the newly-stored Endpoint with the Client specified by the passed ID.
87 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
89 return ErrNoClientStore
91 return c.clients.addEndpoint(client, endpoint)
94 // RemoveEndpoint deletes the Endpoint with the specified ID from the clientStore associated
95 // with the Context, and disassociates the Endpoint from the specified Client.
96 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
98 return ErrNoClientStore
100 return c.clients.removeEndpoint(client, endpoint)
103 // CheckEndpoint finds Endpoints in the clientStore associated with the Context that belong
104 // to the Client specified by the passed ID and match the URI passed. URI matches must be
105 // performed according to RFC 3986 Section 6.
106 func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) {
107 if c.clients == nil {
108 return false, ErrNoClientStore
110 return c.clients.checkEndpoint(client, URI)
113 // ListEndpoints finds Endpoints in the clientStore associated with the Context that belong
114 // to the Client specified by the passed ID. It returns up to num endpoints, starting at offset,
116 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
117 if c.clients == nil {
118 return []Endpoint{}, ErrNoClientStore
120 return c.clients.listEndpoints(client, num, offset)
123 // CountEndpoints returns the number of Endpoints the are associated with the Client specified by the
124 // passed ID in the clientStore associated with the Context.
125 func (c Context) CountEndpoints(client uuid.ID) (int64, error) {
126 if c.clients == nil {
127 return 0, ErrNoClientStore
129 return c.clients.countEndpoints(client)
132 // GetGrant returns the Grant specified by the provided code from the grantStore associated with the
134 func (c Context) GetGrant(code string) (Grant, error) {
136 return Grant{}, ErrNoGrantStore
138 return c.grants.getGrant(code)
141 // SaveGrant stores the passed Grant in the grantStore associated with the Context.
142 func (c Context) SaveGrant(grant Grant) error {
144 return ErrNoGrantStore
146 return c.grants.saveGrant(grant)
149 // DeleteGrant removes the Grant specified by the provided code from the grantStore associated with
151 func (c Context) DeleteGrant(code string) error {
153 return ErrNoGrantStore
155 return c.grants.deleteGrant(code)
158 // GetProfileByID returns the Profile specified by the provided ID from the profileStore associated with
160 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
161 if c.profiles == nil {
162 return Profile{}, ErrNoProfileStore
164 return c.profiles.getProfileByID(id)
167 // GetProfileByLogin returns the Profile associated with the specified Login from the profileStore associated
169 func (c Context) GetProfileByLogin(value string) (Profile, error) {
170 if c.profiles == nil {
171 return Profile{}, ErrNoProfileStore
173 return c.profiles.getProfileByLogin(value)
176 // SaveProfile inserts the passed Profile into the profileStore associated with the Context.
177 func (c Context) SaveProfile(profile Profile) error {
178 if c.profiles == nil {
179 return ErrNoProfileStore
181 return c.profiles.saveProfile(profile)
184 // UpdateProfile applies the supplied ProfileChange to the Profile that matches the specified ID
185 // in the profileStore associated with the Context.
186 func (c Context) UpdateProfile(id uuid.ID, change ProfileChange) error {
187 if c.profiles == nil {
188 return ErrNoProfileStore
190 return c.profiles.updateProfile(id, change)
193 // UpdateProfiles applies the supplied BulkProfileChange to every Profile that matches one of the
194 // specified IDs in the profileStore associated with the Context.
195 func (c Context) UpdateProfiles(ids []uuid.ID, change BulkProfileChange) error {
196 if c.profiles == nil {
197 return ErrNoProfileStore
199 return c.profiles.updateProfiles(ids, change)
202 // DeleteProfile removes the Profile specified by the passed ID from the profileStore associated
204 func (c Context) DeleteProfile(id uuid.ID) error {
205 if c.profiles == nil {
206 return ErrNoProfileStore
208 return c.profiles.deleteProfile(id)
211 // AddLogin stores the passed Login in the profileStore associated with the Context. It also associates
212 // the newly-created Login with the Orofile in login.ProfileID.
213 func (c Context) AddLogin(login Login) error {
214 if c.profiles == nil {
215 return ErrNoProfileStore
217 return c.profiles.addLogin(login)
220 // RemoveLogin removes the specified Login from the profileStore associated with the Context, provided
221 // the Login has a ProfileID property that matches the profile ID passed in. It also disassociates the
222 // deleted Login from the Profile in login.ProfileID.
223 func (c Context) RemoveLogin(value string, profile uuid.ID) error {
224 if c.profiles == nil {
225 return ErrNoProfileStore
227 return c.profiles.removeLogin(value, profile)
230 // RecordLoginUse sets the LastUsed property of the Login specified in the profileStore associated with
231 // the Context to the value passed in as when.
232 func (c Context) RecordLoginUse(value string, when time.Time) error {
233 if c.profiles == nil {
234 return ErrNoProfileStore
236 return c.profiles.recordLoginUse(value, when)
239 // ListLogins returns a slice of up to num Logins associated with the specified Profile from the profileStore
240 // associated with the Context, skipping offset Profiles.
241 func (c Context) ListLogins(profile uuid.ID, num, offset int) ([]Login, error) {
242 if c.profiles == nil {
243 return []Login{}, ErrNoProfileStore
245 return c.profiles.listLogins(profile, num, offset)
248 // GetToken returns the Token specified from the tokenStore associated with the Context.
249 // If refresh is true, the token input should be compared against the refresh tokens, not the
251 func (c Context) GetToken(token string, refresh bool) (Token, error) {
253 return Token{}, ErrNoTokenStore
255 return c.tokens.getToken(token, refresh)
258 // SaveToken stores the passed Token in the tokenStore associated with the Context.
259 func (c Context) SaveToken(token Token) error {
261 return ErrNoTokenStore
263 return c.tokens.saveToken(token)
266 // RemoveToken removes the Token identified by the passed token string from the tokenStore associated
268 func (c Context) RemoveToken(token string) error {
270 return ErrNoTokenStore
272 return c.tokens.removeToken(token)
275 // GetTokensByProfileID returns a slice of up to num Tokens with a ProfileID that matches the specified
276 // profileID from the tokenStore associated with the Context, skipping offset Tokens.
277 func (c Context) GetTokensByProfileID(profileID uuid.ID, num, offset int) ([]Token, error) {
279 return []Token{}, ErrNoTokenStore
281 return c.tokens.getTokensByProfileID(profileID, num, offset)
284 // CreateSession stores the passed Session in the sessionStore associated with the Context.
285 func (c Context) CreateSession(session Session) error {
286 if c.sessions == nil {
287 return ErrNoSessionStore
289 return c.sessions.createSession(session)
292 // GetSession returns the Session specified from the sessionStore associated with the Context.
293 func (c Context) GetSession(id string) (Session, error) {
294 if c.sessions == nil {
295 return Session{}, ErrNoSessionStore
297 return c.sessions.getSession(id)
300 // RemoveSession removes the Session identified by the passed ID from the sessionStore associated with
302 func (c Context) RemoveSession(id string) error {
303 if c.sessions == nil {
304 return ErrNoSessionStore
306 return c.sessions.removeSession(id)
309 // ListSessions returns a slice of up to num Sessions from the sessionStore associated with the Context,
310 // ordered by the date they were created, descending. If before.IsZero() returns false, only Sessions
311 // that were created before that time will be returned. If profile is not nil, only Sessions belonging to
312 // that Profile will be returned.
313 func (c Context) ListSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
314 if c.sessions != nil {
315 return []Session{}, ErrNoSessionStore
317 return c.sessions.listSessions(profile, before, num)