mirror of
https://github.com/volatiletech/authboss.git
synced 2025-09-16 09:06:20 +02:00
Make an GetErr version of the ClientStorer.
This commit is contained in:
@@ -23,6 +23,26 @@ type ClientStorer interface {
|
|||||||
Del(key string)
|
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
|
// 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.
|
// security considerations for your implementation, Secure, HTTP-Only, etc flags.
|
||||||
type CookieStoreMaker func(http.ResponseWriter, *http.Request) ClientStorer
|
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
|
// need for context is a request's session store. It is not safe for use by
|
||||||
// multiple goroutines.
|
// multiple goroutines.
|
||||||
type Context struct {
|
type Context struct {
|
||||||
SessionStorer ClientStorer
|
SessionStorer ClientStorerErr
|
||||||
CookieStorer ClientStorer
|
CookieStorer ClientStorerErr
|
||||||
User Attributes
|
User Attributes
|
||||||
|
|
||||||
postFormValues map[string][]string
|
postFormValues map[string][]string
|
||||||
|
@@ -194,6 +194,17 @@ func (m *MockClientStorer) Get(key string) (string, bool) {
|
|||||||
v, ok := m.Values[key]
|
v, ok := m.Values[key]
|
||||||
return v, ok
|
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) Put(key, val string) { m.Values[key] = val }
|
||||||
func (m *MockClientStorer) Del(key string) { delete(m.Values, key) }
|
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]
|
v, ok := m[key]
|
||||||
return v, ok
|
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) Put(key, val string) { m[key] = val }
|
||||||
func (m mockClientStore) Del(key string) { delete(m, key) }
|
func (m mockClientStore) Del(key string) { delete(m, key) }
|
||||||
|
|
||||||
|
@@ -37,8 +37,8 @@ func (c contextRoute) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.CookieStorer = Cfg.CookieStoreMaker(w, r)
|
ctx.CookieStorer = clientStoreWrapper{Cfg.CookieStoreMaker(w, r)}
|
||||||
ctx.SessionStorer = Cfg.SessionStoreMaker(w, r)
|
ctx.SessionStorer = clientStoreWrapper{Cfg.SessionStoreMaker(w, r)}
|
||||||
|
|
||||||
err = c.fn(ctx, w, r)
|
err = c.fn(ctx, w, r)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
Reference in New Issue
Block a user