Documentation
¶
Overview ¶
Package middleware provides HTTP middleware for the LacyLights server.
Index ¶
- func GetDeviceFromContext(ctx context.Context) *models.Device
- func GetDevicePermissionsFromContext(ctx context.Context) string
- func GetSessionFromContext(ctx context.Context) *session.CachedSession
- func GetUserEmailFromContext(ctx context.Context) string
- func GetUserGroupIDs(ctx context.Context) []string
- func GetUserIDFromContext(ctx context.Context) string
- func GetUserRoleFromContext(ctx context.Context) string
- func IsAdmin(ctx context.Context) bool
- func IsAuthenticated(ctx context.Context) bool
- func IsDeviceAuthenticated(ctx context.Context) bool
- func IsGroupAdmin(ctx context.Context, groupID string) bool
- func IsGroupMember(ctx context.Context, groupID string) bool
- type AuthMiddleware
- type ContextKey
- type UserGroupMembership
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetDeviceFromContext ¶
GetDeviceFromContext retrieves the device from the request context.
func GetDevicePermissionsFromContext ¶
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 ¶
GetUserEmailFromContext retrieves the user email from the request context.
func GetUserGroupIDs ¶
GetUserGroupIDs returns the group IDs the user belongs to.
func GetUserIDFromContext ¶
GetUserIDFromContext retrieves the user ID from the request context.
func GetUserRoleFromContext ¶
GetUserRoleFromContext retrieves the user role from the request context.
func IsAuthenticated ¶
IsAuthenticated checks if the request is authenticated (either by session or device).
func IsDeviceAuthenticated ¶
IsDeviceAuthenticated checks if the request is authenticated by a device.
func IsGroupAdmin ¶
IsGroupAdmin checks if the user is a GROUP_ADMIN 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 ¶
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.