gophertoken

package module
v0.0.0-...-b54bd16 Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TokenTypeJWT    = "jwt"
	TokenTypePaseto = "paseto"
)
View Source
const Author = "github.com/lordofthemind"

Author of the gophertoken package

View Source
const Description = "" /* 127-byte string literal not displayed */

Description of the gophertoken package

View Source
const Version = "1.0.1"

Version of the gophertoken package

Variables

View Source
var (
	ErrInvalidToken = errors.New("token validation failed: signature invalid or claims malformed")
	ErrExpiredToken = errors.New("token validation failed: token has expired")
)

Errors related to token validation.

Functions

This section is empty.

Types

type JWTMaker

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

JWTMaker is a struct for handling JWT token creation and validation.

func (*JWTMaker) GenerateToken

func (j *JWTMaker) GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error)

GenerateToken creates a new JWT token for a specific user with a given duration.

Example usage:

token, err := maker.GenerateToken(userID, "username123", time.Hour)
if err != nil {
  log.Fatal(err)
}

func (*JWTMaker) ValidateToken

func (j *JWTMaker) ValidateToken(tokenString string) (*Payload, error)

ValidateToken checks if the given JWT token is valid.

Example usage:

payload, err := maker.ValidateToken(tokenString)
if err != nil {
  log.Fatal("Invalid token")
}

type PasetoMaker

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

PasetoMaker is a struct for handling Paseto token creation and validation.

func (*PasetoMaker) GenerateToken

func (maker *PasetoMaker) GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error)

GenerateToken creates a new Paseto token for a specific user with a given duration.

Example usage:

token, err := maker.GenerateToken(userID, "username123", time.Hour)
if err != nil {
  log.Fatal(err)
}

func (*PasetoMaker) ValidateToken

func (maker *PasetoMaker) ValidateToken(token string) (*Payload, error)

ValidateToken checks if the given Paseto token is valid.

Example usage:

payload, err := maker.ValidateToken(tokenString)
if err != nil {
  log.Fatal("Invalid token")
}

type Payload

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

Payload contains the data embedded within a token.

func NewPayload

func NewPayload(userID uuid.UUID, username string, duration time.Duration) (*Payload, error)

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

Example usage:

payload, err := NewPayload(userID, "username123", time.Hour)
if err != nil {
  log.Fatal(err)
}

func (*Payload) Valid

func (payload *Payload) Valid() error

Valid checks if the payload's expiration date has passed and returns an error if it has.

Example usage:

err := payload.Valid()
if err != nil {
  log.Fatal("Token expired")
}

type TokenManager

type TokenManager interface {
	GenerateToken(userID uuid.UUID, username string, duration time.Duration) (string, error)
	ValidateToken(token string) (*Payload, error)
}

TokenManager is the interface for creating and verifying tokens.

Example usage:

var manager TokenManager
manager, err = NewTokenManager("jwt", "your-secret-key")
if err != nil {
  log.Fatal(err)
}

func NewJWTMaker

func NewJWTMaker(secretKey string) (TokenManager, error)

NewJWTMaker creates a new JWTMaker with the given symmetric key.

Example usage:

maker, err := NewJWTMaker("your-secret-key")
if err != nil {
  log.Fatal(err)
}

func NewPasetoMaker

func NewPasetoMaker(secretKey string) (TokenManager, error)

NewPasetoMaker creates a new PasetoMaker with the given symmetric key.

Example usage:

maker, err := NewPasetoMaker("your-secret-key")
if err != nil {
  log.Fatal(err)
}

func NewTokenManager

func NewTokenManager(tokenType, secretKey string) (TokenManager, error)

NewTokenManager creates a new token manager (JWT or Paseto) depending on the provided type.

Example usage:

manager, err := NewTokenManager("jwt", "your-secret-key")
if err != nil {
  log.Fatal(err)
}

Jump to

Keyboard shortcuts

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