Skip to content

Commit c39aab7

Browse files
authored
Merge pull request #18 from pquerna/pq/trim_secret_whitespace
Be more careful with secret input. Fixes #17
2 parents bf99dce + ac978bf commit c39aab7

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

Diff for: hotp/hotp.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,9 @@ func GenerateCode(secret string, counter uint64) (string, error) {
7070
// GenerateCodeCustom uses a counter and secret value and options struct to
7171
// create a passcode.
7272
func GenerateCodeCustom(secret string, counter uint64, opts ValidateOpts) (passcode string, err error) {
73-
// As noted in issue #10 this adds support for TOTP secrets that are
73+
// As noted in issue #10 and #17 this adds support for TOTP secrets that are
7474
// missing their padding.
75+
secret = strings.TrimSpace(secret)
7576
if n := len(secret) % 8; n != 0 {
7677
secret = secret + strings.Repeat("=", 8-n)
7778
}

Diff for: otp.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,16 @@ type Key struct {
5757
// https://github.com/google/google-authenticator/wiki/Key-Uri-Format
5858
//
5959
func NewKeyFromURL(orig string) (*Key, error) {
60-
u, err := url.Parse(orig)
60+
s := strings.TrimSpace(orig)
61+
62+
u, err := url.Parse(s)
6163

6264
if err != nil {
6365
return nil, err
6466
}
6567

6668
return &Key{
67-
orig: orig,
69+
orig: s,
6870
url: u,
6971
}, nil
7072
}

Diff for: otp_test.go

+8
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,11 @@ func TestKeyNoIssuer(t *testing.T) {
4545
require.Equal(t, "", k.Issuer(), "Extracting Issuer")
4646
require.Equal(t, "[email protected]", k.AccountName(), "Extracting Account Name")
4747
}
48+
49+
func TestKeyWithNewLine(t *testing.T) {
50+
w, err := NewKeyFromURL(`otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP
51+
`)
52+
require.NoError(t, err)
53+
sec := w.Secret()
54+
require.Equal(t, "JBSWY3DPEHPK3PXP", sec)
55+
}

0 commit comments

Comments
 (0)