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:
parent
06c5e686a1
commit
5f96e8dec8
@ -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
|
||||
|
@ -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
|
||||
|
@ -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) }
|
||||
|
||||
|
@ -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) }
|
||||
|
||||
|
@ -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 {
|
||||
|
Loading…
x
Reference in New Issue
Block a user