1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-09 13:47:09 +02:00

Make an GetErr version of the ClientStorer.

This commit is contained in:
Aaron 2015-02-20 22:02:55 -08:00
parent 06c5e686a1
commit 5f96e8dec8
5 changed files with 42 additions and 4 deletions

View File

@ -23,6 +23,26 @@ type ClientStorer interface {
Del(key string)
}
// ClientStorerErr is a wrapper to return error values from failed Gets.
type ClientStorerErr interface {
ClientStorer
GetErr(key string) (string, error)
}
type clientStoreWrapper struct {
ClientStorer
}
// GetErr returns a value or an error.
func (c clientStoreWrapper) GetErr(key string) (string, error) {
str, ok := c.Get(key)
if !ok {
return str, ClientDataErr{key}
}
return str, nil
}
// CookieStoreMaker is used to create a cookie storer from an http request. Keep in mind
// security considerations for your implementation, Secure, HTTP-Only, etc flags.
type CookieStoreMaker func(http.ResponseWriter, *http.Request) ClientStorer

View File

@ -13,8 +13,8 @@ import (
// need for context is a request's session store. It is not safe for use by
// multiple goroutines.
type Context struct {
SessionStorer ClientStorer
CookieStorer ClientStorer
SessionStorer ClientStorerErr
CookieStorer ClientStorerErr
User Attributes
postFormValues map[string][]string

View File

@ -194,6 +194,17 @@ func (m *MockClientStorer) Get(key string) (string, bool) {
v, ok := m.Values[key]
return v, ok
}
func (m *MockClientStorer) GetErr(key string) (string, error) {
if m.GetShouldFail {
return "", authboss.ClientDataErr{key}
}
v, ok := m.Values[key]
if !ok {
return v, authboss.ClientDataErr{key}
}
return v, ok
}
func (m *MockClientStorer) Put(key, val string) { m.Values[key] = val }
func (m *MockClientStorer) Del(key string) { delete(m.Values, key) }

View File

@ -35,6 +35,13 @@ func (m mockClientStore) Get(key string) (string, bool) {
v, ok := m[key]
return v, ok
}
func (m mockClientStore) GetErr(key string) (string, error) {
v, ok := m[key]
if !ok {
return v, ClientDataErr{key}
}
return v, nil
}
func (m mockClientStore) Put(key, val string) { m[key] = val }
func (m mockClientStore) Del(key string) { delete(m, key) }

View File

@ -37,8 +37,8 @@ func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
ctx.CookieStorer = Cfg.CookieStoreMaker(w, r)
ctx.SessionStorer = Cfg.SessionStoreMaker(w, r)
ctx.CookieStorer = clientStoreWrapper{Cfg.CookieStoreMaker(w, r)}
ctx.SessionStorer = clientStoreWrapper{Cfg.SessionStoreMaker(w, r)}
err = c.fn(ctx, w, r)
if err == nil {