token

package
v5.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 11, 2023 License: MIT Imports: 10 Imported by: 0

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

func NewGenerator(opts ...Option) (*Generator, error)

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

func (m *Generator) Next() (code.Code, error)

func (*Generator) Validate

func (m *Generator) Validate(moth string) bool
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

func (m *Generator) ValidateToken(oldToken string) bool

This should maybe not be used as you should not really expose the `token` to your users

Deprecated: Insecure. Use `Validate` instead.

type Option

type Option func(*Generator) error

func OptionWithAmount

func OptionWithAmount(amount int) Option

OptionWithAmount is used to specify the amount of emojies in a code.

func OptionWithEmojies

func OptionWithEmojies(emojies emojies.Emojies) Option

OptionWithEmojies is used to specify which emojies that can be used in any given code.

func OptionWithPeriod

func OptionWithPeriod(period time.Duration) Option

OptionWithPeriod is used to specify how long a code will remain valid.

func OptionWithSecret

func OptionWithSecret(secret string) Option

OptionWithSecret is used to specify the secret to generate codes from.

func OptionWithTime

func OptionWithTime(t time.Time) Option

OptionWithTime is used to specify a custom time to generate code from.

If none is specified, the current time will be used.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL