Skip to content

Commit

Permalink
Rename otp-package to code
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobilpadde committed Mar 7, 2023
1 parent a3cf5a4 commit fcdf295
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 95 deletions.
7 changes: 1 addition & 6 deletions token/otp/buffer.go → token/code/buffer.go
@@ -1,16 +1,11 @@
package otp
package code

import (
"math/rand"
"strconv"
)

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))

Expand Down
26 changes: 26 additions & 0 deletions 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()
}
10 changes: 5 additions & 5 deletions token/otp/new.go → token/code/new.go
@@ -1,4 +1,4 @@
package otp
package code

import (
"math"
Expand All @@ -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

Expand All @@ -26,15 +26,15 @@ loop:
for {
randomBuffer, err := buffer(step)
if err != nil {
return OTP{}, err
return Code{}, err
}

for i := 0; i < step; i++ {
currentIndex := int(randomBuffer[i]) & mask

if currentIndex < emojiAmount {
if _, err := code.WriteRune(em[currentIndex]); err != nil {
return OTP{}, err
return Code{}, err
} else if code.Len() == size {
break loop
}
Expand All @@ -47,7 +47,7 @@ loop:
}

split := strings.Split(code.String(), "")
return OTP{
return Code{
token: token,
emojies: emojies.ToEmojies(split),
}, nil
Expand Down
8 changes: 4 additions & 4 deletions token/otp/new_test.go → token/code/new_test.go
@@ -1,19 +1,19 @@
package otp
package code

import (
"testing"

"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 {
Expand Down
9 changes: 9 additions & 0 deletions 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
}
36 changes: 36 additions & 0 deletions 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)
}
}
10 changes: 5 additions & 5 deletions token/next.go
Expand Up @@ -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) {
Expand All @@ -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)
Expand Down
26 changes: 0 additions & 26 deletions token/otp/config.go

This file was deleted.

9 changes: 0 additions & 9 deletions token/otp/validate.go

This file was deleted.

36 changes: 0 additions & 36 deletions token/otp/validate_test.go

This file was deleted.

10 changes: 6 additions & 4 deletions 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
}

Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit fcdf295

Please sign in to comment.