Skip to content

Commit

Permalink
Merge pull request pquerna#25 from pquerna/pq/base32_dict_difference
Browse files Browse the repository at this point in the history
When parsing a secret, change input to upper case.
  • Loading branch information
pquerna authored Dec 16, 2017
2 parents c70cbf6 + 9af5514 commit 8439c1e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
4 changes: 4 additions & 0 deletions hotp/hotp.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ func GenerateCodeCustom(secret string, counter uint64, opts ValidateOpts) (passc
secret = secret + strings.Repeat("=", 8-n)
}

// As noted in issue #24 Google has started producing base32 in lower case,
// but the StdEncoding (and the RFC), expect a dictionary of only upper case letters.
secret = strings.ToUpper(secret)

secretBytes, err := base32.StdEncoding.DecodeString(secret)
if err != nil {
return "", otp.ErrValidateSecretInvalidBase32
Expand Down
10 changes: 10 additions & 0 deletions hotp/hotp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func TestValidatePadding(t *testing.T) {
require.Equal(t, true, valid, "Valid should be true.")
}

func TestValidateLowerCaseSecret(t *testing.T) {
valid, err := ValidateCustom("831097", 0, "jbswy3dpehpk3px",
ValidateOpts{
Digits: otp.DigitsSix,
Algorithm: otp.AlgorithmSHA1,
})
require.NoError(t, err, "Expected no error.")
require.Equal(t, true, valid, "Valid should be true.")
}

func TestGenerate(t *testing.T) {
k, err := Generate(GenerateOpts{
Issuer: "SnakeOil",
Expand Down
14 changes: 14 additions & 0 deletions totp/totp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,17 @@ func TestGenerate(t *testing.T) {
require.NoError(t, err, "Secret size is valid when length not divisable by 5.")
require.NotContains(t, k.Secret(), "=", "Secret has no escaped characters.")
}

func TestGoogleLowerCaseSecret(t *testing.T) {
w, err := otp.NewKeyFromURL(`otpauth://totp/Google%3Afoo%40example.com?secret=qlt6vmy6svfx4bt4rpmisaiyol6hihca&issuer=Google`)
require.NoError(t, err)
sec := w.Secret()
require.Equal(t, "qlt6vmy6svfx4bt4rpmisaiyol6hihca", sec)

n := time.Now().UTC()
code, err := GenerateCode(w.Secret(), n)
require.NoError(t, err)

valid := Validate(code, w.Secret())
require.True(t, valid)
}

0 comments on commit 8439c1e

Please sign in to comment.