2015-01-10 22:52:39 -08:00
|
|
|
package authboss
|
|
|
|
|
2015-01-12 14:02:07 -08:00
|
|
|
import "net/http"
|
|
|
|
|
2015-01-15 16:04:33 -08:00
|
|
|
const (
|
|
|
|
// SessionKey is the primarily used key by authboss.
|
|
|
|
SessionKey = "uid"
|
2015-03-16 14:42:45 -07:00
|
|
|
// SessionHalfAuthKey is used for sessions that have been authenticated by
|
2015-01-15 16:04:33 -08:00
|
|
|
// the remember module. This serves as a way to force full authentication
|
|
|
|
// by denying half-authed users acccess to sensitive areas.
|
2015-02-22 13:16:11 -08:00
|
|
|
SessionHalfAuthKey = "halfauth"
|
2015-03-02 08:04:31 -08:00
|
|
|
// SessionLastAction is the session key to retrieve the last action of a user.
|
|
|
|
SessionLastAction = "last_action"
|
2015-03-12 19:20:36 -07:00
|
|
|
// SessionOAuth2State is the xsrf protection key for oauth.
|
2015-03-13 02:15:58 -07:00
|
|
|
SessionOAuth2State = "oauth2_state"
|
2015-03-24 19:39:20 -07:00
|
|
|
// SessionOAuth2Params is the additional settings for oauth like redirection/remember.
|
|
|
|
SessionOAuth2Params = "oauth2_params"
|
2015-03-02 08:04:31 -08:00
|
|
|
|
2015-03-02 22:09:32 -08:00
|
|
|
// CookieRemember is used for cookies and form input names.
|
|
|
|
CookieRemember = "rm"
|
|
|
|
|
2015-01-25 22:58:50 -08:00
|
|
|
// FlashSuccessKey is used for storing sucess flash messages on the session
|
|
|
|
FlashSuccessKey = "flash_success"
|
|
|
|
// FlashErrorKey is used for storing sucess flash messages on the session
|
|
|
|
FlashErrorKey = "flash_error"
|
2015-01-15 16:04:33 -08:00
|
|
|
)
|
2015-01-12 14:02:07 -08:00
|
|
|
|
|
|
|
// ClientStorer should be able to store values on the clients machine. Cookie and
|
|
|
|
// Session storers are built with this interface.
|
2015-01-10 22:52:39 -08:00
|
|
|
type ClientStorer interface {
|
|
|
|
Put(key, value string)
|
2015-01-12 14:02:07 -08:00
|
|
|
Get(key string) (string, bool)
|
2015-01-15 16:04:33 -08:00
|
|
|
Del(key string)
|
2015-01-10 22:52:39 -08:00
|
|
|
}
|
2015-01-12 14:02:07 -08:00
|
|
|
|
2015-02-20 22:02:55 -08:00
|
|
|
// 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
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:02:07 -08:00
|
|
|
// 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.
|
2015-01-15 14:01:01 -08:00
|
|
|
type CookieStoreMaker func(http.ResponseWriter, *http.Request) ClientStorer
|
2015-01-12 14:02:07 -08:00
|
|
|
|
|
|
|
// SessionStoreMaker is used to create a session storer from an http request.
|
|
|
|
// It must be implemented to satisfy certain modules (auth, remember primarily).
|
|
|
|
// It should be a secure storage of the session. This means if it represents a cookie-based session
|
|
|
|
// storage these cookies should be signed in order to prevent tampering, or they should be encrypted.
|
2015-01-15 14:01:01 -08:00
|
|
|
type SessionStoreMaker func(http.ResponseWriter, *http.Request) ClientStorer
|
2015-02-26 22:01:53 -08:00
|
|
|
|
|
|
|
// FlashSuccess returns FlashSuccessKey from the session and removes it.
|
2015-03-31 12:34:03 -07:00
|
|
|
func (a *Authboss) FlashSuccess(w http.ResponseWriter, r *http.Request) string {
|
|
|
|
storer := a.SessionStoreMaker(w, r)
|
2015-02-26 22:01:53 -08:00
|
|
|
msg, ok := storer.Get(FlashSuccessKey)
|
|
|
|
if ok {
|
|
|
|
storer.Del(FlashSuccessKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|
|
|
|
|
|
|
|
// FlashError returns FlashError from the session and removes it.
|
2015-03-31 12:34:03 -07:00
|
|
|
func (a *Authboss) FlashError(w http.ResponseWriter, r *http.Request) string {
|
|
|
|
storer := a.SessionStoreMaker(w, r)
|
2015-02-26 22:01:53 -08:00
|
|
|
msg, ok := storer.Get(FlashErrorKey)
|
|
|
|
if ok {
|
|
|
|
storer.Del(FlashErrorKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
return msg
|
|
|
|
}
|