forked from TrueCloudLab/lego
inwx: improve sleep calculation (#2086)
This commit is contained in:
parent
143aa4f3ee
commit
7fd1704282
2 changed files with 12 additions and 10 deletions
|
@ -211,19 +211,21 @@ func (d *DNSProvider) twoFactorAuth(info *goinwx.LoginResponse) error {
|
||||||
time.Sleep(sleep)
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
d.previousUnlock = time.Now()
|
d.previousUnlock = now.Truncate(30 * time.Second)
|
||||||
|
|
||||||
return d.client.Account.Unlock(tan)
|
return d.client.Account.Unlock(tan)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
|
func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
|
||||||
if d.previousUnlock.IsZero() {
|
if d.previousUnlock.IsZero() {
|
||||||
return 0 * time.Second
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
endPeriod := d.previousUnlock.Add(30 * time.Second)
|
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 endPeriod.Sub(now)
|
||||||
}
|
}
|
||||||
|
|
||||||
return 0 * time.Second
|
return 0
|
||||||
}
|
}
|
||||||
|
|
|
@ -147,27 +147,27 @@ func TestLivePresentAndCleanup(t *testing.T) {
|
||||||
func Test_computeSleep(t *testing.T) {
|
func Test_computeSleep(t *testing.T) {
|
||||||
testCases := []struct {
|
testCases := []struct {
|
||||||
desc string
|
desc string
|
||||||
previous string
|
now string
|
||||||
expected time.Duration
|
expected time.Duration
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
desc: "after 30s",
|
desc: "after 30s",
|
||||||
previous: "2024-01-01T06:29:20Z",
|
now: "2024-01-01T06:30:30Z",
|
||||||
expected: 0 * time.Second,
|
expected: 0 * time.Second,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "0s",
|
desc: "0s",
|
||||||
previous: "2024-01-01T06:29:30Z",
|
now: "2024-01-01T06:30:00Z",
|
||||||
expected: 0 * time.Second,
|
expected: 0 * time.Second,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
desc: "before 30s",
|
desc: "before 30s",
|
||||||
previous: "2024-01-01T06:29:50Z", // 10 s
|
now: "2024-01-01T06:29:40Z", // 10 s
|
||||||
expected: 20 * time.Second,
|
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)
|
require.NoError(t, err)
|
||||||
|
|
||||||
for _, test := range testCases {
|
for _, test := range testCases {
|
||||||
|
@ -175,7 +175,7 @@ func Test_computeSleep(t *testing.T) {
|
||||||
t.Run(test.desc, func(t *testing.T) {
|
t.Run(test.desc, func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
||||||
previous, err := time.Parse(time.RFC3339, test.previous)
|
now, err := time.Parse(time.RFC3339, test.now)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
d := &DNSProvider{previousUnlock: previous}
|
d := &DNSProvider{previousUnlock: previous}
|
||||||
|
|
Loading…
Reference in a new issue