webx

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	HeaderClientPlatform = "X-Client-Platform"
	HeaderClientVersion  = "X-Client-Version"
	HeaderClientOS       = "X-Client-OS"
)
View Source
const (
	CodeUnauthenticated = "E_UNAUTHENTICATED"
	CodeForbidden       = "E_FORBIDDEN"
	CodeNotFound        = "E_NOT_FOUND"
	CodeValidation      = "E_VALIDATION"
	CodeConflict        = "E_CONFLICT"
	CodeRateLimited     = "E_RATE_LIMITED"
	CodeQuotaExceeded   = "E_QUOTA_EXCEEDED"
	CodeInternal        = "E_INTERNAL"
	CodeTimeout         = "E_TIMEOUT"
	CodePayloadTooLarge = "E_PAYLOAD_TOO_LARGE"
	CodeUnavailable     = "E_UNAVAILABLE"
)
View Source
const ClientUnknown = "unknown"
View Source
const DefaultPATPrefix = "tk_"
View Source
const HeaderRequestID = "X-Request-Id"

Variables

This section is empty.

Functions

func AccessLog

func AccessLog(log *slog.Logger, trustedProxies ...*net.IPNet) func(http.Handler) http.Handler

func Authenticate

func Authenticate(cfg *AuthConfig, sessions SessionStore, pats PATLookup) func(http.Handler) http.Handler

func BodyLimit

func BodyLimit(n int64) func(http.Handler) http.Handler

func CORS

func CORS(allowedOrigins []string, allowCredentials bool) func(http.Handler) http.Handler

func CSRFConditional

func CSRFConditional(cfg *CSRFConfig, secure bool) func(http.Handler) http.Handler

func CSRFMaskedToken

func CSRFMaskedToken(r *http.Request) string

func ClientIP

func ClientIP(r *http.Request, trustedProxies ...*net.IPNet) string

func ClientMetadata

func ClientMetadata(next http.Handler) http.Handler

func ClientMetadataFrom

func ClientMetadataFrom(ctx context.Context) (platform, version, os string)

func ConfigHandler

func ConfigHandler(d ConfigDeps) http.HandlerFunc

func DecodeJSON

func DecodeJSON(w http.ResponseWriter, r *http.Request, dst any) bool

func DeriveCSRFKey

func DeriveCSRFKey(signingSecret []byte) []byte

func ErrConflict

func ErrConflict(w http.ResponseWriter, msg string)

func ErrForbidden

func ErrForbidden(w http.ResponseWriter, msg string)

func ErrInternal

func ErrInternal(w http.ResponseWriter)

func ErrNotFound

func ErrNotFound(w http.ResponseWriter, msg string)

func ErrPayloadTooLarge

func ErrPayloadTooLarge(w http.ResponseWriter, msg string)

func ErrTimeout

func ErrTimeout(w http.ResponseWriter)

func ErrUnauthenticated

func ErrUnauthenticated(w http.ResponseWriter, msg string)

func ErrUnavailable

func ErrUnavailable(w http.ResponseWriter, msg string)

func ErrValidation

func ErrValidation(w http.ResponseWriter, msg string)

func HashIP

func HashIP(ip, secret string) string

func HashToken

func HashToken(token string) string

func MintToken

func MintToken() (string, error)

func P

func ParseOrigins

func ParseOrigins(raw string) []string

func ParseTrustedProxies

func ParseTrustedProxies(raw string) []*net.IPNet

func PoolStats

func PoolStats(pool *pgxpool.Pool) (acquired, idle, total int32)

func PrincipalHolder

func PrincipalHolder() func(http.Handler) http.Handler

func RateLimit

func RateLimit(l Limiter, dimension string, perMinute int, keyFn func(r *http.Request) string) func(http.Handler) http.Handler

func Recover

func Recover(log *slog.Logger) func(http.Handler) http.Handler

func RequestID

func RequestID(r *http.Request) string

func RequestIDFromCtx

func RequestIDFromCtx(ctx context.Context) string

func RequestIDMiddleware

func RequestIDMiddleware(next http.Handler) http.Handler

func RequireAuth

func RequireAuth(next http.Handler) http.Handler

func RequireHuman

func RequireHuman(next http.HandlerFunc) http.HandlerFunc

func SecurityHeaders

func SecurityHeaders(next http.Handler) http.Handler

func Timeout

func Timeout(d time.Duration) func(http.Handler) http.Handler

func TimeoutExcept

func TimeoutExcept(d time.Duration, exemptPrefixes ...string) func(http.Handler) http.Handler

func WithPrincipal

