Documentation
¶
Overview ¶
Package identity provides tenant-scoped identity context for multi-tenant services.
Every authenticated request carries an identity Context describing who is calling, which tenant and workspace they belong to, and what roles they hold. Use the context helpers to propagate and retrieve this information through context.Context.
ctx = identity.WithContext(ctx, id) id, ok := identity.FromContext(ctx)
Index ¶
- func WithContext(ctx context.Context, id Context) context.Context
- type Context
- func (c Context) HasAnyRole(roles ...string) bool
- func (c Context) HasRole(role string) bool
- func (c Context) IsService() bool
- func (c Context) IsUser() bool
- func (c Context) Issuer() string
- func (c Context) LogValue() map[string]any
- func (c Context) PrincipalID() string
- func (c Context) PrincipalType() PrincipalType
- func (c Context) Roles() []string
- func (c Context) String() string
- func (c Context) TenantID() string
- func (c Context) WorkspaceID() string
- type PrincipalType
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context carries the authenticated caller's identity within a tenant.
All fields are read-only after construction; create instances via New.
func FromContext ¶
FromContext extracts the identity from ctx. Returns the zero Context and false if none is present.
func MustFromContext ¶
MustFromContext extracts the identity from ctx, panicking if absent. Use only in code paths where middleware guarantees the identity exists.
func New ¶
func New(tenantID, workspaceID, principalID string, principalType PrincipalType, issuer string, roles []string) (Context, error)
New creates a validated identity Context. Returns an error if any required field is empty or principalType is not a known value.
func (Context) HasAnyRole ¶
HasAnyRole reports whether the identity holds at least one of the given roles.
func (Context) LogValue ¶
LogValue implements slog.LogValuer so the identity renders cleanly in structured log output.
func (Context) PrincipalID ¶
func (Context) PrincipalType ¶
func (c Context) PrincipalType() PrincipalType
func (Context) String ¶
String returns a human-readable representation useful for logging. Never includes secret material.
func (Context) WorkspaceID ¶
type PrincipalType ¶
type PrincipalType string
PrincipalType distinguishes between human users and service accounts.
const ( PrincipalUser PrincipalType = "user" PrincipalService PrincipalType = "service" )