Documentation
¶
Index ¶
- Variables
- func AuthMiddleware(config MiddlewareConfig) gin.HandlerFunc
- func DecodeSessionData(sessionData, secretKey string) (string, error)
- func DecodeSessionDataWithMaxAge(sessionData, secretKey string, maxAgeSeconds int) (string, error)
- func DecodeSessionDataWithSalt(sessionData, secretKey, salt string, maxAgeSeconds int) (string, error)
- func EncodeSessionData(userID string, secretKey string, additionalData map[string]interface{}) (string, error)
- func EncodeSessionDataWithSalt(userID string, secretKey string, salt string, ...) (string, error)
- func OptionalAuthMiddleware(config MiddlewareConfig) gin.HandlerFunc
- func UpdateSessionData(sessionData string, secretKey string, updates map[string]interface{}) (string, error)
- func UpdateSessionDataWithSalt(sessionData string, secretKey string, salt string, ...) (string, error)
- type Client
- type ClientConfig
- type DBTX
- type DjangoSigner
- func (ds *DjangoSigner) SignObject(obj map[string]interface{}, compress bool) (string, error)
- func (ds *DjangoSigner) SignTimestamp(value string) string
- func (ds *DjangoSigner) Unsign(signedValue string) (string, error)
- func (ds *DjangoSigner) UnsignObject(signedObj string, maxAge *time.Duration) (map[string]interface{}, error)
- func (ds *DjangoSigner) UnsignTimestamp(signedValue string, maxAge *time.Duration) (string, error)
- type MiddlewareConfig
- type RawSession
Constants ¶
This section is empty.
Variables ¶
var ( // ErrSessionNotFound is returned when session is not found in database ErrSessionNotFound = errors.New("session not found") // ErrSessionExpired is returned when session has expired ErrSessionExpired = errors.New("session expired") // ErrInvalidSignature is returned when session signature is invalid ErrInvalidSignature = errors.New("invalid session signature") // ErrUserNotFound is returned when user is not found in database ErrUserNotFound = errors.New("user not found") )
Functions ¶
func AuthMiddleware ¶
func AuthMiddleware(config MiddlewareConfig) gin.HandlerFunc
AuthMiddleware creates a Gin middleware that validates Django sessions It only checks if session exists and is not expired, WITHOUT decoding the payload Redirects to login page if session is invalid or missing.
func DecodeSessionData ¶
DecodeSessionData decodes Django session data and returns the user ID Uses the default salt for Django sessions: "django.contrib.sessions.SessionStore"
func DecodeSessionDataWithMaxAge ¶
DecodeSessionDataWithMaxAge decodes Django session data with timestamp validation
func DecodeSessionDataWithSalt ¶
func DecodeSessionDataWithSalt(sessionData, secretKey, salt string, maxAgeSeconds int) (string, error)
DecodeSessionDataWithSalt decodes Django session data with custom salt and timestamp validation
func EncodeSessionData ¶
func EncodeSessionData(userID string, secretKey string, additionalData map[string]interface{}) (string, error)
EncodeSessionData creates a new Django session with the given user ID and additional data
func EncodeSessionDataWithSalt ¶
func EncodeSessionDataWithSalt(userID string, secretKey string, salt string, additionalData map[string]interface{}, compress bool) (string, error)
EncodeSessionDataWithSalt creates a new Django session with custom salt
func OptionalAuthMiddleware ¶ added in v1.1.0
func OptionalAuthMiddleware(config MiddlewareConfig) gin.HandlerFunc
OptionalAuthMiddleware creates a Gin middleware that validates Django sessions but does NOT redirect when session is missing or invalid. If session exists and is valid, it will be stored in context. If session is missing or invalid, the request continues without setting session in context.
Types ¶
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client provides methods to interact with Django sessions
func NewClient ¶
func NewClient(config ClientConfig) (*Client, error)
NewClient creates a new Django session client
func (*Client) DecodeSessionUserID ¶
DecodeSessionUserID decodes the session payload and extracts user ID Use this when you have a RawSession and need to get the user ID
func (*Client) GetRawSession ¶
GetRawSession retrieves and validates a Django session by session key WITHOUT decoding the payload. This is fast and used by middleware.
func (*Client) SessionCookieName ¶
SessionCookieName returns the configured session cookie name
type ClientConfig ¶
type ClientConfig struct {
DB DBTX
SecretKey string
SessionCookieName string
MaxAge time.Duration // Optional: max age for session validation
}
ClientConfig holds configuration for the Django session client
type DBTX ¶ added in v1.0.0
type DBTX interface {
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
QueryRow(context.Context, string, ...interface{}) pgx.Row
CopyFrom(ctx context.Context, tableName pgx.Identifier, columnNames []string, rowSrc pgx.CopyFromSource) (int64, error)
}
DBTX is an interface compatible with *pgx.Conn, *pgxpool.Pool and the sqlc generated interfaces.
type DjangoSigner ¶
DjangoSigner handles Django's cryptographic signing
func NewDjangoSigner ¶
func NewDjangoSigner(secretKey string) *DjangoSigner
NewDjangoSigner creates a new signer with default values matching Django's TimestampSigner
func (*DjangoSigner) SignObject ¶
func (ds *DjangoSigner) SignObject(obj map[string]interface{}, compress bool) (string, error)
SignObject encodes and signs a map as JSON with timestamp and optional compression
func (*DjangoSigner) SignTimestamp ¶
func (ds *DjangoSigner) SignTimestamp(value string) string
SignTimestamp signs a value with a timestamp
func (*DjangoSigner) Unsign ¶
func (ds *DjangoSigner) Unsign(signedValue string) (string, error)
Unsign verifies and extracts the original value from a signed string
func (*DjangoSigner) UnsignObject ¶
func (ds *DjangoSigner) UnsignObject(signedObj string, maxAge *time.Duration) (map[string]interface{}, error)
UnsignObject decodes a signed object (JSON)
func (*DjangoSigner) UnsignTimestamp ¶
UnsignTimestamp verifies and extracts value from a timestamped signed string
type MiddlewareConfig ¶
type MiddlewareConfig struct {
Client *Client
LoginRedirectURL string // URL to redirect when auth fails (default: "/account/login")
SessionKey string // Context key for storing session (default: "django_session")
OnError func(c *gin.Context, err error) // Optional: custom error handler
}
MiddlewareConfig configures the authentication middleware