diff --git a/token/otp/buffer.go b/token/code/buffer.go similarity index 67% rename from token/otp/buffer.go rename to token/code/buffer.go index 9a8594c..2c08fc3 100644 --- a/token/otp/buffer.go +++ b/token/code/buffer.go @@ -1,4 +1,4 @@ -package otp +package code import ( "math/rand" @@ -6,11 +6,6 @@ import ( ) func bufferFromToken(token string) func(step int) ([]byte, error) { - // // Does not support any more than a 15-characters - // buf := []byte(token) - // seed := int64(binary.LittleEndian.Uint64(buf)) - - // Works for longer tokens seed, _ := strconv.ParseInt(token, 10, 64) r := rand.New(rand.NewSource(seed)) diff --git a/token/code/config.go b/token/code/config.go new file mode 100644 index 0000000..91007e1 --- /dev/null +++ b/token/code/config.go @@ -0,0 +1,26 @@ +package code + +import ( + "github.com/Mobilpadde/moths/v4/token/emojies" +) + +type Code struct { + token string + emojies emojies.Emojies +} + +func (code Code) Token() string { + return code.token +} + +func (code Code) String() string { + return code.emojies.String() +} + +func (code Code) SpacedString() string { + return code.emojies.SpacedString() +} + +func (code Code) Slice() []string { + return code.emojies.Slice() +} diff --git a/token/otp/new.go b/token/code/new.go similarity index 86% rename from token/otp/new.go rename to token/code/new.go index 93c7bb8..edc36f3 100644 --- a/token/otp/new.go +++ b/token/code/new.go @@ -1,4 +1,4 @@ -package otp +package code import ( "math" @@ -10,7 +10,7 @@ import ( const EmojiBytes = 4 -func NewOTP(token string, amount int, em emojies.Emojies) (OTP, error) { +func NewCode(token string, amount int, em emojies.Emojies) (Code, error) { emojiAmount := len(em) size := EmojiBytes * amount @@ -26,7 +26,7 @@ loop: for { randomBuffer, err := buffer(step) if err != nil { - return OTP{}, err + return Code{}, err } for i := 0; i < step; i++ { @@ -34,7 +34,7 @@ loop: if currentIndex < emojiAmount { if _, err := code.WriteRune(em[currentIndex]); err != nil { - return OTP{}, err + return Code{}, err } else if code.Len() == size { break loop } @@ -47,7 +47,7 @@ loop: } split := strings.Split(code.String(), "") - return OTP{ + return Code{ token: token, emojies: emojies.ToEmojies(split), }, nil diff --git a/token/otp/new_test.go b/token/code/new_test.go similarity index 67% rename from token/otp/new_test.go rename to token/code/new_test.go index 6ab72c9..6070f9b 100644 --- a/token/otp/new_test.go +++ b/token/code/new_test.go @@ -1,4 +1,4 @@ -package otp +package code import ( "testing" @@ -6,14 +6,14 @@ import ( "github.com/Mobilpadde/moths/v4/token/emojies" ) -func TestNewOTP(t *testing.T) { +func TestNewCode(t *testing.T) { amount := 6 token := "000000" correct := "😸😼😹😺😸😻" - otp, err := NewOTP(token, amount, emojies.CATS) + otp, err := NewCode(token, amount, emojies.CATS) if err != nil { - t.Error("Expected to not return an error when creating new OTP:", err) + t.Error("Expected to not return an error when creating new code:", err) } if otp.token != token { diff --git a/token/code/validate.go b/token/code/validate.go new file mode 100644 index 0000000..f14ec75 --- /dev/null +++ b/token/code/validate.go @@ -0,0 +1,9 @@ +package code + +func (code Code) Validate(emojies string) bool { + return code.String() == emojies +} + +func (code Code) ValidateToken(token string) bool { + return code.Token() == token +} diff --git a/token/code/validate_test.go b/token/code/validate_test.go new file mode 100644 index 0000000..99692c0 --- /dev/null +++ b/token/code/validate_test.go @@ -0,0 +1,36 @@ +package code + +import ( + "testing" + + "github.com/Mobilpadde/moths/v4/token/emojies" +) + +func TestValidate(t *testing.T) { + amount := 6 + token := "000000" + correct := "😸😼😹😺😸😻" + + code, err := NewCode(token, amount, emojies.CATS) + if err != nil { + t.Error("Expected to not return an error when creating new code:", err) + } + + if !code.Validate(correct) { + t.Error("Expected code to be", correct, "not", code.emojies) + } +} + +func TestValidateToken(t *testing.T) { + amount := 6 + token := "000000" + + code, err := NewCode(token, amount, emojies.CATS) + if err != nil { + t.Error("Expected to not return an error when creating new code:", err) + } + + if !code.ValidateToken(token) { + t.Error("Expected code to be", token, "not", code.token) + } +} diff --git a/token/next.go b/token/next.go index 9380c63..b25120a 100644 --- a/token/next.go +++ b/token/next.go @@ -8,16 +8,16 @@ import ( "math" "time" - "github.com/Mobilpadde/moths/v4/token/otp" + "github.com/Mobilpadde/moths/v4/token/code" ) -func (m *Generator) Next() (otp.OTP, error) { +func (m *Generator) Next() (code.Code, error) { token, err := m.getToken() if err != nil { - return otp.OTP{}, err + return code.Code{}, err } - return otp.NewOTP(token, m.amount, m.emojies) + return code.NewCode(token, m.amount, m.emojies) } func (m *Generator) getToken() (string, error) { @@ -33,7 +33,7 @@ func (m *Generator) getToken() (string, error) { interval := uint64(m.timing.time.Unix() / int64(m.interval.Seconds())) - // https://github.com/pquerna/otp/blob/master/hotp/hotp.go#L95-L123 + // https://github.com/pquerna/code/blob/master/hotp/hotp.go#L95-L123 buf := make([]byte, 8) h := hmac.New(sha1.New, m.secret) binary.BigEndian.PutUint64(buf, interval) diff --git a/token/otp/config.go b/token/otp/config.go deleted file mode 100644 index 9811c3c..0000000 --- a/token/otp/config.go +++ /dev/null @@ -1,26 +0,0 @@ -package otp - -import ( - "github.com/Mobilpadde/moths/v4/token/emojies" -) - -type OTP struct { - token string - emojies emojies.Emojies -} - -func (otp OTP) Token() string { - return otp.token -} - -func (otp OTP) String() string { - return otp.emojies.String() -} - -func (otp OTP) SpacedString() string { - return otp.emojies.SpacedString() -} - -func (otp OTP) Slice() []string { - return otp.emojies.Slice() -} diff --git a/token/otp/validate.go b/token/otp/validate.go deleted file mode 100644 index c1742ab..0000000 --- a/token/otp/validate.go +++ /dev/null @@ -1,9 +0,0 @@ -package otp - -func (otp OTP) Validate(moth string) bool { - return otp.String() == moth -} - -func (otp OTP) ValidateToken(token string) bool { - return otp.Token() == token -} diff --git a/token/otp/validate_test.go b/token/otp/validate_test.go deleted file mode 100644 index 726146f..0000000 --- a/token/otp/validate_test.go +++ /dev/null @@ -1,36 +0,0 @@ -package otp - -import ( - "testing" - - "github.com/Mobilpadde/moths/v4/token/emojies" -) - -func TestValidate(t *testing.T) { - amount := 6 - token := "000000" - correct := "😸😼😹😺😸😻" - - otp, err := NewOTP(token, amount, emojies.CATS) - if err != nil { - t.Error("Expected to not return an error when creating new OTP:", err) - } - - if !otp.Validate(correct) { - t.Error("Expected code to be", correct, "not", otp.emojies) - } -} - -func TestValidateToken(t *testing.T) { - amount := 6 - token := "000000" - - otp, err := NewOTP(token, amount, emojies.CATS) - if err != nil { - t.Error("Expected to not return an error when creating new OTP:", err) - } - - if !otp.ValidateToken(token) { - t.Error("Expected code to be", token, "not", otp.token) - } -} diff --git a/token/validate.go b/token/validate.go index cbc052f..863a634 100644 --- a/token/validate.go +++ b/token/validate.go @@ -1,11 +1,11 @@ package token import ( - "github.com/Mobilpadde/moths/v4/token/otp" + "github.com/Mobilpadde/moths/v4/token/code" ) func (m *Generator) Validate(moth string) bool { - if len(moth)/otp.EmojiBytes != m.amount { + if len(moth)/code.EmojiBytes != m.amount { return false } @@ -14,7 +14,7 @@ func (m *Generator) Validate(moth string) bool { return false } - same, err := otp.NewOTP(token, m.amount, m.emojies) + same, err := code.NewCode(token, m.amount, m.emojies) if err != nil { return false } @@ -25,13 +25,15 @@ func (m *Generator) Validate(moth string) bool { // This should maybe not be used // as you should not really expose the `token` // to your users +// +// Deprecated: Insecure. Use `Validate` instead. func (m *Generator) ValidateToken(oldToken string) bool { token, err := m.getToken() if err != nil { return false } - same, err := otp.NewOTP(token, m.amount, m.emojies) + same, err := code.NewCode(token, m.amount, m.emojies) if err != nil { return false }