From e74112f6170d67dafda5c9a9033bda38cd9df4e5 Mon Sep 17 00:00:00 2001 From: Aaron L Date: Wed, 11 Aug 2021 19:42:30 -0700 Subject: [PATCH] Add two factor events --- CHANGELOG.md | 7 +++++++ events.go | 2 ++ otp/twofactor/sms2fa/sms.go | 15 +++++++++++++++ otp/twofactor/totp2fa/totp.go | 14 ++++++++++++++ stringers.go | 24 ++++++++++++++++++++++-- 5 files changed, 60 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index b9b922ce..238cb157 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,13 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [3.2.0] - 2021-08-11 + +### Added + +- Add additional events so users can take domain-specific actions when a user + adds or removes 2fa. + ## [3.1.1] - 2021-07-01 ### Fixed diff --git a/events.go b/events.go index 10fc75d2..52b4b520 100644 --- a/events.go +++ b/events.go @@ -30,6 +30,8 @@ const ( // Deprecated: EventPasswordReset is used nowhere EventPasswordReset EventLogout + EventTwoFactorAdded + EventTwoFactorRemoved ) // EventHandler reacts to events that are fired by Authboss controllers. diff --git a/otp/twofactor/sms2fa/sms.go b/otp/twofactor/sms2fa/sms.go index b8241d2f..43989432 100644 --- a/otp/twofactor/sms2fa/sms.go +++ b/otp/twofactor/sms2fa/sms.go @@ -438,6 +438,14 @@ func (s *SMSValidator) validateCode(w http.ResponseWriter, r *http.Request, user logger.Infof("user %s enabled sms 2fa", user.GetPID()) data = authboss.HTMLData{twofactor.DataRecoveryCodes: codes} + + r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user)) + if handled, err := s.Authboss.Events.FireAfter(authboss.EventTwoFactorAdded, w, r); err != nil { + return err + } else if handled { + return nil + } + case PageSMSRemove: user.PutSMSPhoneNumber("") 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 authboss.DelSession(w, authboss.Session2FA) + r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user)) + if handled, err := s.Authboss.Events.FireAfter(authboss.EventTwoFactorRemoved, w, r); err != nil { + return err + } else if handled { + return nil + } + logger.Infof("user %s disabled sms 2fa", user.GetPID()) case PageSMSValidate: authboss.PutSession(w, authboss.SessionKey, user.GetPID()) diff --git a/otp/twofactor/totp2fa/totp.go b/otp/twofactor/totp2fa/totp.go index 41da2f3d..60fc3e22 100644 --- a/otp/twofactor/totp2fa/totp.go +++ b/otp/twofactor/totp2fa/totp.go @@ -310,6 +310,13 @@ func (t *TOTP) PostConfirm(w http.ResponseWriter, r *http.Request) error { logger := t.RequestLogger(r) logger.Infof("user %s enabled totp 2fa", user.GetPID()) + r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user)) + if handled, err := t.Authboss.Events.FireAfter(authboss.EventTwoFactorAdded, w, r); err != nil { + return err + } else if handled { + return nil + } + data := authboss.HTMLData{twofactor.DataRecoveryCodes: codes} return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPConfirmSuccess, data) } @@ -346,6 +353,13 @@ func (t *TOTP) PostRemove(w http.ResponseWriter, r *http.Request) error { logger.Infof("user %s disabled totp 2fa", user.GetPID()) + r = r.WithContext(context.WithValue(r.Context(), authboss.CTXKeyUser, user)) + if handled, err := t.Authboss.Events.FireAfter(authboss.EventTwoFactorRemoved, w, r); err != nil { + return err + } else if handled { + return nil + } + return t.Authboss.Core.Responder.Respond(w, r, http.StatusOK, PageTOTPRemoveSuccess, nil) } diff --git a/stringers.go b/stringers.go index 5cdb2203..0e12f4dd 100644 --- a/stringers.go +++ b/stringers.go @@ -4,9 +4,29 @@ package authboss import "strconv" -const _Event_name = "EventRegisterEventAuthEventAuthHijackEventOAuth2EventAuthFailEventOAuth2FailEventRecoverStartEventRecoverEndEventGetUserEventGetUserSessionEventPasswordReset" +func _() { + // An "invalid array index" compiler error signifies that the constant values have changed. + // Re-run the stringer command to generate them again. + var x [1]struct{} + _ = x[EventRegister-0] + _ = x[EventAuth-1] + _ = x[EventAuthHijack-2] + _ = x[EventOAuth2-3] + _ = x[EventAuthFail-4] + _ = x[EventOAuth2Fail-5] + _ = x[EventRecoverStart-6] + _ = x[EventRecoverEnd-7] + _ = x[EventGetUser-8] + _ = x[EventGetUserSession-9] + _ = x[EventPasswordReset-10] + _ = x[EventLogout-11] + _ = x[EventTwoFactorAdded-12] + _ = x[EventTwoFactorRemoved-13] +} + +const _Event_name = "EventRegisterEventAuthEventAuthHijackEventOAuth2EventAuthFailEventOAuth2FailEventRecoverStartEventRecoverEndEventGetUserEventGetUserSessionEventPasswordResetEventLogoutEventTwoFactorAddedEventTwoFactorRemoved" -var _Event_index = [...]uint8{0, 13, 22, 37, 48, 61, 76, 93, 108, 120, 139, 157} +var _Event_index = [...]uint8{0, 13, 22, 37, 48, 61, 76, 93, 108, 120, 139, 157, 168, 187, 208} func (i Event) String() string { if i < 0 || i >= Event(len(_Event_index)-1) {