Documentation ¶
Index ¶
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.
amount := 6 secret := strings.Repeat("a", 32) gen, _ := NewGenerator( OptionWithSecret(secret), OptionWithPeriod(time.Second), OptionWithAmount(amount), OptionWithEmojies(emojies.CATS), ) gen.Next()
Output:
Example (WithTime) ¶
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:
🙀 😾 😹 🙀 😼 😹
😻 😹 😽 😹 😽 😿
😹 😽 😻 😸 😻 🙀
😼 😼 😾 😾 😿 😹
😿 😽 😿 🙀 😼 😻
amount := 6 secret := strings.Repeat("a", 32) gen, _ := NewGenerator( OptionWithSecret(secret), OptionWithPeriod(time.Second), OptionWithAmount(amount), OptionWithEmojies(emojies.CATS), OptionWithTime(time.Date(0, 0, 0, 0, 0, 0, 0, time.UTC)), ) gen.Next()
Output:
func (*Generator) Validate ¶
Example ¶
amount := 6 secret := strings.Repeat("a", 32) var err error var gen *Generator if gen, err = NewGenerator( OptionWithSecret(secret), OptionWithPeriod(time.Second), OptionWithAmount(amount), 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:
func (*Generator) ValidateToken
deprecated
type Option ¶
func OptionWithAmount ¶
OptionWithAmount is used to specify the amount of emojies in a code.
func OptionWithEmojies ¶
OptionWithEmojies is used to specify which emojies that can be used in any given code.
func OptionWithPeriod ¶
OptionWithPeriod is used to specify how long a code will remain valid.
func OptionWithSecret ¶
OptionWithSecret is used to specify the secret to generate codes from.
func OptionWithTime ¶
OptionWithTime is used to specify a custom time to generate code from.
If none is specified, the current time will be used.