token

package
v6.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2023 License: MIT Imports: 14 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.

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

func (g *Generator) Check() error

Check is checking if everything is working

Meaning no errors is generated from getting codes

func (*Generator) Export

func (g *Generator) Export() string

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

func (g *Generator) Import(encoded string) error

Try parsing a given base64 encoded string into Generator.

func (*Generator) Next

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

func (*Generator) SetAmount

func (g *Generator) SetAmount(amount int) error

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

func (*Generator) SetEmojies

func (g *Generator) SetEmojies(emojies emojies.Emojies) error

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

func (*Generator) SetPeriod

func (g *Generator) SetPeriod(period time.Duration) error

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

func (*Generator) SetSecret

func (g *Generator) SetSecret(secret string) error

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

func (*Generator) SetTime

func (g *Generator) SetTime(t time.Time) error

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

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

func (*Generator) ValidateToken deprecated

func (g *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

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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