middleware

package
v0.0.0-...-f68b460 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2025 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddUserToContext

func AddUserToContext(ctx context.Context, userID int, isAdmin bool, isAPIClient bool) context.Context

AddUserToContext adds user information to the request context

func GenerateJWT

func GenerateJWT(userID int, isAdmin bool) (string, error)

GenerateJWT creates a new JWT token for a user with a short expiration time (15 minutes)

func GenerateRefreshToken

func GenerateRefreshToken() (string, error)

GenerateRefreshToken creates a new refresh token for a user

func GenerateToken

func GenerateToken(userID int, isAdmin bool, expiration time.Duration) (string, error)

GenerateToken generates a JWT token for the given user ID

func GetUserID

func GetUserID(ctx context.Context) (int, bool)

GetUserID retrieves the user ID from the context

func InitAuth

func InitAuth(secret []byte, db *db.DB)

InitAuth initializes the authentication middleware

func IsAPIClient

func IsAPIClient(ctx context.Context) bool

IsAPIClient checks if the request is from an API client

func IsAPIClientKey

func IsAPIClientKey() contextKey

IsAPIClientKey returns the context key for API client status

func IsAdmin

func IsAdmin(ctx context.Context) bool

IsAdmin checks if the user is an admin

func IsAdminKey

func IsAdminKey() contextKey

IsAdminKey returns the context key for admin status

func IsOriginAllowed

func IsOriginAllowed(corsType CORSType, origin string) bool

IsOriginAllowed checks if the given origin is allowed for the specified CORS type

func RefreshJWT

func RefreshJWT(refreshToken string) (string, int, bool, error)

RefreshJWT refreshes a JWT token using a valid refresh token

func UserIDKey

func UserIDKey() contextKey

UserIDKey returns the context key for user ID

func WithAuth

func WithAuth(authType AuthType, next http.HandlerFunc) http.HandlerFunc

WithAuth middleware checks for authentication based on the specified type

func WithCORS

func WithCORS(next http.HandlerFunc) http.HandlerFunc

WithCORS adds CORS headers to responses

func WithCORSType

func WithCORSType(corsType CORSType, next http.HandlerFunc) http.HandlerFunc

WithCORSType adds CORS headers to responses with specific CORS configuration

func WithLogging

func WithLogging(next http.HandlerFunc) http.HandlerFunc

WithLogging adds request logging

Types

type AuthType

type AuthType int

AuthType represents the type of authentication

const (
	// AuthTypeNone means no authentication required
	AuthTypeNone AuthType = iota
	// AuthTypeFrontend means frontend authentication required (JWT)
	AuthTypeFrontend
	// AuthTypeAPI means API key authentication required
	AuthTypeAPI
	// AuthTypeAny means either frontend or API authentication is acceptable
	AuthTypeAny
)

type CORSType

type CORSType int

CORSType represents the type of CORS configuration to apply

const (
	// CORSTypeOpen allows requests from any origin
	CORSTypeOpen CORSType = iota
	// CORSTypeLocal restricts requests to localhost only
	CORSTypeLocal
)

type Claims

type Claims struct {
	UserID  int  `json:"user_id"`
	IsAdmin bool `json:"is_admin"`
	jwt.RegisteredClaims
}

Claims represents the JWT claims

Jump to

Keyboard shortcuts

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