Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions vault/external_tests/identity/login_mfa_totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"encoding/base64"
"fmt"
"strconv"
"strings"
"testing"
"time"
Expand Down Expand Up @@ -92,7 +93,7 @@ func TestLoginMfaGenerateTOTPTestAuditIncluded(t *testing.T) {
// Creating two users in the userpass auth mount
userClient1, entityID1, _ := testhelpers.CreateEntityAndAlias(t, client, mountAccessor, entity1, testuser1)
userClient2, entityID2, _ := testhelpers.CreateEntityAndAlias(t, client, mountAccessor, entity2, testuser2)
waitPeriod := 5
waitPeriod := 3
totpConfig := map[string]interface{}{
"issuer": "yCorp",
"period": waitPeriod,
Expand Down Expand Up @@ -281,10 +282,24 @@ func TestLoginMfaGenerateTOTPTestAuditIncluded(t *testing.T) {
if err == nil {
t.Fatalf("MFA succeeded with an already used passcode")
}
if !strings.Contains(err.Error(), "code already used") {
t.Fatalf("got: %+v, expected: code already used", err.Error())
if !strings.Contains(err.Error(), "failed to validate TOTP passcode") {
t.Fatalf("got: %+v, expected: failed to validate TOTP passcode", err.Error())
}

for _, possibleCode := range []string{totpPasscode1 + " ", " " + totpPasscode1} {
_, err = userClient1.Logical().WriteWithContext(context.Background(), "sys/mfa/validate", map[string]interface{}{
"mfa_request_id": secret.Auth.MFARequirement.MFARequestID,
"mfa_payload": map[string][]string{
methodID: {possibleCode},
},
})
if err == nil {
t.Fatalf("MFA succeeded with an already used passcode")
}
if !strings.Contains(err.Error(), "failed to validate TOTP passcode") {
t.Fatalf("got: %+v, expected: failed to validate TOTP passcode", err.Error())
}
}
// check for reaching max failed validation requests
secret, err = userClient1.Logical().WriteWithContext(context.Background(), userpassPath, map[string]interface{}{
"password": "testpassword",
Expand All @@ -300,7 +315,7 @@ func TestLoginMfaGenerateTOTPTestAuditIncluded(t *testing.T) {
_, maxErr = userClient1.Logical().WriteWithContext(context.Background(), "sys/mfa/validate", map[string]interface{}{
"mfa_request_id": secret.Auth.MFARequirement.MFARequestID,
"mfa_payload": map[string][]string{
methodID: {fmt.Sprintf("%d", i)},
methodID: {fmt.Sprintf("%s", strings.Repeat(strconv.Itoa(i), len(totpPasscode1)))},
},
})
if maxErr == nil {
Expand All @@ -314,8 +329,8 @@ func TestLoginMfaGenerateTOTPTestAuditIncluded(t *testing.T) {
// let's make sure the configID is not blocked for other users
doTwoPhaseLogin(t, userClient2, enginePath2, methodID, testuser2)

// let's see if user1 is able to login after 5 seconds
time.Sleep(5 * time.Second)
// let's see if user1 is able to login after 3 + 3*2 = 9 seconds
time.Sleep(9 * time.Second)
doTwoPhaseLogin(t, userClient1, enginePath1, methodID, testuser1)

// Destroy the secret so that the token can self generate
Expand Down
9 changes: 6 additions & 3 deletions vault/login_mfa.go
Original file line number Diff line number Diff line change
Expand Up @@ -2332,17 +2332,20 @@ func (c *Core) validateTOTP(ctx context.Context, mfaFactors *MFAFactor, entityMe
return fmt.Errorf("entity does not contain the TOTP secret")
}

if len(passcode) != int(totpSecret.GetDigits()) {
return fmt.Errorf("failed to validate TOTP passcode")
}

usedName := fmt.Sprintf("%s_%s", configID, passcode)

_, ok := usedCodes.Get(usedName)
if ok {
return fmt.Errorf("code already used; new code is available in %v seconds", totpSecret.Period)
return fmt.Errorf("failed to validate TOTP passcode")
}

// The duration in which a passcode is stored in cache to enforce
// rate limit on failed totp passcode validation
passcodeTTL := time.Duration(int64(time.Second) * int64(totpSecret.Period))

passcodeTTL := time.Duration(int64(time.Second) * int64(totpSecret.Period) * int64(2*totpSecret.Skew))
// Enforcing rate limit per MethodID per EntityID
rateLimitID := fmt.Sprintf("%s_%s", configID, entityID)

Expand Down
Loading