security

package
v0.1.18 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

View Source
const CtxSessionKey = "_session"

CtxSessionKey is a constant string used as a key for storing session data in the context.

Variables

This section is empty.

Functions

func CheckLoginMiddleware

func CheckLoginMiddleware(paths ...string) mist.Middleware

CheckLoginMiddleware creates a middleware that checks if the user is logged in for specified paths. Parameters: - paths: A variadic list of URL paths to be checked (string). Returns: - mist.Middleware: The constructed middleware.

func ClearToken added in v0.1.12

func ClearToken(ctx *mist.Context) error

ClearToken is a function that serves as a wrapper to invoke the ClearToken method of the defaultProvider. It clears the access and refresh tokens for a session by leveraging the default session provider.

Parameters:

  • ctx: The mist.Context object representing the current HTTP request and response.

Returns:

  • An error object if the underlying ClearToken method in defaultProvider fails, otherwise it returns nil.

func RenewAccessToken

func RenewAccessToken(ctx *mist.Context) error

RenewAccessToken renews the access token for the session associated with the given context. Parameters: - ctx: The request context (*mist.Context). Returns: - error: An error if the token renewal fails.

func SetDefaultProvider

func SetDefaultProvider(sp Provider)

SetDefaultProvider sets the default session provider. Parameters: - sp: The session provider to be set as the default (Provider).

func UpdateClaims

func UpdateClaims(ctx *mist.Context, claims Claims) error

UpdateClaims updates the claims for the session associated with the given context. Parameters: - ctx: The request context (*mist.Context). - claims: The claims to be updated (Claims). Returns: - error: An error if the claims update fails.

Types

type Builder

type Builder struct {
	// contains filtered or unexported fields
}

Builder is a structure that helps in building a session configuration step by step. It contains the context, user ID, JWT data, session data, and a session provider.

func InitSessionBuilder added in v0.1.11

func InitSessionBuilder(ctx *mist.Context, uid int64) *Builder

InitSessionBuilder initializes and returns a new instance of Builder with the given context and user ID. The default session provider is set during initialization. Parameters: - ctx: The request context (*mist.Context). - uid: The user ID for the session (int64). Returns: - *Builder: A pointer to a newly created Builder instance.

func (*Builder) Build

func (b *Builder) Build() (Session, error)

Build constructs the session using the provided or default session provider, context, user ID, JWT data, and session data. Returns: - Session: The newly created session. - error: An error if the session creation fails.

func (*Builder) SetJwtData

func (b *Builder) SetJwtData(data map[string]any) *Builder

SetJwtData sets the JWT data for the Builder. Parameters: - data: The JWT-related data (map[string]any). Returns: - *Builder: The Builder instance with the updated JWT data.

func (*Builder) SetProvider

func (b *Builder) SetProvider(p Provider) *Builder

SetProvider sets a custom session provider for the Builder. Parameters: - p: The custom session provider (Provider). Returns: - *Builder: The Builder instance with the updated provider.

func (*Builder) SetSessData

func (b *Builder) SetSessData(data map[string]any) *Builder

SetSessData sets the session data for the Builder. Parameters: - data: The additional session data (map[string]any). Returns: - *Builder: The Builder instance with the updated session data.

type Claims

type Claims struct {
	UserID    int64          // User ID
	SessionID string         // Session ID
	Data      map[string]any // Additional data related to the claims
}

Claims structure holds the data associated with the session's JWT claims.

func (Claims) Get

func (c Claims) Get(key string) mist.AnyValue

Get retrieves the value associated with the key from the claims.

type MiddlewareBuilder

type MiddlewareBuilder struct {
	// contains filtered or unexported fields
}

MiddlewareBuilder is a structure that helps build middleware for handling HTTP requests. It contains a provider for session management and a list of paths to be handled.

func InitMiddlewareBuilder added in v0.1.11

func InitMiddlewareBuilder(sp Provider, paths ...string) *MiddlewareBuilder

InitMiddlewareBuilder initializes and returns a new instance of MiddlewareBuilder. Parameters: - sp: The session provider for session management (Provider). - paths: A variadic list of URL paths to be handled by the middleware (string). Returns: - *MiddlewareBuilder: A pointer to a newly created MiddlewareBuilder instance.

func (*MiddlewareBuilder) Build

func (b *MiddlewareBuilder) Build() mist.Middleware

