token

package
v0.0.0-...-d5dcd45 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package token provides a web token maker. These tokens are used to authenticate users and store additional payload on user log in, for example, user id..

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is returned when the token is invalid.
	ErrInvalidToken = errors.New("token is invalid")
	// ErrExpiredToken is returned when the token is expired.
	ErrExpiredToken = errors.New("token has expired")
)
View Source
var ErrInvalidKeySize = fmt.Errorf("invalid key size: must be at least %d characters", minSecretKeySize)

ErrInvalidKeySize is returned when the secret key is too short.

Functions

This section is empty.

Types

type JWTMaker

type JWTMaker struct {
	// contains filtered or unexported fields
}

JWTMaker is a JWT web token maker.

func NewJWTMaker

func NewJWTMaker(secretKey string) (JWTMaker, error)

NewJWTMaker creates a new JWTMaker.

func (JWTMaker) CreateToken

func (maker JWTMaker) CreateToken(email string, duration time.Duration) (string, error)

CreateToken creates a new JWT web token for a specific username and duration.

func (JWTMaker) VerifyToken

func (maker JWTMaker) VerifyToken(token string) (*Payload, error)

VerifyToken checks if the token is valid or not.

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Email     string    `json:"email"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
}

Payload contains the payload data of the token.

func NewPayload

func NewPayload(email string, duration time.Duration) (*Payload, error)

NewPayload creates a new token payload with a specific username and duration.

func (*Payload) Valid

func (payload *Payload) Valid() error

Valid checks if the token payload is valid or not.

Jump to

Keyboard shortcuts

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