1
0
mirror of https://github.com/volatiletech/authboss.git synced 2025-01-24 05:17:10 +02:00

Add maximum amount of OTPs

This commit is contained in:
Aaron L 2018-07-17 15:32:10 -07:00
parent 6164dd8da4
commit 9aed0c512d
2 changed files with 49 additions and 2 deletions

View File

@ -20,6 +20,7 @@ import (
const (
otpSize = 16
maxOTPs = 5
// PageLogin is for identifying the login page for parsing & validation
PageLogin = "otplogin"
@ -196,14 +197,20 @@ func (o *OTP) AddPost(w http.ResponseWriter, r *http.Request) error {
return err
}
otpUser := MustBeOTPable(user)
currentOTPs := splitOTPs(otpUser.GetOTPs())
if len(currentOTPs) >= maxOTPs {
data := authboss.HTMLData{authboss.DataValidation: fmt.Sprintf("you cannot have more than %d one time passwords", maxOTPs)}
return o.Core.Responder.Respond(w, r, http.StatusOK, PageAdd, data)
}
logger.Infof("generating otp for %s", user.GetPID())
otp, hash, err := generateOTP()
if err != nil {
return err
}
otpUser := MustBeOTPable(user)
currentOTPs := splitOTPs(otpUser.GetOTPs())
currentOTPs = append(currentOTPs, hash)
otpUser.PutOTPs(joinOTPs(currentOTPs))

View File

@ -459,6 +459,46 @@ func TestAddPost(t *testing.T) {
}
}
func TestAddPostTooMany(t *testing.T) {
t.Parallel()
h := testSetup()
uname := "test@test.com"
h.storer.Users[uname] = &mocks.User{
Email: uname,
OTPs: "2aID,2aID,2aID,2aID,2aID",
}
h.session.ClientValues[authboss.SessionKey] = uname
r := mocks.Request("POST")
w := h.ab.NewResponse(httptest.NewRecorder())
var err error
r, err = h.ab.LoadClientState(w, r)
if err != nil {
t.Fatal(err)
}
if err := h.otp.AddPost(w, r); err != nil {
t.Fatal(err)
}
if h.responder.Page != PageAdd {
t.Error("wanted add page, got:", h.responder.Page)
}
if h.responder.Status != http.StatusOK {
t.Error("wanted ok status, got:", h.responder.Status)
}
if len(h.responder.Data[authboss.DataValidation].(string)) == 0 {
t.Error("there should have been a validation error")
}
otps := splitOTPs(h.storer.Users[uname].OTPs)
if len(otps) != maxOTPs {
t.Error("expected the number of OTPs to be equal to the maximum")
}
}
func TestAddGetUserNotFound(t *testing.T) {
t.Parallel()