Skip to content

Commit

Permalink
fix: set Skew to 1 as default (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
KEINOS committed May 1, 2024
1 parent 1335a37 commit 017e96d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func Example() {
// - Algorithm: SHA1
// - Period: 30
// - Secret Size: 128
// - Skew (time tolerance): 0
// - Skew (time tolerance): 1
// - Digits: 6
// * Validation result: Passcode is valid
}
Expand Down
20 changes: 10 additions & 10 deletions totp/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func Example() {
// - Algorithm: SHA1
// - Period: 30
// - Secret Size: 128
// - Skew (time tolerance): 0
// - Skew (time tolerance): 1
// - Digits: 6
// * Validation result: Passcode is valid
}
Expand Down Expand Up @@ -193,7 +193,7 @@ Digits: 8
Issuer: Example.com
Period: 30
Secret Size: 64
Skew: 0
Skew: 1
gX7ff3VlT4sCakCjQH69ZQxTbzs=
-----END TOTP SECRET KEY-----`
Expand All @@ -219,7 +219,7 @@ gX7ff3VlT4sCakCjQH69ZQxTbzs=
// Issuer: Example.com
// Period: 30
// Secret Size: 64
// Skew: 0
// Skew: 1
// Secret: QF7N673VMVHYWATKICRUA7V5MUGFG3Z3
}

Expand Down Expand Up @@ -332,7 +332,7 @@ func ExampleKey_regenerate() {
// Issuer: Example.com
// Period: 30
// Secret Size: 20
// Skew: 0
// Skew: 1
//
// gX7ff3VlT4sCakCjQH69ZQxTbzs=
// -----END TOTP SECRET KEY-----
Expand Down Expand Up @@ -360,7 +360,7 @@ func ExampleKey_PassCode() {
}

// Validate the passcode with a custom time
validationTime := time.Now().Add(-30 * time.Second)
validationTime := time.Now().Add(-300 * time.Second)

if key.ValidateCustom(code, validationTime) {
fmt.Println("Passcode is valid with custom time")
Expand All @@ -385,8 +385,8 @@ func ExampleKey_PassCodeCustom() {

timeNow := time.Now()

// Generate a passcode for a specific time (30 seconds ago)
code, err := key.PassCodeCustom(timeNow.Add(-30 * time.Second))
// Generate a passcode for a specific time (300 seconds ago)
code, err := key.PassCodeCustom(timeNow.Add(-300 * time.Second))
if err != nil {
log.Fatal(err)
}
Expand All @@ -400,7 +400,7 @@ func ExampleKey_PassCodeCustom() {

// To validate a passcode for a specific time, use ValidateCustom()
// method.
validationTime := timeNow.Add(-30 * time.Second)
validationTime := timeNow.Add(-300 * time.Second)

if key.ValidateCustom(code, validationTime) {
fmt.Println("Passcode is valid with custom time")
Expand Down Expand Up @@ -437,7 +437,7 @@ func ExampleKey_PEM() {
// Issuer: Example.com
// Period: 30
// Secret Size: 20
// Skew: 0
// Skew: 1
//
// gX7ff3VlT4sCakCjQH69ZQxTbzs=
// -----END TOTP SECRET KEY-----
Expand Down Expand Up @@ -657,7 +657,7 @@ func ExampleOptions() {
// Digits: 6
// Period: 30
// Secret Size: 128
// Skew: 0
// Skew: 1
}

// ============================================================================
Expand Down
4 changes: 3 additions & 1 deletion totp/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,8 @@ func TestKey_PEM(t *testing.T) {
// Issue #42.
// If `Period` is set short with `Skew=0`, the passcode validation often fails.
func TestKey_skew_as_one(t *testing.T) {
t.Parallel()

key, err := GenerateKey("dummy issuer", "dummy account")
require.NoError(t, err, "failed to generate TOTP key during test setup")

Expand All @@ -310,7 +312,7 @@ func TestKey_skew_as_one(t *testing.T) {
numIterations := 10

// If skew is set to 0, the validation fails 60-70% of the time.
for i := 0; i < numIterations; i++ {
for _ = range numIterations {

Check failure on line 315 in totp/key_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

File is not `gofmt`-ed with `-s` (gofmt)
passCode, err := key.PassCode()
require.NoError(t, err, "failed to generate passcode")

Expand Down
10 changes: 5 additions & 5 deletions totp/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const (
OptionDigitsDefault = Digits(6) // Google Authenticator does not work other than 6 digits.
OptionPeriodDefault = uint(30) // 30 seconds is recommended in RFC-6238.
OptionSecretSizeDefault = uint(128) // 128 Bytes.
OptionSkewDefault = uint(0) // ± Periods. No tolerance.
OptionSkewDefault = uint(1) // ± 1 period of tolerance.
)

// ============================================================================
Expand Down Expand Up @@ -49,9 +49,10 @@ type Options struct {
Period uint
// SecretSize is the size of the generated Secret. (Default: 128 bytes)
SecretSize uint
// Skew is the periods before or after the current time to allow.
// Skew is the periods before or after the current time to allow. (Default: 1)
//
// Value of 1 allows up to Period of either side of the specified time.
// Defaults to 0 allowed skews. Values greater than 1 are likely sketchy.
// Values greater than 1 are likely sketchy.
Skew uint
}

Expand Down Expand Up @@ -97,8 +98,7 @@ func (opts *Options) SetDefault() {
opts.Digits = OptionDigitsDefault
}

// This is redundant, but it's here to make sure that the default value is
// always set.
// Fix #42
if opts.Skew == 0 {
opts.Skew = OptionSkewDefault
}
Expand Down

0 comments on commit 017e96d

Please sign in to comment.