Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: add account name to MFA secret generator (#4996)

boojack966087fe2cde
diff --git a/backend/plugin/mfa/otp/otp.go b/backend/plugin/mfa/otp/otp.go
index dabaf79c79..4198e3d1d6 100644
--- a/backend/plugin/mfa/otp/otp.go
+++ b/backend/plugin/mfa/otp/otp.go
@@ -2,7 +2,7 @@
package otp
import (
- "strconv"
+ "fmt"
"strings"
"time"
@@ -21,13 +21,13 @@ type TimeBasedReader struct {
reader *strings.Reader
}
-// NewTimeBasedReader creates a new TimeBasedReader for the given timestamp.
-func NewTimeBasedReader(timestamp time.Time) *TimeBasedReader {
+// NewTimeBasedReader creates a new TimeBasedReader with the given account name and timestamp.
+func NewTimeBasedReader(accountName string, timestamp time.Time) *TimeBasedReader {
// Convert the timestamp to Unix time and divide by the secretMaxExpiredDuration.
// We generate a new secret every 5 minutes. e.g. 15:00, 15:05
formatedTimestampUnix := timestamp.Unix() / int64(generateSecretPeriod/time.Second)
return &TimeBasedReader{
- reader: strings.NewReader(strconv.FormatInt(formatedTimestampUnix, 10)),
+ reader: strings.NewReader(fmt.Sprintf("%s-%d", accountName, formatedTimestampUnix)),
}
}
@@ -40,7 +40,7 @@ func GenerateSecret(accountName string, timestamp time.Time) (string, error) {
key, err := totp.Generate(totp.GenerateOpts{
Issuer: issuerName,
AccountName: accountName,
- Rand: NewTimeBasedReader(timestamp),
+ Rand: NewTimeBasedReader(accountName, timestamp),
})
if err != nil {
return "", err
@@ -58,7 +58,7 @@ func GetValidSecrets(accountName string, timestamp time.Time) ([]string, error)
key, err := totp.Generate(totp.GenerateOpts{
Issuer: issuerName,
AccountName: accountName,
- Rand: NewTimeBasedReader(t),
+ Rand: NewTimeBasedReader(accountName, t),
})
if err != nil {
return nil, err
diff --git a/backend/plugin/mfa/otp/otp_test.go b/backend/plugin/mfa/otp/otp_test.go
index 1318fa68ea..1de62c47bc 100644
--- a/backend/plugin/mfa/otp/otp_test.go
+++ b/backend/plugin/mfa/otp/otp_test.go
@@ -8,7 +8,7 @@ import (
"golang.org/x/exp/slices"
)
-func TestGenerateTimeBasedSecret(t *testing.T) {
+func TestValidateTimeBasedSecretDuration(t *testing.T) {
currentTime := time.Now()
accountName := "test-user"
@@ -78,3 +78,30 @@ func TestGenerateTimeBasedSecret(t *testing.T) {
})
}
}
+
+func TestGenerateTimeBasedSecret(t *testing.T) {
+ tests := []struct {
+ accountName string
+ timestamp time.Time
+ wantedSecret string
+ }{
+ {
+ accountName: "frank",
+ timestamp: time.Unix(1678115520, 0),
+ wantedSecret: "MZZGC3TLFU2TKOJTG4YTQAAAAAAAAAAA",
+ },
+ {
+ accountName: "jack",
+ timestamp: time.Unix(1678115520, 0),
+ wantedSecret: "NJQWG2ZNGU2TSMZXGE4AAAAAAAAAAAAA",
+ },
+ }
+
+ for _, test := range tests {
+ t.Run(test.accountName, func(t *testing.T) {
+ secret, err := GenerateSecret(test.accountName, test.timestamp)
+ assert.NoError(t, err)
+ assert.Equal(t, test.wantedSecret, secret)
+ })
+ }
+}