jwtutil

package
v0.35.0 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package jwtutil provides utilities and helpers for working with JWT tokens.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CacheProbeMiddleware

func CacheProbeMiddleware(cacheSizes []int, opts ...CacheProbeMiddlewareOption) func(next http.Handler) http.Handler

CacheProbeMiddleware is a middleware that simulates token caching from the Authorization header of incoming requests. It hashes the bearer token and stores it in multiple LRU caches with different sizes to measure cache hit rates. The middleware creates one cache for each size specified in the cacheSizes parameter.

This middleware is useful for:

  • Probing cache hit ratios in environments where real token caching is not yet implemented.
  • Estimating potential load on the introspection endpoint, since introspection result caches work similarly.
  • Determining optimal cache sizes for your workload before implementing actual caching.

The middleware exposes Prometheus metrics with the "go_authkit_token_claims" namespace and includes a "size" label to distinguish between different cache sizes. The metrics include hits, misses, evictions, and entries amount for each cache size. Note: This namespace is shared with actual JWT claims caching to allow direct comparison of cache behavior between probing and production caching. The metrics have a "source" label set to "cache_probe_middleware" to distinguish them from other components.

Parameters:

  • cacheSizes: Slice of cache sizes to create. Each size will have its own LRU cache. Example: []int{1000, 10000} creates two caches with 1k and 10k entries. IMPORTANT: All cache sizes must be positive integers (> 0), otherwise the function will panic.
  • opts: Optional configuration options:
  • WithCacheProbeMiddlewarePrometheusLibInstanceLabel: set custom Prometheus lib_instance label
  • WithCacheProbeMiddlewareTTL: set TTL for cached tokens (default: 1 minute)

Panics if any cache size is not a positive integer.

Types

type CacheProbeMiddlewareOption

type CacheProbeMiddlewareOption func(options *cacheProbeMiddlewareOpts)

CacheProbeMiddlewareOption is an option for CacheProbeMiddleware.

func WithCacheProbeMiddlewarePrometheusLibInstanceLabel

func WithCacheProbeMiddlewarePrometheusLibInstanceLabel(label string) CacheProbeMiddlewareOption

WithCacheProbeMiddlewarePrometheusLibInstanceLabel is an option to set a label for Prometheus metrics that are used by CacheProbeMiddleware.

func WithCacheProbeMiddlewareTTL

func WithCacheProbeMiddlewareTTL(ttl time.Duration) CacheProbeMiddlewareOption

WithCacheProbeMiddlewareTTL is an option to set the TTL (time-to-live) for cached tokens. Default is 1 minute.

Jump to

Keyboard shortcuts

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