token

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 23, 2025 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrTokenTooLong    = fmt.Errorf("token length exceeds maximum limit of %d characters", maxIDLength)
	ErrTokenInvalid    = fmt.Errorf("token contains invalid characters, only lowercase letters and digits are allowed")
	ErrInvalidTokenTTL = fmt.Errorf("ttl must be positive number")
)

Functions

This section is empty.

Types

type Token

type Token struct {
	ID     string
	Secret string
	TTL    time.Duration
}

func Decode

func Decode(encoded string) (*Token, error)

Decode parses a base64-encoded string into a Token instance. It validates the encoding and token format, ensuring data integrity. Accepts encoded which is a base64-encoded string containing token ID and Secret separated by a colon. Returns a Token containing the ID and Secret if decoding is successful. Returns an error if the base64 string is invalid or the token format is malformed.

func GenerateToken

func GenerateToken(keyID string, ttl int) (*Token, error)

GenerateToken creates a new token with the specified keyID and time-to-live (TTL). It validates the keyID's length and characters, generating a random keyID if none is provided. Accepts keyID as the identifier for the token and ttl as the duration in seconds; if ttl is 0, a default value is used. Returns the generated Token structure or an error if validation fails, or if ID/secret generation errors occur.

func (*Token) Encode

func (t *Token) Encode() string

Encode generates a base64-encoded string representation of the token. It combines the token's ID and Secret, separated by a colon, before encoding. Returns the encoded token string.

Jump to

Keyboard shortcuts

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