Skip to content

Commit

Permalink
Rename Moths to Generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mobilpadde committed Mar 7, 2023
1 parent 6105848 commit 4c973ef
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 27 deletions.
16 changes: 8 additions & 8 deletions main.go
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"time"

"github.com/Mobilpadde/moths/v4/moths"
"github.com/Mobilpadde/moths/v4/token"
"github.com/Mobilpadde/moths/v4/token/emojies"
)

Expand All @@ -20,13 +20,13 @@ func main() {
validationTicker := time.NewTicker(validationInterval)

var err error
var gen *moths.Moths
if gen, err = moths.NewMoths(
moths.OptionWithSecret(secret),
moths.OptionWithInterval(generationInterval),
moths.OptionWithAmount(amount),
moths.OptionWithEmojies(emojies.CATS),
moths.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)),
var gen *token.Generator
if gen, err = token.NewGenerator(
token.OptionWithSecret(secret),
token.OptionWithInterval(generationInterval),
token.OptionWithAmount(amount),
token.OptionWithEmojies(emojies.CATS),
token.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)),
); err != nil {
log.Fatalln(err)
}
Expand Down
4 changes: 2 additions & 2 deletions token/new.go
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/Mobilpadde/moths/v4/token/checks"
)

func NewMoths(opts ...option) (*Moths, error) {
m := &Moths{
func NewGenerator(opts ...option) (*Generator, error) {
m := &Generator{
interval: 0,
amount: 6, // Defaults to `6` as most other TOTP services uses that
}
Expand Down
10 changes: 5 additions & 5 deletions token/new_test.go
Expand Up @@ -14,7 +14,7 @@ func TestNewMoths(t *testing.T) {
amount := 6
secret := strings.Repeat("a", 32)

if _, err := NewMoths(
if _, err := NewGenerator(
OptionWithSecret(secret),
OptionWithInterval(time.Second),
OptionWithAmount(amount),
Expand All @@ -27,7 +27,7 @@ func TestNewMoths(t *testing.T) {
func TestNewMothsNoSecret(t *testing.T) {
amount := 6

_, err := NewMoths(
_, err := NewGenerator(
OptionWithSecret(""),
OptionWithInterval(time.Second),
OptionWithAmount(amount),
Expand All @@ -43,7 +43,7 @@ func TestNewMothsNoInterval(t *testing.T) {
amount := 6
secret := strings.Repeat("a", 32)

_, err := NewMoths(
_, err := NewGenerator(
OptionWithSecret(secret),
OptionWithAmount(amount),
OptionWithEmojies(emojies.CATS),
Expand All @@ -57,7 +57,7 @@ func TestNewMothsNoInterval(t *testing.T) {
func TestNewMothsNoAmount(t *testing.T) {
secret := strings.Repeat("a", 32)

_, err := NewMoths(
_, err := NewGenerator(
OptionWithSecret(secret),
OptionWithAmount(0),
OptionWithInterval(time.Second),
Expand All @@ -73,7 +73,7 @@ func TestNewMothsNoEmojies(t *testing.T) {
amount := 6
secret := strings.Repeat("a", 32)

_, err := NewMoths(
_, err := NewGenerator(
OptionWithSecret(secret),
OptionWithInterval(time.Second),
OptionWithAmount(amount),
Expand Down
4 changes: 2 additions & 2 deletions token/next.go
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/Mobilpadde/moths/v4/token/otp"
)

func (m *Moths) Next() (otp.OTP, error) {
func (m *Generator) Next() (otp.OTP, error) {
token, err := m.getToken()
if err != nil {
return otp.OTP{}, err
Expand All @@ -20,7 +20,7 @@ func (m *Moths) Next() (otp.OTP, error) {
return otp.NewOTP(token, m.amount, m.emojies)
}

func (m *Moths) getToken() (string, error) {
func (m *Generator) getToken() (string, error) {
m.timing.curr = time.Now().UTC()

since := m.timing.curr.Sub(m.timing.last)
Expand Down
2 changes: 1 addition & 1 deletion token/next_test.go
Expand Up @@ -12,7 +12,7 @@ func TestNextAndValidate(t *testing.T) {
amount := 6
secret := strings.Repeat("a", 32)

gen, err := NewMoths(
gen, err := NewGenerator(
OptionWithSecret(secret),
OptionWithInterval(time.Second),
OptionWithAmount(amount),
Expand Down
14 changes: 7 additions & 7 deletions token/options.go
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/Mobilpadde/moths/v4/token/emojies"
)

type Moths struct {
type Generator struct {
secret []byte
amount int
emojies emojies.Emojies
Expand All @@ -23,10 +23,10 @@ type Moths struct {
}
}

type option func(*Moths) error
type option func(*Generator) error

func OptionWithSecret(secret string) option {
return func(m *Moths) error {
return func(m *Generator) error {
if err := checks.CheckSecret(secret); err != nil {
return err
}
Expand All @@ -42,7 +42,7 @@ func OptionWithSecret(secret string) option {
}

func OptionWithInterval(interval time.Duration) option {
return func(m *Moths) error {
return func(m *Generator) error {
if err := checks.CheckInterval(interval); err != nil {
return err
}
Expand All @@ -53,7 +53,7 @@ func OptionWithInterval(interval time.Duration) option {
}

func OptionWithAmount(amount int) option {
return func(m *Moths) error {
return func(m *Generator) error {
if err := checks.CheckAmount(amount); err != nil {
return err
}
Expand All @@ -64,7 +64,7 @@ func OptionWithAmount(amount int) option {
}

func OptionWithEmojies(emojies emojies.Emojies) option {
return func(m *Moths) error {
return func(m *Generator) error {
if err := checks.CheckEmojies(emojies); err != nil {
return err
}
Expand All @@ -75,7 +75,7 @@ func OptionWithEmojies(emojies emojies.Emojies) option {
}

func OptionWithTime(t time.Time) option {
return func(m *Moths) error {
return func(m *Generator) error {
m.timing.time = t
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions token/validate.go
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/Mobilpadde/moths/v4/token/otp"
)

func (m *Moths) Validate(moth string) bool {
func (m *Generator) Validate(moth string) bool {
if len(moth)/otp.EmojiBytes != m.amount {
return false
}
Expand All @@ -25,7 +25,7 @@ func (m *Moths) Validate(moth string) bool {
// This should maybe not be used
// as you should not really expose the `token`
// to your users
func (m *Moths) ValidateToken(oldToken string) bool {
func (m *Generator) ValidateToken(oldToken string) bool {
token, err := m.getToken()
if err != nil {
return false
Expand Down

0 comments on commit 4c973ef

Please sign in to comment.