context

package
v0.0.0-...-1b2c9a0 Latest Latest
Warning

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

Go to latest
Published: Sep 19, 2026 License: LGPL-2.1 Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// PrincipalKindUser is a person acting through a client.
	PrincipalKindUser = PrincipalKind("user")
	// PrincipalKindService is a workload acting on the system's behalf: a scheduled job, a
	// message consumer, a device. It authorizes exactly like a user, on entitlements it was
	// granted - never by virtue of being internal.
	PrincipalKindService = PrincipalKind("service")
	// PrincipalKindSystem is reserved for future platform-level operations. It deliberately
	// carries NO bypass behaviour; treat it as unauthorized until that is designed.
	PrincipalKindSystem = PrincipalKind("system")
)

Variables

View Source
var CtxKeyDomainConstraints = contextKey{"domain_constraints"}

Functions

This section is empty.

Types

type Context

type Context interface {
	context.Context

	InnerContext() context.Context
	GetLogger() logging.LoggerService
	SetLogger(logger logging.LoggerService)
	GetDbTranx() db.DbTransaction
	SetDbTranx(trx db.DbTransaction)
	GetDomainConstraints() dmodel.DynamicFields
	SetDomainConstraints(constraints dmodel.DynamicFields)
	GetModuleName() string
	GetPermissions() ContextPermissions
	SetPermissions(permissions ContextPermissions)
	GetUser() dmodel.DynamicFields
	SetUser(user dmodel.DynamicFields)
	// Replace current inner context with a new one that has the given key and value.
	WithValue(key, val any)
}

func AsRequestContext

func AsRequestContext(echoCtx *echo.Context) (Context, error)

Returns pointer to an instance of RequestContext if it exists, otherwise returns an error.

func CloneRequestContext

func CloneRequestContext(ctx Context) Context

CloneRequestContext returns a copy of ctx that can be given a different transaction without disturbing the original. Use it to derive a scoped context from a live request; the New* constructors above build an empty one, for code with no caller behind it.

It copies every field the copy might be used with, the caller's identity above all: basemodel stamps created_by/updated_by from GetPermissions().UserId, so a clone that dropped permissions would write those columns null and report nothing. Domain constraints live in the inner context's values, so copying Context carries them along.

A field added to RequestContext must be added here too.

func CloneWithInner

func CloneWithInner(ctx Context, inner context.Context) Context

CloneWithInner is CloneRequestContext with the inner context replaced, for a caller that has derived a cancellation or deadline from the request's own context and needs the identity, logger and transaction to travel with it. Passing the bare inner context instead would drop all of those; mutating the original would impose the deadline on the caller's remaining work.

func NewRequestContext

func NewRequestContext(ctx context.Context) Context

func NewRequestContextF

func NewRequestContextF(ctx context.Context, moduleName string, domainConstraints dmodel.DynamicFields) Context

NewRequestContextF builds an empty context carrying domain constraints.

The constraints go through SetDomainConstraints rather than a struct field: GetDomainConstraints reads them back off the inner context's values, so a field set here would never be read.

func NewRequestContextM

func NewRequestContextM(ctx context.Context, moduleName string) Context

type ContextPermissions

type ContextPermissions struct {
	IsOwner      bool
	Entitlements ds.Set[string]
	UserId       model.Id `json:"user_id"`
	// The actor executing this request: a user, or a service acting for the system.
	Principal Principal `json:"principal"`
	// The orgs that user belongs to (if any)
	UserOrgIds ds.Set[model.Id] `json:"user_org_ids"`
	// The org unit that user belongs to (if any)
	OrgUnitId *model.Id `json:"org_unit_id"`
	// The org that the org unit belongs to (if user belongs to an org unit)
	OrgUnitOrgId *model.Id `json:"org_unit_org_id"`
}

type Principal

type Principal struct {
	Kind PrincipalKind `json:"kind"`
	Id   model.Id      `json:"id"`
	// OrgId is the single org a service principal acts within. A service holds no org
	// membership, so this is what org-scoped work is checked against; nil for a user, whose
	// reach comes from UserOrgIds instead.
	OrgId *model.Id `json:"org_id"`
	// DisplayName is for audit and logs only. Never make a security decision from it.
	DisplayName string `json:"display_name"`
}

Principal is the authenticated actor behind the current execution.

A zero Principal means "nobody was authenticated". Authorization must fail closed on it rather than reading absence as permission - see requestguard.AssertPermission.

func (Principal) IsZero

func (this Principal) IsZero() bool

IsZero reports whether no principal was established.

type PrincipalKind

type PrincipalKind string

PrincipalKind says what sort of actor is executing, so that a job or a consumer is distinguishable from an unauthenticated caller rather than looking identical to one.

This is not IAM's PrincipalType (`nikkiuser` | `custom`), which describes where a login credential came from - a different axis, and in a module core cannot import.

type RequestContext

type RequestContext struct {
	context.Context
	// contains filtered or unexported fields
}

func (RequestContext) GetDbTranx

func (this RequestContext) GetDbTranx() db.DbTransaction

func (RequestContext) GetDomainConstraints

func (this RequestContext) GetDomainConstraints() dmodel.DynamicFields

func (RequestContext) GetLogger

func (this RequestContext) GetLogger() logging.LoggerService

func (RequestContext) GetModuleName

func (this RequestContext) GetModuleName() string

func (RequestContext) GetPermissions

func (this RequestContext) GetPermissions() ContextPermissions

func (RequestContext) GetUser

func (this RequestContext) GetUser() dmodel.DynamicFields

func (RequestContext) InnerContext

func (this RequestContext) InnerContext() context.Context

func (*RequestContext) SetDbTranx

func (this *RequestContext) SetDbTranx(trx db.DbTransaction)

func (*RequestContext) SetDomainConstraints

func (this *RequestContext) SetDomainConstraints(constraints dmodel.DynamicFields)

func (*RequestContext) SetLogger

func (this *RequestContext) SetLogger(logger logging.LoggerService)

func (*RequestContext) SetPermissions

func (this *RequestContext) SetPermissions(permissions ContextPermissions)

func (*RequestContext) SetUser

func (this *RequestContext) SetUser(user dmodel.DynamicFields)

func (*RequestContext) Value

func (this *RequestContext) Value(key any) any

func (*RequestContext) WithValue

func (this *RequestContext) WithValue(key, val any)

Replace current inner context with a new one that has the given key and value.

Jump to

Keyboard shortcuts

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