session_to_jwt

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 5, 2025 License: Apache-2.0 Imports: 24 Imported by: 0

README

Caddy Session to JWT

A Caddy v2 module that converts sessionID form a cookie to a JWT token. This JWT token can be used to authenticate the user in the backend. The JWT token is fetched from a Redis store and/or from a http request to a backend server (and then stored in Redis).

Redis code based on: https://github.com/pberkel/caddy-storage-redis

Installation

xcaddy build --with github.com/GetThePointGit/caddy-session-to-jwt

Configuration

todo:


:80 {
  session_to_jwt {
    host localhost
    port 6379
    db 0
    password "wachtwoord"
    key_prefix "SessionID:"
  }

  reverse_proxy localhost:3000
}


compile with custom caddy plugin locally for development

  1. installeer go

  2. installeer xcaddy, zie binaries op https://github.com/caddyserver/xcaddy/releases

  3. build with command ./xcaddy build --with github.com/GetThePointGit/caddy-session-to-jwt=./

  4. run caddy with ./caddy run

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RedisSessionTokenData

type RedisSessionTokenData struct {
	SessionID       string    `json:"SessionID"`
	SessionExpiryDt time.Time `json:"SessionExpiryDt"`
	Token           []byte    `json:"Token"`
	TokenExpiryDt   time.Time `json:"TokenExpiryDt"`
	TokenEncrypted  int       `json:"TokenEncrypted"`
	UserId          string    `json:"UserId"`
	LastDayUsed     time.Time `json:"LastDayUsed"`
}

type SessionTokenMiddleware

type SessionTokenMiddleware struct {
	GetJwtFromUrl bool   `json:"get_jwt_from_url"`
	JwtUrl        string `json:"jwt_url"`

	// ClientType specifies the Redis client type. Valid values are "cluster" or "failover"
	ClientType string `json:"client_type"`
	// Address The full address of the Redis server. Example: "127.0.0.1:6379"
	// If not defined, will be generated from Host and Port parameters.
	Address []string `json:"address"`
	// Host The Redis server hostname or IP address. Default: "127.0.0.1"
	Host []string `json:"host"`
	// Host The Redis server port number. Default: "6379"
	Port []string `json:"port"`
	// DB The Redis server database number. Default: 0
	DB int `json:"db"`
	// Timeout The Redis server timeout in seconds. Default: 5
	Timeout string `json:"timeout"`
	// Username The username for authenticating with the Redis server. Default: "" (No authentication)
	Username string `json:"username"`
	// Password The password for authenticating with the Redis server. Default: "" (No authentication)
	Password string `json:"password"`
	// MasterName Only required when connecting to Redis via Sentinal (Failover mode). Default ""
	MasterName string `json:"master_name"`
	// KeyPrefix A string prefix that is appended to Redis keys. Default: "caddy"
	// Useful when the Redis server is used by multiple applications.
	KeyPrefix string `json:"key_prefix"`
	// EncryptionKey A key string used to symmetrically encrypt and decrypt data stored in Redis.
	// The key must be exactly 32 characters, longer values will be truncated. Default: "" (No encryption)
	EncryptionKey string `json:"encryption_key"`
	// Compression Specifies whether values should be compressed before storing in Redis. Default: false
	TlsEnabled bool `json:"tls_enabled"`
	// TlsInsecure controls whether the client will verify the server
	// certificate. See `InsecureSkipVerify` in `tls.Config` for details. True
	// by default.
	// https://pkg.go.dev/crypto/tls#Config
	TlsInsecure bool `json:"tls_insecure"`
	// TlsServerCertsPEM is a series of PEM encoded certificates that will be
	// used by the client to validate trust in the Redis server's certificate
	// instead of the system trust store. May not be specified alongside
	// `TlsServerCertsPath`. See `x509.CertPool.AppendCertsFromPem` for details.
	// https://pkg.go.dev/crypto/x509#CertPool.AppendCertsFromPEM
	TlsServerCertsPEM string `json:"tls_server_certs_pem"`
	// TlsServerCertsPath is the path to a file containing a series of PEM
	// encoded certificates that will be used by the client to validate trust in
	// the Redis server's certificate instead of the system trust store. May not
	// be specified alongside `TlsServerCertsPem`. See
	// `x509.CertPool.AppendCertsFromPem` for details.
	// https://pkg.go.dev/crypto/x509#CertPool.AppendCertsFromPEM
	TlsServerCertsPath string `json:"tls_server_certs_path"`
	// RouteByLatency Route commands by latency, only used in Cluster mode. Default: false
	RouteByLatency bool `json:"route_by_latency"`
	// RouteRandomly Route commands randomly, only used in Cluster mode. Default: false
	RouteRandomly bool `json:"route_randomly"`
	// contains filtered or unexported fields
}

RedisStorage implements a Caddy storage backend for Redis It supports Single (Standalone), Cluster, or Sentinal (Failover) Redis server configurations.

func (SessionTokenMiddleware) CaddyModule

func (SessionTokenMiddleware) CaddyModule() caddy.ModuleInfo

CaddyModule returns the Caddy module information.

func (*SessionTokenMiddleware) Cleanup

func (m *SessionTokenMiddleware) Cleanup() error

func (*SessionTokenMiddleware) Delete

func (m *SessionTokenMiddleware) Delete(ctx context.Context, sessionId string) error

func (*SessionTokenMiddleware) Exists

func (m *SessionTokenMiddleware) Exists(ctx context.Context, sessionId string) bool

func (*SessionTokenMiddleware) GetClient

func (m *SessionTokenMiddleware) GetClient() any

GetClient returns the Redis client initialized by this storage.

This is useful for other modules that need to interact with the same Redis instance. The return type of GetClient is "any" for forward-compatibility new versions of go-redis. The returned value must usually be cast to redis.UniversalClient.

func (*SessionTokenMiddleware) Load

func (m *SessionTokenMiddleware) Load(ctx context.Context, sessionId string) (string, error)

func (*SessionTokenMiddleware) Lock

func (m *SessionTokenMiddleware) Lock(ctx context.Context, name string) error

func (*SessionTokenMiddleware) Provision

func (m *SessionTokenMiddleware) Provision(ctx caddy.Context) error

Provision module function called by Caddy Server

func (SessionTokenMiddleware) ServeHTTP

ServeHTTP implements caddyhttp.MiddlewareHandler.

func (*SessionTokenMiddleware) Store

func (m *SessionTokenMiddleware) Store(ctx context.Context, sessionId string, token []byte, sessionExpiryDt time.Time, tokenExpiryDt time.Time) error

func (*SessionTokenMiddleware) String

func (m *SessionTokenMiddleware) String() string

func (*SessionTokenMiddleware) Unlock

func (m *SessionTokenMiddleware) Unlock(ctx context.Context, name string) error

func (*SessionTokenMiddleware) UnmarshalCaddyfile

func (m *SessionTokenMiddleware) UnmarshalCaddyfile(d *caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

func (*SessionTokenMiddleware) Validate

func (m *SessionTokenMiddleware) Validate() error

Validate implements caddy.Validator.

Jump to

Keyboard shortcuts

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