middleware

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package middleware provides HTTP middleware for the LacyLights server.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetDeviceFromContext

func GetDeviceFromContext(ctx context.Context) *models.Device

GetDeviceFromContext retrieves the device from the request context.

func GetDevicePermissionsFromContext

func GetDevicePermissionsFromContext(ctx context.Context) string

GetDevicePermissionsFromContext retrieves the device permissions from the request context.

func GetSessionFromContext

func GetSessionFromContext(ctx context.Context) *session.CachedSession

GetSessionFromContext retrieves the session from the request context.

func GetUserEmailFromContext

func GetUserEmailFromContext(ctx context.Context) string

GetUserEmailFromContext retrieves the user email from the request context.

func GetUserGroupIDs

func GetUserGroupIDs(ctx context.Context) []string

GetUserGroupIDs returns the group IDs the user belongs to.

func GetUserIDFromContext

func GetUserIDFromContext(ctx context.Context) string

GetUserIDFromContext retrieves the user ID from the request context.

func GetUserRoleFromContext

func GetUserRoleFromContext(ctx context.Context) string

GetUserRoleFromContext retrieves the user role from the request context.

func IsAdmin

func IsAdmin(ctx context.Context) bool

IsAdmin checks if the authenticated user/device is an admin.

func IsAuthenticated

func IsAuthenticated(ctx context.Context) bool

IsAuthenticated checks if the request is authenticated (either by session or device).

func IsDeviceAuthenticated

func IsDeviceAuthenticated(ctx context.Context) bool

IsDeviceAuthenticated checks if the request is authenticated by a device.

func IsGroupAdmin

func IsGroupAdmin(ctx context.Context, groupID string) bool

IsGroupAdmin checks if the user is a GROUP_ADMIN of the given group.

func IsGroupMember

func IsGroupMember(ctx context.Context, groupID string) bool

IsGroupMember checks if the user is a member of the given group.

Types

type AuthMiddleware

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

AuthMiddleware provides authentication middleware.

func NewAuthMiddleware

func NewAuthMiddleware(authService *auth.Service) *AuthMiddleware

NewAuthMiddleware creates a new auth middleware instance.

func NewAuthMiddlewareWithDB

func NewAuthMiddlewareWithDB(authService *auth.Service, db *gorm.DB) *AuthMiddleware

NewAuthMiddlewareWithDB creates a new auth middleware instance with database access.

func (*AuthMiddleware) Authenticate

func (m *AuthMiddleware) Authenticate(next http.Handler) http.Handler

Authenticate is middleware that extracts and validates authentication. If auth is disabled, it passes through without checking. If auth is enabled:

  • First checks for Bearer token (JWT) authentication
  • Then checks for device fingerprint (X-Device-Fingerprint header)
  • When both are present, JWT provides user identity and device context is also set
  • If only device is approved, uses device identity as fallback
  • If no valid auth, passes through without context (resolvers check)

func (*AuthMiddleware) RequireAdmin

func (m *AuthMiddleware) RequireAdmin(next http.Handler) http.Handler

RequireAdmin is middleware that requires admin role. Returns 401 if not authenticated, 403 if not admin.

func (*AuthMiddleware) RequireAuth

func (m *AuthMiddleware) RequireAuth(next http.Handler) http.Handler

RequireAuth is middleware that requires a valid authentication. Returns 401 if not authenticated.

type ContextKey

type ContextKey string

ContextKey is a type for context keys used by auth middleware. It is defined as its own type to reduce the risk of collisions with context keys from other packages.

const (
	// ContextKeySession is the context key for the authenticated session.
	ContextKeySession ContextKey = "lacylights:auth:session"
	// ContextKeyUserID is the context key for the authenticated user's ID.
	ContextKeyUserID ContextKey = "lacylights:auth:userID"
	// ContextKeyUserEmail is the context key for the authenticated user's email.
	ContextKeyUserEmail ContextKey = "lacylights:auth:userEmail"
	// ContextKeyUserRole is the context key for the authenticated user's role.
	ContextKeyUserRole ContextKey = "lacylights:auth:userRole"
	// ContextKeyDevice is the context key for the authenticated device.
	ContextKeyDevice ContextKey = "lacylights:auth:device"
	// ContextKeyDevicePermissions is the context key for the device's permissions.
	ContextKeyDevicePermissions ContextKey = "lacylights:auth:devicePermissions"
	// ContextKeyUserGroups is the context key for the user's group memberships.
	ContextKeyUserGroups ContextKey = "lacylights:auth:userGroups"
)

type UserGroupMembership

type UserGroupMembership struct {
	GroupID string
	Role    string
}

UserGroupMembership represents a user's or device's membership in a group.

func GetUserGroupsFromContext

func GetUserGroupsFromContext(ctx context.Context) []UserGroupMembership

GetUserGroupsFromContext retrieves the user's group memberships from the context.

Jump to

Keyboard shortcuts

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