2015-01-11 08:52:39 +02:00
|
|
|
package authboss
|
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
)
|
2015-02-21 09:01:45 +02:00
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
//go:generate stringer -output stringers.go -type "Event"
|
2015-04-11 04:28:11 +02:00
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
// Event type is for describing events
|
2015-01-13 00:02:07 +02:00
|
|
|
type Event int
|
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
// Event kinds
|
2015-01-13 00:02:07 +02:00
|
|
|
const (
|
|
|
|
EventRegister Event = iota
|
|
|
|
EventAuth
|
2018-03-09 04:39:51 +02:00
|
|
|
EventOAuth2
|
2015-01-24 01:56:24 +02:00
|
|
|
EventAuthFail
|
2018-03-09 04:39:51 +02:00
|
|
|
EventOAuth2Fail
|
2015-01-21 08:41:31 +02:00
|
|
|
EventRecoverStart
|
|
|
|
EventRecoverEnd
|
2015-09-22 05:53:51 +02:00
|
|
|
EventGetUser
|
2015-03-02 06:40:09 +02:00
|
|
|
EventGetUserSession
|
2015-03-06 06:05:47 +02:00
|
|
|
EventPasswordReset
|
2015-01-13 00:02:07 +02:00
|
|
|
)
|
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
// EventHandler reacts to events that are fired by Authboss controllers.
|
|
|
|
// These controllers will normally process a request by themselves, but if
|
|
|
|
// there is special consideration for example a successful login, but the
|
|
|
|
// user is locked, the lock module's controller may seize control over the
|
|
|
|
// request.
|
|
|
|
//
|
|
|
|
// Very much a controller level middleware.
|
|
|
|
type EventHandler func(w http.ResponseWriter, r *http.Request, handled bool) (bool, error)
|
2015-01-11 08:52:39 +02:00
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
// Events is a collection of Events that fire before and after certain methods.
|
2018-02-02 02:31:08 +02:00
|
|
|
type Events struct {
|
2018-02-16 19:49:07 +02:00
|
|
|
before map[Event][]EventHandler
|
|
|
|
after map[Event][]EventHandler
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 02:31:08 +02:00
|
|
|
// NewEvents creates a new set of before and after Events.
|
|
|
|
func NewEvents() *Events {
|
|
|
|
return &Events{
|
2018-02-16 19:49:07 +02:00
|
|
|
before: make(map[Event][]EventHandler),
|
|
|
|
after: make(map[Event][]EventHandler),
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-24 01:56:24 +02:00
|
|
|
// Before event, call f.
|
2018-02-16 19:49:07 +02:00
|
|
|
func (c *Events) Before(e Event, f EventHandler) {
|
|
|
|
events := c.before[e]
|
|
|
|
events = append(events, f)
|
|
|
|
c.before[e] = events
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2015-01-24 01:56:24 +02:00
|
|
|
// After event, call f.
|
2018-02-16 19:49:07 +02:00
|
|
|
func (c *Events) After(e Event, f EventHandler) {
|
|
|
|
events := c.after[e]
|
|
|
|
events = append(events, f)
|
|
|
|
c.after[e] = events
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
// FireBefore executes the handlers that were registered to fire before
|
|
|
|
// the event passed in.
|
|
|
|
//
|
|
|
|
// If it encounters an error it will stop immediately without calling
|
|
|
|
// other handlers.
|
|
|
|
//
|
|
|
|
// If a handler handles the request, it will pass this information both
|
|
|
|
// to handlers further down the chain (to let them know that w has been used)
|
|
|
|
// as well as set w to nil as a precaution.
|
|
|
|
func (c *Events) FireBefore(e Event, w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
|
|
return c.call(c.before[e], w, r)
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
|
|
|
|
2018-02-02 02:31:08 +02:00
|
|
|
// FireAfter event to all the Events with a context. The error can safely be
|
2015-02-21 09:01:45 +02:00
|
|
|
// ignored as it is logged.
|
2018-02-16 19:49:07 +02:00
|
|
|
func (c *Events) FireAfter(e Event, w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
|
|
return c.call(c.after[e], w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Events) call(evs []EventHandler, w http.ResponseWriter, r *http.Request) (bool, error) {
|
|
|
|
handled := false
|
|
|
|
|
|
|
|
for _, fn := range evs {
|
|
|
|
interrupt, err := fn(w, r, handled)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if interrupt {
|
|
|
|
handled = true
|
2015-02-20 00:34:29 +02:00
|
|
|
}
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|
2015-02-20 14:03:22 +02:00
|
|
|
|
2018-02-16 19:49:07 +02:00
|
|
|
return handled, nil
|
2015-01-11 08:52:39 +02:00
|
|
|
}
|