middleware

package
v0.6.7 Latest Latest
Warning

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

Go to latest
Published: Sep 8, 2025 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

View Source
const HttpStatusInternalError = 600

Variables

View Source
var SigningMethods struct {
	// RSA
	RS256 SigningMethod[*rsa.PrivateKey]

	// EdDSA
	EdDSA SigningMethod[ed25519.PrivateKey]
}

Functions

func Metrics

func Metrics(name string, args ...any) gin.HandlerFunc

func ResponseCsv

func ResponseCsv(c *gin.Context, data CsvData)

func ResponseError

func ResponseError(c *gin.Context, err error)

func ResponseSuccess

func ResponseSuccess(c *gin.Context, data any)

func Wrap

func Wrap(controller func(c *gin.Context) (any, error)) gin.HandlerFunc

Types

type CsvData

type CsvData struct {
	Filename string
	Data     [][]string
	// bom head is included by default, set true to exclude it.
	ExcludeBOMHeader bool
}

type Jwt

type Jwt[DATA any, KEY PrivateKey] struct {
	// contains filtered or unexported fields
}

func MustNewRandomJwt

func MustNewRandomJwt[T any, KEY PrivateKey](config JwtConfig, method SigningMethod[KEY]) *Jwt[T, KEY]

func NewJwt

func NewJwt[T any, KEY PrivateKey](config JwtConfig, method SigningMethod[KEY]) (*Jwt[T, KEY], error)

func (*Jwt[T, KEY]) ClaimsFromContext

func (j *Jwt[T, KEY]) ClaimsFromContext(c *gin.Context) (data T, err error)

ClaimsFromContext returns JWT claims that injected via middleware.

func (*Jwt[T, KEY]) Config

func (j *Jwt[T, KEY]) Config() JwtConfig

func (*Jwt[T, KEY]) Generate

func (j *Jwt[T, KEY]) Generate(data T, optNow ...time.Time) (string, string, error)

Generate generates access token and refresh token.

func (*Jwt[T, KEY]) Middleware

func (j *Jwt[T, KEY]) Middleware(c *gin.Context)

Middleware validates JWT token from HTTP Authorization header that in format of 'Bearer xxx'.

func (*Jwt[T, KEY]) RawClaimsFromContext added in v0.6.7

func (j *Jwt[T, KEY]) RawClaimsFromContext(c *gin.Context) (*JwtClaims[T], error)

ClaimsFromContext returns JWT claims that injected via middleware.

func (*Jwt[T, KEY]) Validate

func (j *Jwt[T, KEY]) Validate(tokenString string) (*JwtClaims[T], error)

Validate validates the specified JWT token and returns parsed claims.

type JwtClaims

type JwtClaims[T any] struct {
	jwt.RegisteredClaims

	Data T
}

type JwtConfig

type JwtConfig struct {
	Issuer  string
	Subject string

	AccessTokenTimeout  time.Duration `default:"10m"` // 10 minutes
	RefreshTokenTimeout time.Duration `default:"24h"` // 1 day

	Key string // private key in PEM format, key type could be ignored
}

type PrivateKey

type PrivateKey interface {
	Public() crypto.PublicKey
	Equal(x crypto.PrivateKey) bool
}

type SigningMethod

type SigningMethod[T PrivateKey] interface {
	MustGenerateKey() T
	ToPEM(key T) []byte
	FromPEM(data []byte) (T, error)

	JwtSigningMethod() jwt.SigningMethod
}

Jump to

Keyboard shortcuts

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