2015-01-11 08:52:39 +02:00
|
|
|
package authboss
|
|
|
|
|
2015-01-19 00:35:44 +02:00
|
|
|
import (
|
|
|
|
"errors"
|
2015-01-28 10:32:28 +02:00
|
|
|
"strings"
|
2015-01-19 00:35:44 +02:00
|
|
|
)
|
2015-01-15 05:18:45 +02:00
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// FormValue constants
|
2015-03-14 01:23:43 +02:00
|
|
|
var (
|
|
|
|
FormValueRedirect = "redir"
|
|
|
|
FormValueOAuth2State = "state"
|
|
|
|
)
|
|
|
|
|
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-03-31 21:34:03 +02:00
|
|
|
*Authboss
|
|
|
|
|
2015-02-21 08:02:55 +02:00
|
|
|
SessionStorer ClientStorerErr
|
|
|
|
CookieStorer ClientStorerErr
|
2015-01-13 00:02:07 +02:00
|
|
|
User Attributes
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-03-16 23:42:45 +02:00
|
|
|
// NewContext is exported for testing modules.
|
2015-03-31 21:34:03 +02:00
|
|
|
func (a *Authboss) NewContext() *Context {
|
|
|
|
return &Context{
|
|
|
|
Authboss: a,
|
|
|
|
}
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-15 05:18:45 +02:00
|
|
|
// LoadUser loads the user Attributes if they haven't already been loaded.
|
2015-02-18 18:45:27 +02:00
|
|
|
func (c *Context) LoadUser(key string) error {
|
2015-01-15 05:18:45 +02:00
|
|
|
if c.User != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-03-14 01:23:43 +02:00
|
|
|
var user interface{}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
if index := strings.IndexByte(key, ';'); index > 0 {
|
2015-03-31 21:34:03 +02:00
|
|
|
user, err = c.OAuth2Storer.GetOAuth(key[:index], key[index+1:])
|
2015-03-14 01:23:43 +02:00
|
|
|
} else {
|
2015-03-31 21:34:03 +02:00
|
|
|
user, err = c.Storer.Get(key)
|
2015-03-14 01:23:43 +02:00
|
|
|
}
|
2015-01-15 12:56:13 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-03-14 01:23:43 +02:00
|
|
|
c.User = Unbind(user)
|
2015-01-15 05:18:45 +02:00
|
|
|
return nil
|
|
|
|
}
|
2015-01-17 07:30:04 +02:00
|
|
|
|
2015-02-18 18:45:27 +02:00
|
|
|
// LoadSessionUser loads the user from the session if the user has not already been
|
|
|
|
// loaded.
|
|
|
|
func (c *Context) LoadSessionUser() error {
|
|
|
|
if c.User != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
key, ok := c.SessionStorer.Get(SessionKey)
|
|
|
|
if !ok {
|
|
|
|
return ErrUserNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.LoadUser(key)
|
|
|
|
}
|
|
|
|
|
2015-01-17 07:30:04 +02:00
|
|
|
// SaveUser saves the user Attributes.
|
2015-02-18 18:45:27 +02:00
|
|
|
func (c *Context) SaveUser() error {
|
2015-01-17 07:30:04 +02:00
|
|
|
if c.User == nil {
|
2015-01-19 00:35:44 +02:00
|
|
|
return errors.New("User not initialized.")
|
2015-01-17 07:30:04 +02:00
|
|
|
}
|
|
|
|
|
2015-03-31 21:34:03 +02:00
|
|
|
key, ok := c.User.String(c.PrimaryID)
|
2015-02-18 18:45:27 +02:00
|
|
|
if !ok {
|
|
|
|
return errors.New("User improperly initialized, primary ID missing")
|
|
|
|
}
|
|
|
|
|
2015-03-31 21:34:03 +02:00
|
|
|
return c.Storer.Put(key, c.User)
|
2015-01-17 07:30:04 +02:00
|
|
|
}
|