accesspolicy

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package accesspolicy merges per-team session rules into one effective set and answers the questions the API middleware asks on every request.

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidEntry = errors.New("accesspolicy: invalid allowlist entry")

ErrInvalidEntry is returned for an allowlist entry that is neither an IP nor a CIDR.

Functions

func ClientAddr

func ClientAddr(peer netip.Addr, forwardedFor string, trusted func(netip.Addr) bool) netip.Addr

ClientAddr resolves the caller's address: the TCP peer, or when the peer is a trusted proxy, the rightmost X-Forwarded-For hop that is not one.

func ParseAllowlist

func ParseAllowlist(entries []string) ([]netip.Prefix, error)

ParseAllowlist accepts IPv4 or IPv6 addresses and CIDR prefixes.

Types

type Policy

type Policy struct {
	IdleTimeout time.Duration
	MaxLifetime time.Duration
	IPAllowlist []string
	RequireMFA  bool
	// MaxSessions is the per-user cap on concurrent full sessions; 0 means none.
	MaxSessions int
	// RejectOnLimit refuses new sessions over the cap instead of evicting the oldest.
	RejectOnLimit bool
}

Policy is one team's rules as stored.

type Rules

type Rules struct {
	IdleTimeout time.Duration
	MaxLifetime time.Duration
	// AllowLists holds one list per team that set one; an address must be in every list.
	AllowLists [][]netip.Prefix
	RequireMFA bool
	// MaxSessions is the smallest cap any team sets; 0 means no cap.
	MaxSessions int
	// RejectOnLimit is true when any team that sets a cap chooses reject.
	RejectOnLimit bool
}

Rules is the strictest combination of every policy that applies to a user.

func Merge

func Merge(policies []Policy) (Rules, error)

Merge combines policies: the shortest non-zero timeouts, MFA if any team requires it, the smallest session cap, and every non-empty allowlist. A bad entry fails the merge.

func (Rules) IPAllowed

func (r Rules) IPAllowed(addr netip.Addr) bool

IPAllowed reports whether addr satisfies every allowlist. An invalid address never passes a non-empty allowlist.

func (Rules) OverCap

func (r Rules) OverCap(rank, total int) Verdict

OverCap reports what happens to the session at rank (0 is oldest) among total live sessions.

func (Rules) SessionAge

func (r Rules) SessionAge(now, created, lastSeen time.Time, seen bool) Verdict

SessionAge checks a session's lifetime and idle time. A session never seen since sign-in counts as idle from its creation.

type Verdict

type Verdict string

Verdict names why a session is refused; empty means allowed.

const (
	Allowed            Verdict = ""
	ExpiredIdle        Verdict = "session_idle_timeout"
	ExpiredLifetime    Verdict = "session_max_lifetime"
	AddressNotAllowed  Verdict = "ip_not_allowed"
	MFAEnrollmentNeeds Verdict = "mfa_enrollment_required"
	SessionEvicted     Verdict = "session_evicted"
	SessionLimit       Verdict = "session_limit"
)

Refusal reasons, also used as API error codes.

Jump to

Keyboard shortcuts

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