func WithPrincipal(ctx context.Context, p *Principal) context.Context

func WriteErr

func WriteErr(w http.ResponseWriter, err error)

func WriteError

func WriteError(w http.ResponseWriter, status int, code, message string, details any)

func WriteJSON

func WriteJSON(w http.ResponseWriter, status int, v any)

Types

type AuthConfig

type AuthConfig struct {
	CookieName string

	CookieDomain string
	Secure       bool

	SessionTTL  time.Duration
	AbsoluteTTL time.Duration

	IPHashSecret string

	PATPrefix string
}

func DefaultAuthConfig

func DefaultAuthConfig() AuthConfig

func (*AuthConfig) ClearSessionCookie

func (c *AuthConfig) ClearSessionCookie(w http.ResponseWriter)

func (*AuthConfig) SetSessionCookie

func (c *AuthConfig) SetSessionCookie(w http.ResponseWriter, token string, exp time.Time)

type CSRFConfig

type CSRFConfig struct {
	Key []byte

	TrustedOrigins []string

	ExemptPrefixes []string

	PATPrefix string
}

type CheckFunc

type CheckFunc func(ctx context.Context) error

type ConfigDeps

type ConfigDeps struct {
	OAuthProviders  func() []string
	BillingChannels func() []string

	Extra func() map[string]any
}

type CredentialScope

type CredentialScope struct {
	WorkspaceIDs []string `json:"workspace_ids,omitempty"`

	Permissions []string `json:"permissions,omitempty"`
}

func (*CredentialScope) AllowsPermission

func (s *CredentialScope) AllowsPermission(perm string) bool

func (*CredentialScope) AllowsWorkspace

func (s *CredentialScope) AllowsWorkspace(workspaceID string) bool

type Error

type Error struct {
	Status  int
	Code    string
	Message string
}

func NewConflict

func NewConflict(msg string) *Error

func NewError

func NewError(status int, code, message string) *Error

func NewForbidden

func NewForbidden(msg string) *Error

func NewNotFound

func NewNotFound(msg string) *Error

func NewQuotaExceeded

func NewQuotaExceeded(msg string) *Error

func NewRateLimited

func NewRateLimited(msg string) *Error

func NewUnauthenticated

func NewUnauthenticated(msg string) *Error

func NewValidation

func NewValidation(msg string) *Error

func (*Error) Error

func (e *Error) Error() string

type ErrorBody

type ErrorBody struct {
	Code    string `json:"error"`
	Message string `json:"message"`
	Details any    `json:"details,omitempty"`
}

type FailOpenLimiter

type FailOpenLimiter struct{}

func (FailOpenLimiter) Allow

type Health

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

func NewHealth

func NewHealth() *Health

func (*Health) AddCheck

func (h *Health) AddCheck(name string, fn CheckFunc) *Health

func (*Health) Liveness

func (h *Health) Liveness() http.HandlerFunc

func (*Health) Readiness

func (h *Health) Readiness() http.HandlerFunc

type Limiter

type Limiter interface {
	Allow(ctx context.Context, key string, perMinute int) (ok bool, retryAfter time.Duration)
}

type PATLookup

type PATLookup interface {
	ResolvePAT(ctx context.Context, token string, now time.Time) (*Principal, error)
}

type Principal

type Principal struct {
	UserID      string
	Email       string
	Name        string
	Source      Source
	SessionID   string
	PATID       string
	WorkspaceID string
	Role        string

	Project *ProjectScope

	Scope *CredentialScope

	AgentID string

	IsPlatformAdmin bool

	ImpersonatedBy string
}

func PrincipalFrom

func PrincipalFrom(ctx context.Context) *Principal

func PrincipalFromRequest

func PrincipalFromRequest(r *http.Request) *Principal

type PrincipalCell

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

func (*PrincipalCell) Get

func (c *PrincipalCell) Get() *Principal

func (*PrincipalCell) Set

func (c *PrincipalCell) Set(p *Principal)

type ProjectScope

type ProjectScope struct {
	ID string

	Role string
}

type SessionStore

type SessionStore interface {
	CreateSession(ctx context.Context, userID, ipHash, userAgent string, now time.Time) (token string, exp time.Time, err error)

	VerifySession(ctx context.Context, token string, now time.Time) (*Principal, error)

	RenewSession(ctx context.Context, token string, now time.Time) (exp time.Time, renewed bool, err error)

	RevokeSession(ctx context.Context, token string) error
}

type Source

type Source string
const (
	SourceSession Source = "session"
	SourcePAT     Source = "pat"
	SourceSystem  Source = "system"
)

Jump to

Keyboard shortcuts

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