Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

inwx: wait before generating new TOTP TANs #2084

Merged
merged 3 commits into from
Jan 18, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
32 changes: 29 additions & 3 deletions providers/dns/inwx/inwx.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ func NewDefaultConfig() *Config {

// DNSProvider implements the challenge.Provider interface.
type DNSProvider struct {
config *Config
client *goinwx.Client
config *Config
client *goinwx.Client
previousCall time.Time
}

// NewDNSProvider returns a DNSProvider instance configured for Dyn DNS.
Expand Down Expand Up @@ -202,10 +203,35 @@ func (d *DNSProvider) twoFactorAuth(info *goinwx.LoginResponse) error {
return errors.New("two-factor authentication but no shared secret is given")
}

tan, err := totp.GenerateCode(d.config.SharedSecret, time.Now())
// INWX forbids re-authentication with a previously used TAN.
// To avoid using the same TAN twice, we wait until the next TOTP period and retry.
sleep := d.computeSleep(time.Now())
if sleep != 0 {
log.Infof("inwx: waiting %s for next TOTP token", sleep)
time.Sleep(sleep)
}

now := time.Now()

tan, err := totp.GenerateCode(d.config.SharedSecret, now)
if err != nil {
return err
}

d.previousCall = now

return d.client.Account.Unlock(tan)
}

func (d *DNSProvider) computeSleep(now time.Time) time.Duration {
if d.previousCall.IsZero() {
return 0 * time.Second
ldez marked this conversation as resolved.
Show resolved Hide resolved
}

endPeriod := d.previousCall.Add(30 * time.Second)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this works, I suspect we are wasting a few seconds here on average, because d.previousCall is never rooted to be a multiple of 30 seconds after the epoch (Unix time 0)?

It works though and how relevant this is depends a bit on the distance between ajacent TOTP attempts.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, sorry I completely missed that.
Sorry, I will try to fix that.
I will truncate the previous time by 30 seconds.

if endPeriod.After(now) {
return endPeriod.Sub(now)
}

return 0 * time.Second
ldez marked this conversation as resolved.
Show resolved Hide resolved
}
44 changes: 44 additions & 0 deletions providers/dns/inwx/inwx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package inwx

import (
"testing"
"time"

"github.com/go-acme/lego/v4/platform/tester"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

Expand Down Expand Up @@ -141,3 +143,45 @@ func TestLivePresentAndCleanup(t *testing.T) {
err = provider.CleanUp(envTest.GetDomain(), "", "123d==")
require.NoError(t, err)
}

func Test_computeSleep(t *testing.T) {
testCases := []struct {
desc string
time string
expected time.Duration
}{
{
desc: "after 30s",
time: "2024-01-01T06:29:20Z",
expected: 0 * time.Second,
},
{
desc: "0s",
time: "2024-01-01T06:29:30Z",
expected: 0 * time.Second,
},
{
desc: "before 30s",
time: "2024-01-01T06:29:50Z", // 10 s
expected: 20 * time.Second,
},
}

now, err := time.Parse(time.RFC3339, "2024-01-01T06:30:00Z")
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.time)
require.NoError(t, err)

d := &DNSProvider{previousCall: previous}

sleep := d.computeSleep(now)
assert.Equal(t, test.expected, sleep)
})
}
}