Documentation
¶
Index ¶
- Constants
- func CheckLoginMiddleware() mist.Middleware
- func RenewAccessToken(ctx *mist.Context) error
- func SetDefaultProvider(sp Provider)
- func UpdateClaims(ctx *mist.Context, claims Claims) error
- type Builder
- type Claims
- type MemorySession
- func (m *MemorySession) Claims() Claims
- func (m *MemorySession) Del(ctx context.Context, key string) error
- func (m *MemorySession) Destroy(ctx context.Context) error
- func (m *MemorySession) Get(ctx context.Context, key string) kit.AnyValue
- func (m *MemorySession) Set(ctx context.Context, key string, val any) error
- func (m *MemorySession) UpdateClaims(ctx *mist.Context, claims Claims) error
- type MiddlewareBuilder
- type Provider
- type Session
Constants ¶
const CtxSessionKey = "_session"
CtxSessionKey is the key under which session information is stored in the context.
Variables ¶
This section is empty.
Functions ¶
func CheckLoginMiddleware ¶
func CheckLoginMiddleware() mist.Middleware
CheckLoginMiddleware creates and returns a middleware that uses the default session provider to check if the user is logged in for each request. Returns: - mist.Middleware: The middleware function for checking login status.
func RenewAccessToken ¶
RenewAccessToken renews the access token for the session associated with the given context. Parameters: - ctx: The context associated with the current request. Returns: - error: Potential error during access token renewal.
func SetDefaultProvider ¶
func SetDefaultProvider(sp Provider)
SetDefaultProvider sets the provided session provider as the default provider. Parameters: - sp: The session provider to set as default.
func UpdateClaims ¶
UpdateClaims updates the session claims for the session associated with the given context. Parameters: - ctx: The context associated with the current request. - claims: The new claims to set for the session. Returns: - error: Potential error during claims update.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder serves as a constructor for creating new sessions with custom attributes.
func InitSessionBuilder ¶
InitSessionBuilder initializes a new Builder with the provided context and user ID. Parameters: - ctx: The context associated with the current request. - uid: The user ID for which the session is being built. Returns: - *Builder: A pointer to the newly initialized Builder instance.
func (*Builder) Build ¶
Build creates a new session using the data and provider set in the Builder. Returns: - Session: The newly created session object. - error: Potential error encountered during session initialization.
func (*Builder) SetJwtData ¶
SetJwtData assigns custom JWT data to the Builder. Parameters: - data: The JWT data map to store in the session. Returns: - *Builder: The Builder instance to allow for method chaining.
func (*Builder) SetProvider ¶
SetProvider sets the session provider for the Builder to the provided provider. Parameters: - p: The session provider to use for this session. Returns: - *Builder: The Builder instance to allow for method chaining.
type Claims ¶
type Claims struct {
Uid int64 // User ID associated with the session.
SSID string // Session ID.
Data map[string]string // Arbitrary data stored as part of the session claims.
}
Claims represent the data and identification associated with a session.
type MemorySession ¶
type MemorySession struct {
// contains filtered or unexported fields
}
MemorySession is an in-memory implementation of the Session interface. It stores session data and associated claims.
func InitMemorySession ¶
func InitMemorySession(cl Claims) *MemorySession
InitMemorySession initializes a new MemorySession with the given Claims. Parameters: - cl: Claims to associate with the new session. Returns: - *MemorySession: A pointer to the newly created MemorySession instance.
func (*MemorySession) Claims ¶
func (m *MemorySession) Claims() Claims
Claims returns the Claims associated with the session. Returns: - Claims: The current set of claims stored with the session.
func (*MemorySession) Del ¶
func (m *MemorySession) Del(ctx context.Context, key string) error
Del removes the value associated with the given key from the session. Parameters: - ctx: The context carrying deadline and cancellation information. - key: The key of the session data to delete. Returns: - error: Returns nil after successfully deleting the key-value pair, if any.
func (*MemorySession) Destroy ¶
func (m *MemorySession) Destroy(ctx context.Context) error
Destroy does nothing in MemorySession because there is no persistent store to clean up. Parameters: - ctx: The context carrying deadline and cancellation information. Returns: - error: Always returns nil for MemorySession.
func (*MemorySession) Get ¶
Get retrieves the value associated with the given key in the session's data map. Parameters: - ctx: The context carrying deadline and cancellation information. - key: The key of the session data to retrieve. Returns: - kit.AnyValue: A struct wrapping the retrieved value or an error if the key does not exist.
func (*MemorySession) Set ¶
Set stores the provided key-value pair into the session's data map. Parameters: - ctx: The context carrying deadline and cancellation information. - key: The key for the session data to store. - val: The value to associate with the key. Returns: - error: Returns nil after adding or updating the key-value pair in the session's data map.
func (*MemorySession) UpdateClaims ¶
func (m *MemorySession) UpdateClaims(ctx *mist.Context, claims Claims) error
UpdateClaims is a stub method for MemorySession. It doesn't perform any operations as claims are not updated dynamically in this implementation. Parameters: - ctx: The request context with HTTP-specific information. - claims: The new claims data to store in the session. Returns: - error: Always returns nil for MemorySession.
type MiddlewareBuilder ¶
type MiddlewareBuilder struct {
// contains filtered or unexported fields
}
MiddlewareBuilder is a type that encapsulates session management within middleware.
Fields: - sp: An instance of a Provider that manages session-related operations.
func (*MiddlewareBuilder) Build ¶
func (b *MiddlewareBuilder) Build() mist.Middleware
Build constructs a middleware function that integrates session management. This middleware retrieves the session for each request and stores it in the context if successful. If the session cannot be obtained, the middleware logs an error and aborts the request with an HTTP 401 Unauthorized status.
Returns: - mist.Middleware: A middleware function that handles session retrieval and management.
func (*MiddlewareBuilder) IgnorePaths ¶ added in v0.1.2
func (b *MiddlewareBuilder) IgnorePaths(pathPatterns ...string) *MiddlewareBuilder
IgnorePaths compiles the provided path patterns into regular expressions and adds them to the MiddlewareBuilder. This method allows specifying which paths the middleware should apply to. Parameters: - pathPatterns: a slice of strings representing the path patterns to be added. Returns: - the pointer to the MiddlewareBuilder instance to allow method chaining.
type Provider ¶
type Provider interface {
// InitSession initializes a session for the user with the provided data.
// Parameters:
// - ctx: The request context that includes HTTP-specific information.
// - uid: The user ID associated with the session.
// - jwtData: JWT-related data to be stored in the session.
// - sessData: General session data to be stored.
// Returns:
// - Session: The initialized session object.
// - error: Potential error that occurred during session initialization.
InitSession(ctx *mist.Context, uid int64, jwtData map[string]string, sessData map[string]any) (Session, error)
// Get retrieves the current session from the context.
// Parameters:
// - ctx: The request context that includes HTTP-specific information.
// Returns:
// - Session: The current session object.
// - error: Potential error that occurred during session retrieval.
Get(ctx *mist.Context) (Session, error)
// UpdateClaims updates the claims associated with the current session.
// Parameters:
// - ctx: The request context that includes HTTP-specific information.
// - claims: The new claims to be applied to the session.
// Returns:
// - error: Potential error that occurred during claims update.
UpdateClaims(ctx *mist.Context, claims Claims) error
// RenewAccessToken renews the access token for the current session.
// Parameters:
// - ctx: The request context that includes HTTP-specific information.
// Returns:
// - error: Potential error that occurred during access token renewal.
RenewAccessToken(ctx *mist.Context) error
}
Provider represents the session provider interface. It supports session initialization, retrieval, updating claims, and renewing the access token.
func DefaultProvider ¶
func DefaultProvider() Provider
DefaultProvider returns the current default session provider. Returns: - Provider: The default session provider.
type Session ¶
type Session interface {
// Set stores a key-value pair in the session.
// Parameters:
// - ctx: Context for managing request-scoped values, deadlines, and cancellations.
// - key: The key under which the value is stored.
// - val: The value to store.
// Returns:
// - error: Potential error that occurred while setting the value.
Set(ctx context.Context, key string, val any) error
// Get retrieves a value based on the key from the session.
// Parameters:
// - ctx: Context for managing request-scoped values, deadlines, and cancellations.
// - key: The key under which the value is stored.
// Returns:
// - kit.AnyValue: The value retrieved from the session, wrapped in an AnyValue type for flexibility.
Get(ctx context.Context, key string) kit.AnyValue
// Del deletes a key-value pair from the session based on the key.
// Parameters:
// - ctx: Context for managing request-scoped values, deadlines, and cancellations.
// - key: The key of the value to be deleted.
// Returns:
// - error: Potential error that occurred while deleting the value.
Del(ctx context.Context, key string) error
// Destroy removes the session entirely.
// Parameters:
// - ctx: Context for managing request-scoped values, deadlines, and cancellations.
// Returns:
// - error: Potential error that occurred while destroying the session.
Destroy(ctx context.Context) error
// Claims returns the claims associated with the session.
// Returns:
// - Claims: The claims related to the session.
Claims() Claims
}
Session represents the session management interface. It allows for getting, setting, and deleting session values, as well as destroying the session and retrieving session claims.
func Get ¶
Get retrieves the current session from the defaultProvider. Parameters: - ctx: The context associated with the current request. Returns: - Session: The session object retrieved. - error: Potential error during session retrieval.
func InitSession ¶
func InitSession(ctx *mist.Context, uid int64, jwtData map[string]string, sessData map[string]any) (Session, error)
InitSession initializes a session using the default session provider. Parameters: - ctx: The context associated with the current request. - uid: The user ID for which the session is being initialized. - jwtData: Map containing JWT-related data for the session. - sessData: Additional session data as a map. Returns: - Session: The newly initialized session object. - error: Potential error during session initialization.