middleware

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Feb 28, 2026 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ContextKeyUserID   contextKey = "user_id"
	ContextKeyUserRole contextKey = "user_role"
	ContextKeyOrgID    contextKey = "org_id"
	ContextKeyAPIKey   contextKey = "api_key"
	ContextKeyScopes   contextKey = "api_key_scopes"
)
View Source
const (
	SessionCookieName = "overwatch_session"
)

Variables

This section is empty.

Functions

func CORS

func CORS(next http.Handler) http.Handler

CORS adds CORS headers to the response

func ClearSessionCookie added in v0.0.5

func ClearSessionCookie(w http.ResponseWriter)

ClearSessionCookie clears the session cookie

func GetAllowedOrigins added in v0.0.5

func GetAllowedOrigins() []string

GetAllowedOrigins returns the list of allowed origins from environment

func HasScope added in v0.0.18

func HasScope(scopes []string, required string) bool

HasScope checks whether the given scope (or "admin") is present in the list.

func IsOriginAllowed added in v0.0.5

func IsOriginAllowed(origin string) bool

IsOriginAllowed checks if an origin is in the allowed list. Returns false when ALLOWED_ORIGINS is not configured (deny by default).

func MaxBodySize added in v0.0.18

func MaxBodySize(maxBytes int64) func(http.Handler) http.Handler

MaxBodySize limits the request body to the given number of bytes.

func OrgIDFromContext added in v0.0.18

func OrgIDFromContext(ctx context.Context) string

OrgIDFromContext extracts the organization ID from the request context.

func RequireScope added in v0.0.18

func RequireScope(scope string) func(http.Handler) http.Handler

RequireScope returns middleware that ensures the authenticated API key possesses the given scope (or the "admin" scope, which implies all scopes).

func ScopesFromContext added in v0.0.18

func ScopesFromContext(ctx context.Context) []string

ScopesFromContext extracts the API key scopes from the request context.

func SetSessionCookie added in v0.0.5

func SetSessionCookie(w http.ResponseWriter, token string)

SetSessionCookie sets the session cookie on the response

func UserIDFromContext added in v0.0.18

func UserIDFromContext(ctx context.Context) string

UserIDFromContext extracts the authenticated user ID from the request context.

func UserRoleFromContext added in v0.0.18

func UserRoleFromContext(ctx context.Context) string

UserRoleFromContext extracts the authenticated user role from the request context.

Types

type APIKeyMiddleware added in v0.0.18

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

APIKeyMiddleware handles API key authentication and scope enforcement.

func NewAPIKeyMiddleware added in v0.0.18

func NewAPIKeyMiddleware(db *sql.DB) *APIKeyMiddleware

NewAPIKeyMiddleware creates a new APIKeyMiddleware with the given database connection.

func (*APIKeyMiddleware) Authenticate added in v0.0.18

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

Authenticate is HTTP middleware that validates API keys from the X-API-Key header or from Bearer tokens. Keys must carry the c4_live_ or c4_test_ prefix.

type SessionAuth added in v0.0.5

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

SessionAuth handles session-based authentication for the web UI. Sessions are persisted in SQLite so they survive server restarts.

func NewSessionAuth added in v0.0.5

func NewSessionAuth(db *sql.DB) *SessionAuth

NewSessionAuth creates a new session auth handler backed by the given database.

func (*SessionAuth) ClearPasskeySetup added in v0.0.18

func (s *SessionAuth) ClearPasskeySetup(token string)

ClearPasskeySetup removes the needsPasskeySetup flag from an existing session.

func (*SessionAuth) CreateSessionForUser added in v0.0.18

func (s *SessionAuth) CreateSessionForUser(userID, role string, needsPasskey bool, orgID string) (string, error)

CreateSessionForUser creates a new session with user identity and returns the session token. Set needsPasskey to true for users that still need to register a passkey (bootstrap admin, invite flow). orgID is stored in the session for use by admin handlers to scope operations.

func (*SessionAuth) DestroySession added in v0.0.5

func (s *SessionAuth) DestroySession(token string)

DestroySession removes a session.

func (*SessionAuth) IsPasskeySetup added in v0.0.18

func (s *SessionAuth) IsPasskeySetup(token string) bool

IsPasskeySetup returns true if the session has the needsPasskeySetup flag set.

func (*SessionAuth) RequireSession added in v0.0.5

func (s *SessionAuth) RequireSession(next http.Handler) http.Handler

RequireSession is middleware that checks for a valid session cookie and injects user identity into the request context. If the session has needsPasskeySetup=true, non-passkey paths redirect to /setup-passkey.

func (*SessionAuth) ValidateSession added in v0.0.5

func (s *SessionAuth) ValidateSession(token string) bool

ValidateSession checks if the session token is valid and not expired.

Jump to

Keyboard shortcuts

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