Skip to content

Commit e74112f

Browse files
committed
Add two factor events
1 parent 89680e4 commit e74112f

File tree

5 files changed

+60
-2
lines changed

5 files changed

+60
-2
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
44
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
55

6+
## [3.2.0] - 2021-08-11
7+
8+
### Added
9+
10+
- Add additional events so users can take domain-specific actions when a user
11+
adds or removes 2fa.
12+
613
## [3.1.1] - 2021-07-01
714

815
### Fixed

events.go

+2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ const (
3030
// Deprecated: EventPasswordReset is used nowhere
3131
EventPasswordReset
3232
EventLogout
33+
EventTwoFactorAdded
34+
EventTwoFactorRemoved
3335
)
3436

3537
// EventHandler reacts to events that are fired by Authboss controllers.

otp/twofactor/sms2fa/sms.go

+15
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,14 @@ func (s *SMSValidator) validateCode(w http.ResponseWriter, r *http.Request, user
438438

439439
logger.Infof("user %s enabled sms 2fa", user.GetPID())
440440
data = authboss.HTMLData{twofactor.DataRecoveryCodes: codes}
441+
442+
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
443+
if handled, err := s.Authboss.Events.FireAfter(authboss.EventTwoFactorAdded, w, r); err != nil {
444+
return err
445+
} else if handled {
446+
return nil
447+
}
448+
441449
case PageSMSRemove:
442450
user.PutSMSPhoneNumber("")
443451
if err := s.Authboss.Config.Storage.Server.Save(r.Context(), user); err != nil {
@@ -446,6 +454,13 @@ func (s *SMSValidator) validateCode(w http.ResponseWriter, r *http.Request, user
446454

447455
authboss.DelSession(w, authboss.Session2FA)
448456

457+
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
458+
if handled, err := s.Authboss.Events.FireAfter(authboss.EventTwoFactorRemoved, w, r); err != nil {
459+
return err
460+
} else if handled {
461+
return nil
462+
}
463+
449464
logger.Infof("user %s disabled sms 2fa", user.GetPID())
450465
case PageSMSValidate:
451466
authboss.PutSession(w, authboss.SessionKey, user.GetPID())

otp/twofactor/totp2fa/totp.go

+14
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ func (t *TOTP) PostConfirm(w http.ResponseWriter, r *http.Request) error {
310310
logger := t.RequestLogger(r)
311311
logger.Infof("user %s enabled totp 2fa", user.GetPID())
312312

313+
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
314+
if handled, err := t.Authboss.Events.FireAfter(authboss.EventTwoFactorAdded, w, r); err != nil {
315+
return err
316+
} else if handled {
317+
return nil
318+
}
319+
313320
data := authboss.HTMLData{twofactor.DataRecoveryCodes: codes}
314321
return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPConfirmSuccess, data)
315322
}
@@ -346,6 +353,13 @@ func (t *TOTP) PostRemove(w http.ResponseWriter, r *http.Request) error {
346353

347354
logger.Infof("user %s disabled totp 2fa", user.GetPID())
348355

356+
r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user))
357+
if handled, err := t.Authboss.Events.FireAfter(authboss.EventTwoFactorRemoved, w, r); err != nil {
358+
return err
359+
} else if handled {
360+
return nil
361+
}
362+
349363
return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPRemoveSuccess, nil)
350364
}
351365

stringers.go

+22-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)