authz

package
v0.1.1 Latest Latest
Warning

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

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

Documentation

Overview

Package authz models the authenticated caller (a Principal) and the authorization decisions made about it. It is deliberately free of imports on the auth/agent packages so those packages can depend on it without a cycle; the concrete authentication wiring is injected (see Authenticator).

Index

Constants

View Source
const AgentTokenPrefix = "legant_at_"

AgentTokenPrefix marks an opaque agent token presented as a bearer credential.

Variables

This section is empty.

Functions

func RequireAdmin

func RequireAdmin(next http.Handler) http.Handler

RequireAdmin is middleware (placed after Require) that allows only admin-capable principals: a superadmin, or an owner/admin of some org. Use it for org-scoped resources whose handlers further restrict by the caller's orgs.

func RequireSuperadmin

func RequireSuperadmin(next http.Handler) http.Handler

RequireSuperadmin is middleware (placed after Require) that allows only platform superadmins. Use it for global resources that are not org-scoped (e.g. user and client management), so an org-admin cannot reach across tenants.

func WithPrincipal

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

WithPrincipal stores the principal on the context.

Types

type AgentTokenResolver

type AgentTokenResolver func(ctx context.Context, token string) (agentID string, scopes []string, tokenID string, ok bool)

AgentTokenResolver validates an opaque agent token.

type Authenticator

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

Authenticator resolves a request to a Principal using, in order: a session cookie, an agent bearer token, or an OAuth2 access token. It loads org membership and superadmin status from the database to populate the Principal.

func NewAuthenticator

func NewAuthenticator(pool *pgxpool.Pool, session SessionResolver, agent AgentTokenResolver, bearer BearerResolver) *Authenticator

func (*Authenticator) Authenticate

func (a *Authenticator) Authenticate(r *http.Request) (*Principal, bool)

Authenticate resolves the request to a Principal, or returns ok=false.

func (*Authenticator) AuthenticateActor

func (a *Authenticator) AuthenticateActor(ctx context.Context, token string) (*Principal, error)

AuthenticateActor validates an agent token and returns the agent Principal. Used by the RFC 8693 token-exchange endpoint to authenticate the acting agent.

func (*Authenticator) Require

func (a *Authenticator) Require(next http.Handler) http.Handler

Require is middleware that rejects unauthenticated requests with 401 and attaches the Principal to the context for downstream handlers.

type BearerResolver

type BearerResolver func(ctx context.Context, token string) (userID string, scopes []string, ok bool)

BearerResolver validates an OAuth2 access token (e.g. via introspection).

type Principal

type Principal struct {
	Type         Type
	ID           string            // user id / agent id / client id
	OrgRoles     map[string]string // org_id -> role ("owner" | "admin" | "member" | "agent")
	IsSuperadmin bool
	Scopes       []string // granted scopes, for token/agent principals
	AuthMethod   string   // "session" | "bearer" | "agent_token"
}

Principal is the authenticated identity behind a request: who they are, which organizations they belong to and with what role, and how they authenticated. It replaces the previously-trusted, spoofable X-User-ID header.

func FromContext

func FromContext(ctx context.Context) (*Principal, bool)

FromContext retrieves the principal placed on the context by the authenticator.

func (*Principal) AdminCapable

func (p *Principal) AdminCapable() bool

AdminCapable reports whether the principal may reach the admin API at all — a superadmin, or an owner/admin of at least one organization.

func (*Principal) CanAccessOrg

func (p *Principal) CanAccessOrg(orgID string) bool

CanAccessOrg reports whether the principal may access resources in orgID. A superadmin may access any org. An empty orgID denotes a global (org-less) resource, which only a superadmin may touch.

func (*Principal) IsOrgAdmin

func (p *Principal) IsOrgAdmin(orgID string) bool

IsOrgAdmin reports whether the principal is an owner/admin of orgID (or a superadmin).

func (*Principal) OrgIDs

func (p *Principal) OrgIDs() []string

OrgIDs returns the organizations this principal belongs to.

func (*Principal) RoleIn

func (p *Principal) RoleIn(orgID string) (string, bool)

RoleIn returns the principal's role in an organization.

type SessionResolver

type SessionResolver func(r *http.Request) (userID string, ok bool)

SessionResolver returns the user id for a request's session cookie.

type Type

type Type string

Type distinguishes the kind of caller behind a request.

const (
	TypeUser  Type = "user"
	TypeAgent Type = "agent"
)

Jump to

Keyboard shortcuts

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