Documentation ¶
Index ¶
- type Generator
- func (g *Generator) Check() error
- func (g *Generator) Export() string
- func (g *Generator) Import(encoded string) error
- func (g *Generator) Next() (code.Code, error)
- func (g *Generator) SetAmount(amount int) error
- func (g *Generator) SetEmojies(emojies emojies.Emojies) error
- func (g *Generator) SetPeriod(period time.Duration) error
- func (g *Generator) SetSecret(secret string) error
- func (g *Generator) SetTime(t time.Time) error
- func (g *Generator) Validate(moth string) bool
- func (g *Generator) ValidateToken(oldToken string) booldeprecated
- type Option
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Generator ¶
type Generator struct {
// contains filtered or unexported fields
}
func NewGenerator ¶
NewGenerator creates a new token generator with the given [Option]s.
Example ¶
Instantiate a new generator with a secret, period, amount, and emojies.
Use this generator to generate new codes from the options provided.
package main import ( "strings" "time" "github.com/Mobilpadde/moths/v6/token" "github.com/Mobilpadde/moths/v6/token/emojies" "github.com/Mobilpadde/moths/v6/token/option" ) func main() { amount := 6 secret := strings.Repeat("a", 32) gen, _ := token.NewGenerator( option.OptionWithSecret(secret), option.OptionWithPeriod(time.Second), option.OptionWithAmount(amount), option.OptionWithEmojies(emojies.CATS), ) // It's good practice to use the run `g.Check()` // method to check if everything is working. // This is not requried though. if err := gen.Check(); err != nil { panic(err) } gen.Next() }
Output:
Example (Set_period) ¶
Instantiate a new generator with a secret, amount, and emojies.
We omit the period option and set it later.
This can be done with the other options as well.
package main import ( "strings" "time" "github.com/Mobilpadde/moths/v6/token" "github.com/Mobilpadde/moths/v6/token/emojies" "github.com/Mobilpadde/moths/v6/token/option" ) func main() { secret := strings.Repeat("a", 32) gen, _ := token.NewGenerator( option.OptionWithSecret(secret), option.OptionWithAmount(6), option.OptionWithEmojies(emojies.CATS), ) // If we run `gen.Check()` here, // it'll return an error, because // the period is not set. gen.Check() // not nil // set the period to 1 second gen.SetPeriod(time.Second) // If we run `gen.Check()` again, // everything works, as expected // because the period is now set. gen.Check() // nil }
Output:
Example (With_time) ¶
Instantiates a generator as above, but also with a specified time.
This will **always** generate the same codes *virtually forever*.
If we chose to generate **five** codes, these would be as follows:
🙀 😾 😹 🙀 😼 😹
😻 😹 😽 😹 😽 😿
😹 😽 😻 😸 😻 🙀
😼 😼 😾 😾 😿 😹
😿 😽 😿 🙀 😼 😻
package main import ( "strings" "time" "github.com/Mobilpadde/moths/v6/token" "github.com/Mobilpadde/moths/v6/token/emojies" "github.com/Mobilpadde/moths/v6/token/option" ) func main() { secret := strings.Repeat("a", 32) gen, _ := token.NewGenerator( option.OptionWithSecret(secret), option.OptionWithPeriod(time.Second), option.OptionWithAmount(6), option.OptionWithEmojies(emojies.CATS), option.OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)), ) gen.Next() }
Output:
func (*Generator) Check ¶
Check is checking if everything is working
Meaning no errors is generated from getting codes
func (*Generator) Export ¶
Encode generators fields as a base64 string.
Should not be shared, as this shows the secret as a string if this is decoded.
func (*Generator) SetEmojies ¶
SetEmojies is used to specify which emojies that can be used in any given code.
func (*Generator) SetTime ¶
OptionWithTime is used to specify a custom time to generate code from.
If none is specified, the current time will be used.
func (*Generator) Validate ¶
Example ¶
package main import ( "log" "strings" "time" "github.com/Mobilpadde/moths/v6/token" "github.com/Mobilpadde/moths/v6/token/emojies" "github.com/Mobilpadde/moths/v6/token/option" ) func main() { amount := 6 secret := strings.Repeat("a", 32) var err error var gen *token.Generator if gen, err = token.NewGenerator( option.OptionWithSecret(secret), option.OptionWithPeriod(time.Second), option.OptionWithAmount(amount), option.OptionWithEmojies(emojies.CATS), ); err != nil { log.Fatalln(err) } code, _ := gen.Next() gen.Validate(code.String()) // This is true, as it's validated within specified period. }
Output: