Stub out sessions.
Stop using the Login type when getting profile by Login, removing Logins,
or recording Login use. The Login value has to be unique, anyways, and we don't
actually know the Login type when getting a profile by Login. That's sort of the
point.
Create the concept of Sessions and a sessionStore type to manage our
authentication sessions with the server. As per OWASP, we're basically just
going to use a transparent, SHA256-generated random string as an ID, and store
it client-side and server-side and just pass it back and forth.
Add the ProfileID to the Grant type, because we need to remember who granted
access. That's sort of important.
Set a defaultGrantExpiration constant to an hour, so we have that one constant
when creating new Grants.
Create a helper that pulls the session ID out of an auth cookie, checks it
against the sessionStore, and returns the Session if it's valid.
Create a helper that pulls the username and password out of a basic auth header.
Create a helper that authenticates a user's login and passphrase, checking them
against the profileStore securely.
Stub out how the cookie checking is going to work for getting grant approval.
Fix the stored Grant RedirectURI to be the passed in redirect URI, not the
RedirectURI that we ultimately redirect to. This is in accordance with the spec.
Store the profile ID from our session in the created Grant.
Stub out a GetTokenHandler that will allow users to exchange a Grant for a
Token.
Set a constant for the current passphrase scheme, which we will increment for
each revision to the passphrase scheme, for backwards compatibility.
Change the Profile iterations property to an int, not an int64, to match the
code.secondbit.org/pass library (which is matching the PBKDF2 library).
9 "code.secondbit.org/uuid"
12 // Context wraps the different storage interfaces and should
13 // be used as the main point of interaction for the data storage
16 template *template.Template
24 // Render uses the HTML templates associated with the Context to render the
25 // template specified by name to out using data to fill any template variables.
26 func (c Context) Render(out io.Writer, name string, data interface{}) {
27 if c.template == nil {
28 log.Println("No template set on Context, can't render anything!")
31 err := c.template.ExecuteTemplate(out, name, data)
33 log.Println("Error executing template", name, ":", err)
37 // GetClient returns a single Client by its ID from the
38 // clientStore associated with the Context.
39 func (c Context) GetClient(id uuid.ID) (Client, error) {
41 return Client{}, ErrNoClientStore
43 return c.clients.getClient(id)
46 // SaveClient stores the passed Client in the clientStore
47 // associated with the Context.
48 func (c Context) SaveClient(client Client) error {
50 return ErrNoClientStore
52 return c.clients.saveClient(client)
55 // UpdateClient applies the specified ClientChange to the Client
56 // with the specified ID in the clientStore associated with the
58 func (c Context) UpdateClient(id uuid.ID, change ClientChange) error {
60 return ErrNoClientStore
62 return c.clients.updateClient(id, change)
65 // DeleteClient removes the client with the specified ID from the
66 // clientStore associated with the Context.
67 func (c Context) DeleteClient(id uuid.ID) error {
69 return ErrNoClientStore
71 return c.clients.deleteClient(id)
74 // ListClientsByOwner returns a slice of up to num Clients, starting at offset (inclusive)
75 // that have the specified OwnerID in the clientStore associated with the Context.
76 func (c Context) ListClientsByOwner(ownerID uuid.ID, num, offset int) ([]Client, error) {
78 return []Client{}, ErrNoClientStore
80 return c.clients.listClientsByOwner(ownerID, num, offset)
83 // AddEndpoint stores the specified Endpoint in the clientStore associated with the Context,
84 // and associates the newly-stored Endpoint with the Client specified by the passed ID.
85 func (c Context) AddEndpoint(client uuid.ID, endpoint Endpoint) error {
87 return ErrNoClientStore
89 return c.clients.addEndpoint(client, endpoint)
92 // RemoveEndpoint deletes the Endpoint with the specified ID from the clientStore associated
93 // with the Context, and disassociates the Endpoint from the specified Client.
94 func (c Context) RemoveEndpoint(client, endpoint uuid.ID) error {
96 return ErrNoClientStore
98 return c.clients.removeEndpoint(client, endpoint)
101 // CheckEndpoint finds Endpoints in the clientStore associated with the Context that belong
102 // to the Client specified by the passed ID and match the URI passed. URI matches must be
103 // performed according to RFC 3986 Section 6.
104 func (c Context) CheckEndpoint(client uuid.ID, URI string) (bool, error) {
105 if c.clients == nil {
106 return false, ErrNoClientStore
108 return c.clients.checkEndpoint(client, URI)
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,
114 func (c Context) ListEndpoints(client uuid.ID, num, offset int) ([]Endpoint, error) {
115 if c.clients == nil {
116 return []Endpoint{}, ErrNoClientStore
118 return c.clients.listEndpoints(client, num, offset)
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
127 return c.clients.countEndpoints(client)
130 // GetGrant returns the Grant specified by the provided code from the grantStore associated with the
132 func (c Context) GetGrant(code string) (Grant, error) {
134 return Grant{}, ErrNoGrantStore
136 return c.grants.getGrant(code)
139 // SaveGrant stores the passed Grant in the grantStore associated with the Context.
140 func (c Context) SaveGrant(grant Grant) error {
142 return ErrNoGrantStore
144 return c.grants.saveGrant(grant)
147 // DeleteGrant removes the Grant specified by the provided code from the grantStore associated with
149 func (c Context) DeleteGrant(code string) error {
151 return ErrNoGrantStore
153 return c.grants.deleteGrant(code)
156 // GetProfileByID returns the Profile specified by the provided ID from the profileStore associated with
158 func (c Context) GetProfileByID(id uuid.ID) (Profile, error) {
159 if c.profiles == nil {
160 return Profile{}, ErrNoProfileStore
162 return c.profiles.getProfileByID(id)
165 // GetProfileByLogin returns the Profile associated with the specified Login from the profileStore associated
167 func (c Context) GetProfileByLogin(value string) (Profile, error) {
168 if c.profiles == nil {
169 return Profile{}, ErrNoProfileStore
171 return c.profiles.getProfileByLogin(value)
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
179 return c.profiles.saveProfile(profile)
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
188 return c.profiles.updateProfile(id, change)
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
197 return c.profiles.updateProfiles(ids, change)
200 // DeleteProfile removes the Profile specified by the passed ID from the profileStore associated
202 func (c Context) DeleteProfile(id uuid.ID) error {
203 if c.profiles == nil {
204 return ErrNoProfileStore
206 return c.profiles.deleteProfile(id)
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
215 return c.profiles.addLogin(login)
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(value string, profile uuid.ID) error {
222 if c.profiles == nil {
223 return ErrNoProfileStore
225 return c.profiles.removeLogin(value, profile)
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(value string, when time.Time) error {
231 if c.profiles == nil {
232 return ErrNoProfileStore
234 return c.profiles.recordLoginUse(value, when)
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
243 return c.profiles.listLogins(profile, num, offset)
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
249 func (c Context) GetToken(token string, refresh bool) (Token, error) {
251 return Token{}, ErrNoTokenStore
253 return c.tokens.getToken(token, refresh)
256 // SaveToken stores the passed Token in the tokenStore associated with the Context.
257 func (c Context) SaveToken(token Token) error {
259 return ErrNoTokenStore
261 return c.tokens.saveToken(token)
264 // RemoveToken removes the Token identified by the passed token string from the tokenStore associated
266 func (c Context) RemoveToken(token string) error {
268 return ErrNoTokenStore
270 return c.tokens.removeToken(token)
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) {
277 return []Token{}, ErrNoTokenStore
279 return c.tokens.getTokensByProfileID(profileID, num, offset)
282 // CreateSession stores the passed Session in the sessionStore associated with the Context.
283 func (c Context) CreateSession(session Session) error {
284 if c.sessions == nil {
285 return ErrNoSessionStore
287 return c.sessions.createSession(session)
290 // GetSession returns the Session specified from the sessionStore associated with the Context.
291 func (c Context) GetSession(id string) (Session, error) {
292 if c.sessions == nil {
293 return Session{}, ErrNoSessionStore
295 return c.sessions.getSession(id)
298 // RemoveSession removes the Session identified by the passed ID from the sessionStore associated with
300 func (c Context) RemoveSession(id string) error {
301 if c.sessions == nil {
302 return ErrNoSessionStore
304 return c.sessions.removeSession(id)
307 // ListSessions returns a slice of up to num Sessions from the sessionStore associated with the Context,
308 // ordered by the date they were created, descending. If before.IsZero() returns false, only Sessions
309 // that were created before that time will be returned. If profile is not nil, only Sessions belonging to
310 // that Profile will be returned.
311 func (c Context) ListSessions(profile uuid.ID, before time.Time, num int64) ([]Session, error) {
312 if c.sessions != nil {
313 return []Session{}, ErrNoSessionStore
315 return c.sessions.listSessions(profile, before, num)