2015-01-03 22:03:57 +02:00
|
|
|
/*
|
|
|
|
Package authboss is a modular authentication system for the web. It tries to
|
|
|
|
remove as much boilerplate and "hard things" as possible so that each time you
|
|
|
|
start a new web project in Go, you can plug it in, configure and be off to the
|
2015-03-17 06:38:00 +02:00
|
|
|
races without having to think about how to store passwords or remember tokens.
|
2015-01-03 22:03:57 +02:00
|
|
|
*/
|
2016-12-20 08:43:51 +02:00
|
|
|
package authboss
|
2015-01-05 10:18:41 +02:00
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
import "github.com/pkg/errors"
|
2015-01-05 10:18:41 +02:00
|
|
|
|
2015-03-31 21:34:03 +02:00
|
|
|
// Authboss contains a configuration and other details for running.
|
|
|
|
type Authboss struct {
|
|
|
|
Config
|
2018-02-02 02:31:08 +02:00
|
|
|
Events *Events
|
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
loadedModules map[string]Moduler
|
2015-03-31 21:34:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// New makes a new instance of authboss with a default
|
|
|
|
// configuration.
|
|
|
|
func New() *Authboss {
|
2018-02-01 03:07:11 +02:00
|
|
|
ab := &Authboss{}
|
2018-02-02 02:31:08 +02:00
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
ab.loadedModules = make(map[string]Moduler)
|
2018-02-02 02:31:08 +02:00
|
|
|
ab.Events = NewEvents()
|
2018-02-02 01:42:48 +02:00
|
|
|
|
2015-04-01 00:08:43 +02:00
|
|
|
ab.Config.Defaults()
|
2015-03-31 21:34:03 +02:00
|
|
|
return ab
|
|
|
|
}
|
|
|
|
|
2018-02-01 03:07:11 +02:00
|
|
|
// Init authboss, modules, renderers
|
2018-02-02 01:42:48 +02:00
|
|
|
func (a *Authboss) Init(modulesToLoad ...string) error {
|
|
|
|
if len(modulesToLoad) == 0 {
|
|
|
|
modulesToLoad = RegisteredModules()
|
2015-01-05 10:18:41 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 01:42:48 +02:00
|
|
|
for _, name := range modulesToLoad {
|
2018-02-02 03:10:26 +02:00
|
|
|
if err := a.loadModule(name); err != nil {
|
|
|
|
return errors.Errorf("module %s failed to load", name)
|
2018-02-02 01:42:48 +02:00
|
|
|
}
|
2017-02-24 02:13:25 +02:00
|
|
|
}
|
2015-01-17 12:42:42 +02:00
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
return nil
|
2015-01-17 12:42:42 +02:00
|
|
|
}
|
2015-03-17 07:58:32 +02:00
|
|
|
|
|
|
|
/*
|
2018-02-01 03:07:11 +02:00
|
|
|
TODO(aarondl): Fixup
|
|
|
|
|
2015-03-17 07:58:32 +02:00
|
|
|
UpdatePassword should be called to recalculate hashes and do any cleanup
|
|
|
|
that should occur on password resets. Updater should return an error if the
|
|
|
|
update to the user failed (for reasons say like validation, duplicate
|
|
|
|
primary key, etc...). In that case the cleanup will not be performed.
|
|
|
|
|
|
|
|
The w and r parameters are for establishing session and cookie storers.
|
|
|
|
|
2015-03-27 18:44:20 +02:00
|
|
|
The ptPassword parameter is the new password to update to. updater is called
|
|
|
|
regardless if this is empty or not, but if it is empty, it will not set a new
|
|
|
|
password before calling updater.
|
2015-03-17 07:58:32 +02:00
|
|
|
|
|
|
|
The user parameter is the user struct which will have it's
|
|
|
|
Password string/sql.NullString value set to the new bcrypted password. Therefore
|
|
|
|
it must be passed in as a pointer with the Password field exported or an error
|
|
|
|
will be returned.
|
|
|
|
|
|
|
|
The error returned is returned either from the updater if that produced an error
|
|
|
|
or from the cleanup routines.
|
2015-03-31 21:34:03 +02:00
|
|
|
func (a *Authboss) UpdatePassword(w http.ResponseWriter, r *http.Request,
|
2017-02-24 02:13:25 +02:00
|
|
|
ptPassword string, user Storer, updater func() error) error {
|
|
|
|
|
|
|
|
/*updatePwd := len(ptPassword) > 0
|
2015-03-17 07:58:32 +02:00
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
if updatePwd {
|
|
|
|
pass, err := bcrypt.GenerateFromPassword([]byte(ptPassword), a.BCryptCost)
|
|
|
|
if err != nil {
|
2017-02-22 01:04:30 +02:00
|
|
|
return err
|
2015-03-17 07:58:32 +02:00
|
|
|
}
|
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
user.PutPassword(r.Context(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := updater(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-03-17 07:58:32 +02:00
|
|
|
|
2017-02-24 02:13:25 +02:00
|
|
|
if !updatePwd {
|
|
|
|
return nil
|
|
|
|
}
|
2015-03-17 07:58:32 +02:00
|
|
|
|
2018-02-02 02:31:08 +02:00
|
|
|
return a.Events.FireAfter(EventPasswordReset, r.Context())
|
2017-02-24 02:13:25 +02:00
|
|
|
// TODO(aarondl): Fix
|
|
|
|
return errors.New("not implemented")
|
2015-03-17 07:58:32 +02:00
|
|
|
}
|
2018-02-01 03:07:11 +02:00
|
|
|
|
|
|
|
*/
|