1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-03-23 21:50:50 +02:00
authboss/validate/validate.go

83 lines
2.0 KiB
Go

// Package validate supports validation of usernames, email addresses, and passwords.
package validate
import (
"fmt"
"gopkg.in/authboss.v0"
)
var V *Validate
func init() {
V = &Validate{}
authboss.RegisterModule("validate", V)
}
const (
policyEmail = "email"
policyUsername = "username"
policyPassword = "password"
)
type Validate struct {
Email authboss.Validator
Username authboss.Validator
Password authboss.Validator
}
func (v *Validate) Initialize() error {
policies := authboss.FilterValidators(authboss.Cfg.Policies, policyEmail, policyUsername, policyPassword)
if v.Email = policies[0]; v.Email.Field() != policyEmail {
return fmt.Errorf("validate: missin g policy: %s", policyEmail)
}
if v.Username = policies[1]; v.Username.Field() != policyUsername {
return fmt.Errorf("validate: missing policy: %s", policyUsername)
}
if v.Password = policies[2]; v.Password.Field() != policyPassword {
return fmt.Errorf("validate: missing policy: %s", policyPassword)
}
authboss.Cfg.Callbacks.Before(authboss.EventRegister, v.BeforeRegister)
authboss.Cfg.Callbacks.Before(authboss.EventRecoverStart, v.BeforeRegister)
authboss.Cfg.Callbacks.Before(authboss.EventRecoverEnd, v.BeforeRegister)
return nil
}
func (v *Validate) Routes() authboss.RouteTable { return nil }
func (v *Validate) Storage() authboss.StorageOptions { return nil }
func (v *Validate) BeforeRegister(ctx *authboss.Context) error {
errList := make(authboss.ErrorList, 0)
if v.Email != nil {
email, ok := ctx.FirstPostFormValue("email")
if ok {
errs := v.Email.Errors(email)
errList = append(errList, errs...)
}
}
if v.Username != nil {
username, ok := ctx.FirstPostFormValue("username")
if ok {
errs := v.Username.Errors(username)
errList = append(errList, errs...)
}
}
if v.Password != nil {
password, ok := ctx.FirstPostFormValue("password")
if ok {
errs := v.Password.Errors(password)
errList = append(errList, errs...)
}
}
return errList
}