Skip to content

Commit

Permalink
fix: move expiry check into validate factors
Browse files Browse the repository at this point in the history
  • Loading branch information
J0 committed Sep 30, 2024
1 parent 22e230f commit 8a65eb8
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions internal/api/mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ const (
)

func validateFactors(db *storage.Connection, user *models.User, newFactorName string, config *conf.GlobalConfiguration, session *models.Session) error {
if err := models.DeleteExpiredFactors(db, config.MFA.FactorExpiryDuration); err != nil {
return err
}
if err := db.Load(user, "Factors"); err != nil {
return err
}
Expand Down Expand Up @@ -106,7 +109,6 @@ func validateFactors(db *storage.Connection, user *models.User, newFactorName st

func (a *API) enrollPhoneFactor(w http.ResponseWriter, r *http.Request, params *EnrollFactorParams) error {
ctx := r.Context()
config := a.config
user := getUser(ctx)
session := getSession(ctx)
db := a.db.WithContext(ctx)
Expand All @@ -118,9 +120,6 @@ func (a *API) enrollPhoneFactor(w http.ResponseWriter, r *http.Request, params *
if err != nil {
return badRequestError(ErrorCodeValidationFailed, "Invalid phone number format (E.164 required)")
}
if err := models.DeleteExpiredFactors(db, config.MFA.FactorExpiryDuration); err != nil {
return err
}

var factorsToDelete []models.Factor
for _, factor := range user.Factors {
Expand Down Expand Up @@ -185,10 +184,6 @@ func (a *API) enrollTOTPFactor(w http.ResponseWriter, r *http.Request, params *E
issuer = params.Issuer
}

if err := models.DeleteExpiredFactors(db, config.MFA.FactorExpiryDuration); err != nil {
return err
}

if err := validateFactors(db, user, params.FriendlyName, config, session); err != nil {
return err
}
Expand Down

0 comments on commit 8a65eb8

Please sign in to comment.