1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-02-09 13:47:09 +02:00

Cleanup various sad things.

- Export ModuleAttrMeta so the modules can access it.
- Add a couple new events for later use.
- Fix a few compile errors.
- Prefix err constants with Err.
This commit is contained in:
Aaron 2015-01-23 15:56:24 -08:00
parent b60b667184
commit f537664fd9
8 changed files with 35 additions and 14 deletions

View File

@ -42,6 +42,7 @@ func Init(config *Config) error {
return nil
}
// CurrentUser retrieves the current user from the session and the database.
func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
sessions := cfg.SessionStoreMaker(w, r)
key, ok := sessions.Get(SessionKey)
@ -49,9 +50,26 @@ func CurrentUser(w http.ResponseWriter, r *http.Request) (interface{}, error) {
return nil, nil
}
return cfg.Storer.Get(key, moduleAttrMeta)
ctx, err := ContextFromRequest(r)
if err != nil {
return nil, err
}
err = ctx.LoadUser(key, cfg.Storer)
if err != nil {
return nil, err
}
err = cfg.Callbacks.FireBefore(EventGet, ctx)
if err != nil {
return nil, err
}
return cfg.Storer.Get(key, ModuleAttrMeta)
}
// CurrentUserP retrieves the current user but panics if it's not available for
// any reason.
func CurrentUserP(w http.ResponseWriter, r *http.Request) interface{} {
i, err := CurrentUser(w, r)
if err != nil {

View File

@ -7,8 +7,10 @@ type Event int
const (
EventRegister Event = iota
EventAuth
EventAuthFail
EventRecoverStart
EventRecoverEnd
EventGet
)
// Before callbacks can interrupt the flow by returning an error. This is used to stop
@ -32,14 +34,14 @@ func NewCallbacks() *Callbacks {
}
}
// Before event, call callback.
// Before event, call f.
func (c *Callbacks) Before(e Event, f Before) {
callbacks := c.before[e]
callbacks = append(callbacks, f)
c.before[e] = callbacks
}
// After event, call callback.
// After event, call f.
func (c *Callbacks) After(e Event, f After) {
callbacks := c.after[e]
callbacks = append(callbacks, f)

View File

@ -87,7 +87,7 @@ func (c *Context) LoadUser(key string, storer Storer) error {
return nil
}
intf, err := storer.Get(key, moduleAttrMeta)
intf, err := storer.Get(key, ModuleAttrMeta)
if err != nil {
return err
}

View File

@ -2,7 +2,7 @@ package authboss
var modules = make(map[string]Modularizer)
var moduleAttrMeta = make(AttributeMeta)
var ModuleAttrMeta = make(AttributeMeta)
// Modularizer should be implemented by all the authboss modules.
type Modularizer interface {
@ -17,7 +17,7 @@ func RegisterModule(name string, m Modularizer) {
modules[name] = m
for k, v := range m.Storage() {
moduleAttrMeta[k] = v
ModuleAttrMeta[k] = v
}
}

View File

@ -144,7 +144,7 @@ func (r *Remember) Auth(
sum := md5.Sum(token)
key, err := r.storer.UseToken(string(givenKey), base64.StdEncoding.EncodeToString(sum[:]))
if err == authboss.TokenNotFound {
if err == authboss.ErrTokenNotFound {
return "", nil
} else if err != nil {
return "", err

View File

@ -55,7 +55,7 @@ func (t *testTokenStorer) UseToken(givenKey, token string) (key string, err erro
t.token = ""
return ret, nil
}
return "", authboss.TokenNotFound
return "", authboss.ErrTokenNotFound
}
func TestInitialize(t *testing.T) {

View File

@ -9,11 +9,12 @@ import (
"unicode"
)
// UserNotFound should be returned from Get when the record is not found.
var UserNotFound = errors.New("User not found")
// TokenNotFound should be returned from UseToken when the record is not found.
var TokenNotFound = errors.New("Token not found")
var (
// UserNotFound should be returned from Get when the record is not found.
ErrUserNotFound = errors.New("User not found")
// TokenNotFound should be returned from UseToken when the record is not found.
ErrTokenNotFound = errors.New("Token not found")
)
// StorageOptions is a map depicting the things a module must be able to store.
type StorageOptions map[string]DataType

View File

@ -31,7 +31,7 @@ func (r Rules) Errors(toValidate string) authboss.ErrorList {
ln := len(toValidate)
if ln == 0 {
errs = append(errs, authboss.FieldError{r.Field, errors.New("Cannot be blank")})
return err
return errs
}
if r.MustMatch != nil {