inwx: improve sleep calculation (#2086)

This commit is contained in:
Ludovic Fernandez 2024-01-19 13:55:57 +01:00 committed by GitHub
parent 143aa4f3ee
commit 7fd1704282
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 12 additions and 10 deletions

View file

@ -211,19 +211,21 @@ func (d *DNSProvider) twoFactorAuth(info *goinwx.LoginResponse) error {
time.Sleep(sleep)
}
tan, err := totp.GenerateCode(d.config.SharedSecret, time.Now())
now := time.Now()
tan, err := totp.GenerateCode(d.config.SharedSecret, now)
if err != nil {
return err
}
d.previousUnlock = time.Now()
d.previousUnlock = now.Truncate(30 * time.Second)
return d.client.Account.Unlock(tan)
}
func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
if d.previousUnlock.IsZero() {
return 0 * time.Second
return 0
}
endPeriod := d.previousUnlock.Add(30 * time.Second)
@ -231,5 +233,5 @@ func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
return endPeriod.Sub(now)
}
return 0 * time.Second
return 0
}

View file

@ -147,27 +147,27 @@ func TestLivePresentAndCleanup(t *testing.T) {
func Test_computeSleep(t *testing.T) {
testCases := []struct {
desc string
previous string
now string
expected time.Duration
}{
{
desc: "after 30s",
previous: "2024-01-01T06:29:20Z",
now: "2024-01-01T06:30:30Z",
expected: 0 * time.Second,
},
{
desc: "0s",
previous: "2024-01-01T06:29:30Z",
now: "2024-01-01T06:30:00Z",
expected: 0 * time.Second,
},
{
desc: "before 30s",
previous: "2024-01-01T06:29:50Z", // 10 s
now: "2024-01-01T06:29:40Z", // 10 s
expected: 20 * time.Second,
},
}
now, err := time.Parse(time.RFC3339, "2024-01-01T06:30:00Z")
previous, err := time.Parse(time.RFC3339, "2024-01-01T06:29:30Z")
require.NoError(t, err)
for _, test := range testCases {
@ -175,7 +175,7 @@ func Test_computeSleep(t *testing.T) {
t.Run(test.desc, func(t *testing.T) {
t.Parallel()
previous, err := time.Parse(time.RFC3339, test.previous)
now, err := time.Parse(time.RFC3339, test.now)
require.NoError(t, err)
d := &DNSProvider{previousUnlock: previous}