1
0
mirror of https://github.com/volatiletech/authboss.git synced 2024-11-28 08:58:38 +02:00

Working auth and recover

This commit is contained in:
Kris Runzer 2015-02-24 10:12:23 -08:00
parent 199d0ec0b3
commit 468113a398
2 changed files with 18 additions and 28 deletions

View File

@ -23,10 +23,7 @@ func init() {
}
type Auth struct {
templates render.Templates
policies []authboss.Validator
isRememberLoaded bool
isRecoverLoaded bool
templates render.Templates
}
func (a *Auth) Initialize() (err error) {
@ -39,11 +36,6 @@ func (a *Auth) Initialize() (err error) {
return err
}
a.policies = authboss.FilterValidators(authboss.Cfg.Policies, authboss.Cfg.PrimaryID, authboss.StorePassword)
a.isRememberLoaded = authboss.IsLoaded("remember")
a.isRecoverLoaded = authboss.IsLoaded("recover")
return nil
}
@ -71,8 +63,8 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
}
data := authboss.NewHTMLData(
"showRemember", a.isRememberLoaded,
"showRecover", a.isRecoverLoaded,
"showRemember", authboss.IsLoaded("remember"),
"showRecover", authboss.IsLoaded("recover"),
"primaryID", authboss.Cfg.PrimaryID,
"primaryIDValue", "",
)
@ -100,11 +92,12 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
"error", fmt.Sprintf("invalid %s and/or password", authboss.Cfg.PrimaryID),
"primaryID", authboss.Cfg.PrimaryID,
"primaryIDValue", key,
"showRemember", a.isRememberLoaded,
"showRecover", a.isRecoverLoaded,
"showRemember", authboss.IsLoaded("remember"),
"showRecover", authboss.IsLoaded("recover"),
)
if validationErrs := ctx.Validate(a.policies); len(validationErrs) > 0 {
policies := authboss.FilterValidators(authboss.Cfg.Policies, authboss.Cfg.PrimaryID, authboss.StorePassword)
if validationErrs := ctx.Validate(policies); len(validationErrs) > 0 {
fmt.Fprintln(authboss.Cfg.LogWriter, "auth: form validation failed:", validationErrs.Map())
return a.templates.Render(ctx, w, r, tplLogin, errData)
}
@ -114,7 +107,6 @@ func (a *Auth) loginHandlerFunc(ctx *authboss.Context, w http.ResponseWriter, r
return a.templates.Render(ctx, w, r, tplLogin, errData)
}
ctx.SessionStorer.Put(authboss.SessionKey, key)
authboss.Cfg.Callbacks.FireAfter(authboss.EventAuth, ctx)
http.Redirect(w, r, authboss.Cfg.AuthLoginSuccessRoute, http.StatusFound)
default:
@ -138,6 +130,7 @@ func validateCredentials(ctx *authboss.Context, key, password string) error {
return err
}
ctx.SessionStorer.Put(authboss.SessionKey, key)
return nil
}

View File

@ -24,11 +24,8 @@ const (
tplInitHTMLEmail = "recover-html.email"
tplInitTextEmail = "recover-text.email"
storeUsername = "username"
storeRecoverToken = "recover_token"
storeRecoverTokenExpiry = "recover_token_expiry"
storeEmail = "email"
storePassword = "password"
StoreRecoverToken = "recover_token"
StoreRecoverTokenExpiry = "recover_token_expiry"
)
var errRecoveryTokenExpired = errors.New("recovery token expired")
@ -73,11 +70,11 @@ func (r *Recover) Routes() authboss.RouteTable {
}
func (r *Recover) Storage() authboss.StorageOptions {
return authboss.StorageOptions{
storeUsername: authboss.String,
storeRecoverToken: authboss.String,
storeEmail: authboss.String,
storeRecoverTokenExpiry: authboss.String,
storePassword: authboss.String,
authboss.StoreUsername: authboss.String,
authboss.StoreEmail: authboss.String,
authboss.StorePassword: authboss.String,
StoreRecoverToken: authboss.String,
StoreRecoverTokenExpiry: authboss.String,
}
}
@ -115,7 +112,7 @@ func (r *Recover) startHandlerFunc(ctx *authboss.Context, w http.ResponseWriter,
return err
}
email, err := ctx.User.StringErr(storeEmail)
email, err := ctx.User.StringErr(authboss.StoreEmail)
if err != nil {
return err
}
@ -125,8 +122,8 @@ func (r *Recover) startHandlerFunc(ctx *authboss.Context, w http.ResponseWriter,
return err
}
ctx.User[storeRecoverToken] = encodedChecksum
ctx.User[storeRecoverTokenExpiry] = time.Now().Add(authboss.Cfg.RecoverTokenDuration)
ctx.User[StoreRecoverToken] = encodedChecksum
ctx.User[StoreRecoverTokenExpiry] = time.Now().Add(authboss.Cfg.RecoverTokenDuration)
if err := ctx.SaveUser(); err != nil {
return err