2015-01-11 08:52:39 +02:00
|
|
|
package authboss
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
import "net/http"
|
|
|
|
|
2015-01-11 08:52:39 +02:00
|
|
|
// Context provides context for module operations and callbacks. One obvious
|
|
|
|
// need for context is a request's session store. It is not safe for use by
|
|
|
|
// multiple goroutines.
|
|
|
|
type Context struct {
|
2015-01-13 00:02:07 +02:00
|
|
|
SessionStorer ClientStorer
|
|
|
|
CookieStorer ClientStorer
|
|
|
|
User Attributes
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
postFormValues map[string][]string
|
|
|
|
formValues map[string][]string
|
|
|
|
keyValues map[string]interface{}
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewContext() *Context {
|
|
|
|
return &Context{
|
|
|
|
keyValues: make(map[string]interface{}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
// ContextFromRequest creates a context from an http request.
|
|
|
|
func ContextFromRequest(r *http.Request) (*Context, error) {
|
|
|
|
if err := r.ParseForm(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c := NewContext()
|
|
|
|
c.formValues = map[string][]string(r.Form)
|
|
|
|
c.postFormValues = map[string][]string(r.PostForm)
|
|
|
|
return c, nil
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// Put an arbitrary key-value into the context.
|
2015-01-11 08:52:39 +02:00
|
|
|
func (c *Context) Put(key string, thing interface{}) {
|
|
|
|
c.keyValues[key] = thing
|
|
|
|
}
|
|
|
|
|
2015-01-13 00:02:07 +02:00
|
|
|
// Get an arbitrary key-value from the context.
|
2015-01-11 08:52:39 +02:00
|
|
|
func (c *Context) Get(key string) (thing interface{}, ok bool) {
|
|
|
|
thing, ok = c.keyValues[key]
|
|
|
|
return thing, ok
|
|
|
|
}
|
2015-01-15 05:18:45 +02:00
|
|
|
|
|
|
|
// FormValue gets a form value from a context created with a request.
|
|
|
|
func (c *Context) FormValue(key string) ([]string, bool) {
|
|
|
|
val, ok := c.formValues[key]
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// PostFormValue gets a form value from a context created with a request.
|
|
|
|
func (c *Context) PostFormValue(key string) ([]string, bool) {
|
|
|
|
val, ok := c.postFormValues[key]
|
|
|
|
return val, ok
|
|
|
|
}
|
|
|
|
|
2015-01-16 00:01:01 +02:00
|
|
|
// FirstFormValue gets the first form value from a context created with a request.
|
|
|
|
func (c *Context) FirstFormValue(key string) (string, bool) {
|
|
|
|
val, ok := c.formValues[key]
|
|
|
|
|
|
|
|
if !ok || len(val) == 0 || len(val[0]) == 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return val[0], ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// FirstPostFormValue gets the first form value from a context created with a request.
|
|
|
|
func (c *Context) FirstPostFormValue(key string) (string, bool) {
|
|
|
|
val, ok := c.postFormValues[key]
|
|
|
|
|
|
|
|
if !ok || len(val) == 0 || len(val[0]) == 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
return val[0], ok
|
|
|
|
}
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
// LoadUser loads the user Attributes if they haven't already been loaded.
|
2015-01-15 12:56:13 +02:00
|
|
|
func (c *Context) LoadUser(key string, storer Storer) error {
|
2015-01-15 05:18:45 +02:00
|
|
|
if c.User != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 12:56:13 +02:00
|
|
|
intf, err := storer.Get(key, moduleAttrMeta)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.User = Unbind(intf)
|
2015-01-15 05:18:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-01-17 07:30:04 +02:00
|
|
|
|
|
|
|
// SaveUser saves the user Attributes.
|
|
|
|
func (c *Context) SaveUser(key string, storer Storer) error {
|
|
|
|
if c.User == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return storer.Put(key, c.User)
|
|
|
|
}
|