Skip to content

Commit dbfe07e

Browse files
authored
Merge pull request #354 from justin-wilxite/totp-fix
Handle invalid recovery code in totp2fa #351
2 parents d38273a + ee0d69a commit dbfe07e

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

‎otp/twofactor/sms2fa/sms_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,33 @@ func TestValidatorPostOk(t *testing.T) {
525525
}
526526
})
527527

528+
t.Run("InvalidRecovery", func(t *testing.T) {
529+
h := testSetup()
530+
r, w, _ := h.newHTTP("POST")
531+
v := &SMSValidator{SMS: h.sms, Page: PageSMSValidate}
532+
533+
user := &mocks.User{Email: "test@test.com", SMSPhoneNumber: "number"}
534+
h.storer.Users[user.Email] = user
535+
h.setSession(authboss.SessionKey, user.Email)
536+
537+
h.setSession(SessionSMSSecret, "code-user-never-got")
538+
h.bodyReader.Return = mocks.Values{Recovery: "INVALID"}
539+
540+
h.loadClientState(w, &r)
541+
542+
if err := v.Post(w, r); err != nil {
543+
t.Fatal(err)
544+
}
545+
546+
// Flush client state
547+
w.WriteHeader(http.StatusOK)
548+
549+
validation := h.responder.Data[authboss.DataValidation].(map[string][]string)
550+
if got := validation[FormValueCode][0]; got != "2fa code was invalid" {
551+
t.Error("data wrong:", got)
552+
}
553+
})
554+
528555
t.Run("FailRemoveCode", func(t *testing.T) {
529556
h := testSetup()
530557
r, w, _ := h.newHTTP("POST")

‎otp/twofactor/totp2fa/totp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,8 @@ func (t *TOTP) validate(r *http.Request) (User, string, error) {
477477
if err := t.Authboss.Config.Storage.Server.Save(r.Context(), user); err != nil {
478478
return nil, "", err
479479
}
480+
} else {
481+
return user, validationErrInvalidCode, nil
480482
}
481483

482484
return user, validationSuccess, nil

‎otp/twofactor/totp2fa/totp_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,34 @@ func TestPostValidate(t *testing.T) {
597597
t.Error("path wrong:", opts.RedirectPath)
598598
}
599599
})
600+
601+
t.Run("InvalidRecovery", func(t *testing.T) {
602+
h := testSetup()
603+
604+
r, w, _ := h.newHTTP("POST")
605+
user := setupMore(h)
606+
secret := makeSecretKey(h, user.Email)
607+
user.TOTPSecretKey = secret
608+
609+
// User inputs invalid recovery code
610+
h.bodyReader.Return = mocks.Values{Recovery: "INVALID"}
611+
612+
h.setSession(SessionTOTPPendingPID, user.Email)
613+
h.setSession(SessionTOTPSecret, "secret")
614+
h.setSession(authboss.SessionHalfAuthKey, "true")
615+
h.loadClientState(w, &r)
616+
617+
if err := h.totp.PostValidate(w, r); err != nil {
618+
t.Error(err)
619+
}
620+
621+
// Flush client state
622+
w.WriteHeader(http.StatusOK)
623+
624+
if got := h.responder.Data[authboss.DataValidation].(map[string][]string); got[FormValueCode][0] != "2fa code was invalid" {
625+
t.Error("data wrong:", got)
626+
}
627+
})
600628
}
601629

602630
func makeSecretKey(h *testHarness, email string) string {

0 commit comments

Comments
 (0)
close