-
Notifications
You must be signed in to change notification settings - Fork 224
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow calls to {htop,totp}.Generate() to specify the secret, rather t…
…han using a randomly generated one. Fixes #37
- Loading branch information
Showing
4 changed files
with
42 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,6 +146,8 @@ type GenerateOpts struct { | |
AccountName string | ||
// Size in size of the generated Secret. Defaults to 10 bytes. | ||
SecretSize uint | ||
// Secret to store. Defaults to a randomly generated secret of SecretSize. You should generally leave this empty. | ||
Secret []byte | ||
// Digits to request. Defaults to 6. | ||
Digits otp.Digits | ||
// Algorithm to use for HMAC. Defaults to SHA1. | ||
|
@@ -176,13 +178,17 @@ func Generate(opts GenerateOpts) (*otp.Key, error) { | |
// otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example | ||
|
||
v := url.Values{} | ||
secret := make([]byte, opts.SecretSize) | ||
_, err := rand.Read(secret) | ||
if err != nil { | ||
return nil, err | ||
if len(opts.Secret) != 0 { | ||
v.Set("secret", b32NoPadding.EncodeToString(opts.Secret)) | ||
} else { | ||
secret := make([]byte, opts.SecretSize) | ||
_, err := rand.Read(secret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v.Set("secret", b32NoPadding.EncodeToString(secret)) | ||
} | ||
|
||
v.Set("secret", b32NoPadding.EncodeToString(secret)) | ||
v.Set("issuer", opts.Issuer) | ||
v.Set("algorithm", opts.Algorithm.String()) | ||
v.Set("digits", opts.Digits.String()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -169,4 +169,14 @@ 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.") | ||
|
||
k, err = Generate(GenerateOpts{ | ||
Issuer: "SnakeOil", | ||
AccountName: "[email protected]", | ||
Secret: []byte("helloworld"), | ||
}) | ||
require.NoError(t, err, "Secret generation failed") | ||
sec, err := b32NoPadding.DecodeString(k.Secret()) | ||
require.NoError(t, err, "Secret wa not valid base32") | ||
require.Equal(t, sec, []byte("helloworld"), "Specified Secret was not kept") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -136,6 +136,8 @@ type GenerateOpts struct { | |
Period uint | ||
// Size in size of the generated Secret. Defaults to 20 bytes. | ||
SecretSize uint | ||
// Secret to store. Defaults to a randomly generated secret of SecretSize. You should generally leave this empty. | ||
Secret []byte | ||
// Digits to request. Defaults to 6. | ||
Digits otp.Digits | ||
// Algorithm to use for HMAC. Defaults to SHA1. | ||
|
@@ -170,13 +172,17 @@ func Generate(opts GenerateOpts) (*otp.Key, error) { | |
// otpauth://totp/Example:[email protected]?secret=JBSWY3DPEHPK3PXP&issuer=Example | ||
|
||
v := url.Values{} | ||
secret := make([]byte, opts.SecretSize) | ||
_, err := rand.Read(secret) | ||
if err != nil { | ||
return nil, err | ||
if len(opts.Secret) != 0 { | ||
v.Set("secret", b32NoPadding.EncodeToString(opts.Secret)) | ||
} else { | ||
secret := make([]byte, opts.SecretSize) | ||
_, err := rand.Read(secret) | ||
if err != nil { | ||
return nil, err | ||
} | ||
v.Set("secret", b32NoPadding.EncodeToString(secret)) | ||
} | ||
|
||
v.Set("secret", b32NoPadding.EncodeToString(secret)) | ||
v.Set("issuer", opts.Issuer) | ||
v.Set("period", strconv.FormatUint(uint64(opts.Period), 10)) | ||
v.Set("algorithm", opts.Algorithm.String()) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -143,6 +143,16 @@ 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.") | ||
|
||
k, err = Generate(GenerateOpts{ | ||
Issuer: "SnakeOil", | ||
AccountName: "[email protected]", | ||
Secret: []byte("helloworld"), | ||
}) | ||
require.NoError(t, err, "Secret generation failed") | ||
sec, err := b32NoPadding.DecodeString(k.Secret()) | ||
require.NoError(t, err, "Secret wa not valid base32") | ||
require.Equal(t, sec, []byte("helloworld"), "Specified Secret was not kept") | ||
} | ||
|
||
func TestGoogleLowerCaseSecret(t *testing.T) { | ||
|