token

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2022 License: BSD-3-Clause Imports: 7 Imported by: 1

README

token

Go JWT/Paseto token package.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New("token is invalid.")
	ErrExpiredToken = errors.New("token has expired.")
)

Different types of error returned by the VerifyToken function

Functions

This section is empty.

Types

type Builder

type Builder interface {
	// CreateToken creates a new token for a specific username and duration
	CreateToken(username string, duration time.Duration, claims Claims) (string, error)

	// VerifyToken checks if the token is valid or not
	VerifyToken(token string) (*Payload, error)
}

Builder is an interface for managing tokens

func NewJWTBuilder

func NewJWTBuilder(secretKey string, signatureMethod jwt.SigningMethod) (Builder, error)

NewJWTBuilder creates a new JWTBuilder

func NewPasetoBuilder

func NewPasetoBuilder(symmetricKey string) (Builder, error)

NewPasetoBuilder creates a new PasetoTokenBuilder

type Claims

type Claims map[string]interface{}

type JWTBuilder

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

JWTBuilder is a JSON Web Token builder

func (*JWTBuilder) CreateToken

func (builder *JWTBuilder) CreateToken(username string, duration time.Duration, claims Claims) (string, error)

CreateToken creates a new token for a specific username and duration

func (*JWTBuilder) VerifyToken

func (builder *JWTBuilder) VerifyToken(token string) (*Payload, error)

VerifyToken checks if the token is valid or not

type PasetoTokenBuilder

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

PasetoTokenBuilder is a PASETO token builder

func (*PasetoTokenBuilder) CreateToken

func (builder *PasetoTokenBuilder) CreateToken(username string, duration time.Duration, claims Claims) (string, error)

CreateToken creates a new token for a specific username and duration

func (*PasetoTokenBuilder) VerifyToken

func (builder *PasetoTokenBuilder) VerifyToken(token string) (*Payload, error)

VerifyToken checks if the token is valid or not

type Payload

type Payload struct {
	ID        uuid.UUID `json:"id"`
	Username  string    `json:"username"`
	IssuedAt  time.Time `json:"issued_at"`
	ExpiredAt time.Time `json:"expired_at"`
	Claims    Claims    `json:"claims"`
}

Payload contains the payload data of the token

func NewPayload

func NewPayload(username string, duration time.Duration, claims Claims) (*Payload, error)

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

func (*Payload) Valid

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