Build constructs the middleware function. Returns: - mist.Middleware: A middleware function that processes the HTTP requests.

type Provider

type Provider interface {
	// InitSession initializes a new session with the specified user ID, JWT data, and session data.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('mist.Context')
	// - uid: user ID for which the session is being created ('int64')
	// - jwtData: JWT token data (usually claims) to store with the session ('map[string]any')
	// - sessData: additional session-specific data to associate with the session ('map[string]any')
	// Returns:
	// - Session: the initialized session
	// - error: error, if any occurred while initializing the session
	InitSession(ctx *mist.Context, uid int64, jwtData map[string]any, sessData map[string]any) (Session, error)

	// Get retrieves the current session associated with the context.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('mist.Context')
	// Returns:
	// - Session: the current session
	// - error: error, if any occurred while retrieving the session
	Get(ctx *mist.Context) (Session, error)

	// UpdateClaims updates the claims associated with the current session.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('mist.Context')
	// - claims: a new set of claims to associate with the session ('Claims')
	// Returns:
	// - error: error, if any occurred while updating the claims
	UpdateClaims(ctx *mist.Context, claims Claims) error

	// RenewAccessToken renews the access token associated with the session.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('mist.Context')
	// Returns:
	// - error: error, if any occurred while renewing the access token
	RenewAccessToken(ctx *mist.Context) error

	// The ClearToken function is designed to remove or invalidate a security or session token associated with the given context.
	//
	// Parameters:
	// ctx: A pointer to a mist.Context object, which holds contextual information for the function to operate within.
	//      The mist.Context might include various details like user information, request scope, or environmental settings.
	//
	// Return:
	// error: This function returns an error type. If the token clearing process fails for any reason (e.g., token doesn't exist,
	//        network issues, permission issues), the function will return a non-nil error indicating what went wrong.
	//        If the token clearing process is successful, it returns nil.
	ClearToken(ctx *mist.Context) error
}

Provider interface defines methods for session lifecycle management and JWT claim updates.

func DefaultProvider

func DefaultProvider() Provider

DefaultProvider returns the current default session provider. Returns: - Provider: The current default session provider.

type Session

type Session interface {
	// Set assigns a value to a session key. The context is typically used for request-scoped values.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('context.Context')
	// - key: the key under which the value is stored ('string')
	// - val: the value to store, which can be of any type ('any')
	// Returns:
	// - error: error, if any occurred while setting the value
	Set(ctx context.Context, key string, val any) error

	// Get retrieves the value associated with the key from the session.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('context.Context')
	// - key: the key for the value to be retrieved ('string')
	// Returns:
	// - mist.AnyValue: a wrapper containing the retrieved value or an error if the key wasn't found
	Get(ctx context.Context, key string) mist.AnyValue

	// Del deletes the key-value pair associated with the key from the session.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('context.Context')
	// - key: the key for the value to be deleted ('string')
	// Returns:
	// - error: error, if any occurred while deleting the value
	Del(ctx context.Context, key string) error

	// Destroy invalidates the session entirely, clearing all data within the session.
	// Parameters:
	// - ctx: context for managing deadlines, cancel operation signals, and other request-scoped values ('context.Context')
	// Returns:
	// - error: error, if any occurred while destroying the session
	Destroy(ctx context.Context) error

	// Claims retrieves the claims associated with the session. Claims usually contain user-related data, often in a JWT context.
	// Returns:
	// - Claims: a set of claims related to the session
	Claims() Claims
}

Session interface defines multiple methods for session management.

func Get

func Get(ctx *mist.Context) (Session, error)

Get retrieves the session associated with the given context using the default provider. Parameters: - ctx: The request context (*mist.Context). Returns: - Session: The session associated with the context. - error: An error if the session retrieval fails.

func InitSession added in v0.1.11

func InitSession(ctx *mist.Context, uid int64, jwtData map[string]any, sessData map[string]any) (Session, error)

InitSession initializes a new session using the default provider. Parameters: - ctx: The request context (*mist.Context). - uid: User ID for the session (int64). - jwtData: JWT-related data to be included in the session (map[string]any). - sessData: Additional session data (map[string]any). Returns: - Session: The newly created session. - error: An error if the session creation fails.

Directories

Path Synopsis
kit

Jump to

Keyboard shortcuts

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