token

package
v0.2.5 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package token provides PASETO v4 token validation for microservices.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidToken is returned when the token cannot be parsed or verified.
	ErrInvalidToken = errors.New("invalid token")
	// ErrExpiredToken is returned when the token has expired.
	ErrExpiredToken = errors.New("token expired")
	// ErrInvalidPublicKey is returned when the public key is invalid.
	ErrInvalidPublicKey = errors.New("invalid public key")
)
View Source
var ErrInsufficientPermissions = errors.New("insufficient permissions")

ErrInsufficientPermissions is returned when user lacks required permissions.

Functions

func ContextWithClaims

func ContextWithClaims(ctx context.Context, claims *Claims) context.Context

ContextWithClaims returns a new context with the claims stored.

func NewTokenValidatorModule

func NewTokenValidatorModule() fx.Option

NewTokenValidatorModule provides a TokenValidator for dependency injection.

Types

type Claims

type Claims struct {
	// UserID is the unique identifier of the user (subject).
	UserID string
	// Role is the user's role (e.g., "super_admin", "catalog_manager", "viewer").
	Role string
	// Permissions is the list of permissions granted to the user.
	Permissions []string
	// Type is the token type (e.g., "access", "refresh").
	Type string
	// IssuedAt is the time when the token was issued.
	IssuedAt time.Time
	// ExpiresAt is the time when the token expires.
	ExpiresAt time.Time
	// NotBefore is the time before which the token is not valid.
	NotBefore time.Time
	// contains filtered or unexported fields
}

Claims represents the token claims. This is used across all services for authentication.

func ClaimsFromContext

func ClaimsFromContext(ctx context.Context) *Claims

ClaimsFromContext retrieves claims from the context. Returns nil if no claims are present.

func HandleBearerAuth

func HandleBearerAuth(
	validator TokenValidator,
	ctx context.Context,
	tokenString string,
	requiredPermissions []string,
) (context.Context, *Claims, error)

HandleBearerAuth validates a token and checks permissions. It returns the context with claims stored, the claims, and any error.

Usage in your service:

func (s *securityHandler) HandleBearerAuth(ctx context.Context, operationName httpapi.OperationName, t httpapi.BearerAuth) (context.Context, error) {
    ctx, _, err := token.HandleBearerAuth(s.validator, ctx, t.Token, t.Roles)
    return ctx, err
}

func (*Claims) Get

func (c *Claims) Get(key string, v any) error

Get unmarshals a custom claim from the token into the provided value.

func (*Claims) GetString

func (c *Claims) GetString(key string) (string, error)

GetString returns a custom string claim from the token.

func (*Claims) HasAnyPermission

func (c *Claims) HasAnyPermission(permissions []string) bool

HasAnyPermission checks if the user has at least one of the required permissions.

func (*Claims) HasPermission

func (c *Claims) HasPermission(permission string) bool

HasPermission checks if the user has a specific permission.

func (*Claims) IsAccess

func (c *Claims) IsAccess() bool

IsAccess returns true if the token is an access token.

func (*Claims) IsExpired

func (c *Claims) IsExpired() bool

IsExpired checks if the token has expired.

func (*Claims) IsRefresh

func (c *Claims) IsRefresh() bool

IsRefresh returns true if the token is a refresh token.

type Config

type Config struct {
	// PublicKey is the hex-encoded Ed25519 public key for verifying tokens.
	PublicKey string `mapstructure:"public-key"`
}

Config holds the configuration for PASETO token validation.

type TokenValidator

type TokenValidator interface {
	// ValidateToken validates a token and returns the claims.
	ValidateToken(token string) (*Claims, error)
}

TokenValidator validates tokens and returns claims.

Jump to

Keyboard shortcuts

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