Documentation
¶
Overview ¶
Package customsession provides dynamic session payload transformation and dynamic additional fields management.
Index ¶
- Constants
- Variables
- func PreserveResponseHeaders(w http.ResponseWriter, src http.Header)
- type Config
- type CustomSessionData
- type MemoryRepository
- type Option
- type Plugin
- func (p *Plugin) Config() Config
- func (p *Plugin) ID() string
- func (p *Plugin) Init(ctx *plugin.Context) error
- func (p *Plugin) Repository() Repository
- func (p *Plugin) SanitizePayload(payload any) any
- func (p *Plugin) ServeGetCustomSession(w http.ResponseWriter, r *http.Request, sessionData *dto.SessionData)
- func (p *Plugin) SessionInterceptor(next http.Handler) http.Handler
- func (p *Plugin) TransformSession(ctx context.Context, sessionData *dto.SessionData, req *http.Request) (any, error)
- type Repository
- type TransformEventPayload
- type TransformSessionFunc
Constants ¶
const ( // EventTransformBefore is emitted immediately before executing session transformation logic. EventTransformBefore = "customsession:transform:before" // EventTransformAfter is emitted immediately after a successful session transformation. EventTransformAfter = "customsession:transform:after" // EventTransformError is emitted when session transformation encounters an error. EventTransformError = "customsession:transform:error" )
Event constants for CustomSession plugin lifecycle hooks.
Variables ¶
var ( // ErrSessionNotFound is returned when a requested session entity does not exist in storage. ErrSessionNotFound = errors.New("customsession: session not found") // ErrInvalidTransformResult is returned when a transform callback returns an incompatible data structure. ErrInvalidTransformResult = errors.New("customsession: invalid transform result") // ErrRepositoryRequired is returned when an operation requires persistent storage but no repository was supplied. ErrRepositoryRequired = errors.New("customsession: repository implementation required") )
Functions ¶
func PreserveResponseHeaders ¶
func PreserveResponseHeaders(w http.ResponseWriter, src http.Header)
PreserveResponseHeaders copies custom headers and Set-Cookie headers from a source Header map to a ResponseWriter.
Types ¶
type Config ¶
type Config struct {
// TransformFunc is an optional custom callback for dynamic session payload transformation.
TransformFunc TransformSessionFunc
// MutateListDeviceSessions controls whether session transformation is applied to GET /multi-session/list-device-sessions.
// Default: false
MutateListDeviceSessions bool
// AllowedExtraKeys specifies which keys in session.Extra are preserved when FilterUnregisteredFields is enabled.
AllowedExtraKeys []string
// FilterUnregisteredFields controls whether extra fields not explicitly listed in AllowedExtraKeys are omitted from response JSON.
// Default: false
FilterUnregisteredFields bool
}
Config defines the configuration parameters for the CustomSession plugin.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns a Config struct initialized with recommended defaults.
type CustomSessionData ¶
type CustomSessionData struct {
User *entity.User `json:"user"`
Session *entity.Session `json:"session"`
Extra map[string]any `json:"extra,omitzero"`
}
CustomSessionData is the standard response container holding user, session, and dynamic extra payload fields.
type MemoryRepository ¶
type MemoryRepository struct {
*repository.MemorySessionRepository
// contains filtered or unexported fields
}
MemoryRepository provides a thread-safe in-memory implementation of the Repository interface.
func NewMemoryRepository ¶
func NewMemoryRepository() *MemoryRepository
NewMemoryRepository initializes and returns a new thread-safe in-memory custom session repository.
func (*MemoryRepository) GetCustomUserFields ¶
func (m *MemoryRepository) GetCustomUserFields(ctx context.Context, userID string) (map[string]any, error)
GetCustomUserFields retrieves dynamic user fields from in-memory storage.
func (*MemoryRepository) SaveCustomUserFields ¶
func (m *MemoryRepository) SaveCustomUserFields(ctx context.Context, userID string, fields map[string]any) error
SaveCustomUserFields stores dynamic user fields in in-memory storage.
type Option ¶
type Option func(*Config)
Option defines a functional option type for configuring the plugin.
func WithAllowedExtraKeys ¶ added in v1.1.0
WithAllowedExtraKeys registers allowed key names for session Extra metadata filtering.
func WithFilterUnregisteredFields ¶
WithFilterUnregisteredFields enables or disables strict filtering of unregistered extra fields.
func WithMutateListDeviceSessions ¶
WithMutateListDeviceSessions configures whether session transformation is also applied to device session listings.
func WithTransformFunc ¶
func WithTransformFunc(fn TransformSessionFunc) Option
WithTransformFunc configures a custom dynamic transformation callback for session payloads.
type Plugin ¶
type Plugin struct {
// contains filtered or unexported fields
}
Plugin implements the plugin.Plugin interface for dynamic session payload modification and dynamic fields management.
func New ¶
func New(repo Repository, opts ...Option) *Plugin
New instantiates a new CustomSession plugin with the provided repository and options.
func (*Plugin) Init ¶
Init initializes the plugin with the shared execution context and registers lifecycle event handlers.
func (*Plugin) Repository ¶
func (p *Plugin) Repository() Repository
Repository returns the underlying persistence repository instance.
func (*Plugin) SanitizePayload ¶
SanitizePayload filters unregistered additional fields from session payloads when FilterUnregisteredFields is true.
func (*Plugin) ServeGetCustomSession ¶
func (p *Plugin) ServeGetCustomSession(w http.ResponseWriter, r *http.Request, sessionData *dto.SessionData)
ServeGetCustomSession responds to GET /get-session requests with transformed session payloads.
func (*Plugin) SessionInterceptor ¶
SessionInterceptor creates an HTTP middleware that intercepts responses on GET /get-session and applies dynamic session transformation.
type Repository ¶
type Repository interface {
repository.SessionRepository
// GetCustomUserFields retrieves dynamic additional fields associated with a specific user ID.
GetCustomUserFields(ctx context.Context, userID string) (map[string]any, error)
// SaveCustomUserFields persists dynamic additional fields for a specific user ID.
SaveCustomUserFields(ctx context.Context, userID string, fields map[string]any) error
}
Repository defines the persistent storage contract required by the CustomSession plugin. Implement this interface on your custom database adapter (e.g. PostgreSQL, MySQL, SQLite, MongoDB, GORM, Redis).
type TransformEventPayload ¶
type TransformEventPayload struct {
// SessionData is the initial session and user payload before transformation.
SessionData *dto.SessionData `json:"sessionData"`
// TransformedData is the resulting payload produced by TransformSessionFunc.
TransformedData any `json:"transformedData,omitempty"`
// Request is the active HTTP request triggering the transformation.
Request *http.Request `json:"-"`
// Err contains any error encountered during transformation processing.
Err error `json:"error,omitempty"`
}
TransformEventPayload holds event metadata dispatched across Hooks during transformation execution.
type TransformSessionFunc ¶
type TransformSessionFunc func(ctx context.Context, sessionData *dto.SessionData, req *http.Request) (any, error)
TransformSessionFunc is the callback signature for transforming and enriching session response data dynamically per request.