auth

package
v0.0.0-...-f34ca43 Latest Latest
Warning

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

Go to latest
Published: May 23, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidToken = errors.New(constants.ErrInvalidToken)
	ErrExpiredToken = errors.New(constants.ErrExpiredToken)
)

Different types of error returned by the VerifyToken function

Functions

func DecodePassword

func DecodePassword(password string, hashedPassword string) error

CheckPassword checks if the provided password is correct or not

func EncodePassword

func EncodePassword(password string) (string, error)

HashPassword returns the bcrypt hash of the password

Types

type JWTMaker

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

JWTMaker is a JSON Web Token maker

func (*JWTMaker) CreateToken

func (maker *JWTMaker) CreateToken(username string, role string, duration time.Duration) (string, *Payload, error)

CreateToken creates a new 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 Maker

type Maker interface {
	// CreateToken creates a new token for a specific username and duration
	CreateToken(username string, role string, duration time.Duration) (string, *Payload, error)

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

Maker is an interface for managing tokens

var JwtMaker Maker

func GetJWTMaker

func GetJWTMaker() (Maker, error)

func NewJWTMaker

func NewJWTMaker(secretKey string) (Maker, error)

type Payload

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

Payload contains the payload data of the token

func NewPayload

func NewPayload(username string, role 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