jwtauth

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 13, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type JwtAuth

type JwtAuth[T jwt.Claims] struct {
	// contains filtered or unexported fields
}

JwtAuth provides JWT token generation and verification functionality. It is parameterized by the claims type for type safety.

func NewJwtAuth

func NewJwtAuth[T jwt.Claims](secret string, options ...Option) *JwtAuth[T]

NewJwtAuth creates a new JWT authenticator with the specified secret and options. The default signing method is HMAC-SHA256.

func (*JwtAuth[T]) GenerateToken

func (g *JwtAuth[T]) GenerateToken(ctx context.Context, claims T) (string, error)

GenerateToken creates a signed JWT token from the provided claims.

func (*JwtAuth[T]) ParseToken

func (g *JwtAuth[T]) ParseToken(ctx context.Context, signedToken string) (T, error)

ParseToken parses and validates a signed JWT token, returning the claims. It handles both direct jwt.Claims types and custom structs, using JSON marshaling/unmarshaling for struct conversion when necessary.

type Option

type Option func(*options)

Option is a functional option for configuring JWT authentication.

func WithSigningMethod

func WithSigningMethod(method jwt.SigningMethod) Option

WithSigningMethod sets the JWT signing method for token generation and verification.

type RBACClaims

type RBACClaims[T authorizer.UID] struct {
	jwt.RegisteredClaims
	UID   T        `json:"uid,omitempty"`
	Roles []string `json:"roles,omitempty"`
}

func NewRBACClaims

func NewRBACClaims[T authorizer.UID](uid T, subject string, roles []string, expiresAt time.Time) RBACClaims[T]

func (RBACClaims[T]) GetRoles

func (r RBACClaims[T]) GetRoles() ([]string, error)

func (RBACClaims[T]) GetUID

func (r RBACClaims[T]) GetUID() (T, error)

Jump to

Keyboard shortcuts

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