Skip to content

Commit

Permalink
inwx: improve sleep calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
ldez committed Jan 18, 2024
1 parent 143aa4f commit 9f1e3b4
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
10 changes: 6 additions & 4 deletions providers/dns/inwx/inwx.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,25 +211,27 @@ 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)
if endPeriod.After(now) {
return endPeriod.Sub(now)
}

return 0 * time.Second
return 0
}
12 changes: 6 additions & 6 deletions providers/dns/inwx/inwx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,35 +147,35 @@ 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 {
test := test
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}
Expand Down

0 comments on commit 9f1e3b4

Please sign in to comment.