persona

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 22, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package persona provides persona-based access control and customization.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func PersonaMiddleware

func PersonaMiddleware(mapper RoleMapper) middleware.Middleware

PersonaMiddleware creates middleware that sets persona in context.

Types

type ChainedRoleMapper

type ChainedRoleMapper struct {
	Mappers []RoleMapper
}

ChainedRoleMapper tries multiple mappers in order.

func (*ChainedRoleMapper) MapToPersona

func (c *ChainedRoleMapper) MapToPersona(ctx context.Context, roles []string) (*Persona, error)

MapToPersona uses the first mapper that returns a persona.

func (*ChainedRoleMapper) MapToRoles

func (c *ChainedRoleMapper) MapToRoles(claims map[string]any) ([]string, error)

MapToRoles aggregates roles from all mappers.

type OIDCRoleMapper

type OIDCRoleMapper struct {
	// ClaimPath is the dot-separated path to roles in claims.
	ClaimPath string

	// RolePrefix filters roles to those starting with this prefix.
	RolePrefix string

	// PersonaMapping maps roles to persona names.
	PersonaMapping map[string]string

	// Registry is the persona registry.
	Registry *Registry
}

OIDCRoleMapper extracts roles from OIDC token claims.

func (*OIDCRoleMapper) MapToPersona

func (m *OIDCRoleMapper) MapToPersona(_ context.Context, roles []string) (*Persona, error)

MapToPersona maps roles to a persona.

func (*OIDCRoleMapper) MapToRoles

func (m *OIDCRoleMapper) MapToRoles(claims map[string]any) ([]string, error)

MapToRoles extracts roles from OIDC claims.

type Persona

type Persona struct {
	// Name is the unique identifier for this persona.
	Name string `json:"name" yaml:"name"`

	// DisplayName is the human-readable name.
	DisplayName string `json:"display_name" yaml:"display_name"`

	// Description describes this persona.
	Description string `json:"description,omitempty" yaml:"description,omitempty"`

	// Roles are the roles that map to this persona.
	Roles []string `json:"roles" yaml:"roles"`

	// Tools defines tool access rules.
	Tools ToolRules `json:"tools" yaml:"tools"`

	// Prompts defines prompt customizations.
	Prompts PromptConfig `json:"prompts" yaml:"prompts"`

	// Hints provides tool-specific hints for the AI.
	Hints map[string]string `json:"hints,omitempty" yaml:"hints,omitempty"`

	// Priority determines which persona takes precedence.
	// Higher values have higher priority.
	Priority int `json:"priority,omitempty" yaml:"priority,omitempty"`
}

Persona defines a user persona with associated permissions and customizations.

func AdminPersona

func AdminPersona() *Persona

AdminPersona creates an admin persona with full access.

func DefaultPersona

func DefaultPersona() *Persona

DefaultPersona creates a default persona that denies all access. This ensures fail-closed behavior - users must be explicitly granted access.

type PersonaAuthorizer

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

PersonaAuthorizer implements middleware.Authorizer using personas.

func NewPersonaAuthorizer

func NewPersonaAuthorizer(registry *Registry, mapper RoleMapper) *PersonaAuthorizer

NewPersonaAuthorizer creates a new persona-based authorizer.

func (*PersonaAuthorizer) IsAuthorized

func (a *PersonaAuthorizer) IsAuthorized(ctx context.Context, userID string, roles []string, toolName string) (bool, string)

IsAuthorized checks if the user is authorized for the tool.

type PersonaConfig

type PersonaConfig struct {
	DisplayName string            `yaml:"display_name"`
	Description string            `yaml:"description,omitempty"`
	Roles       []string          `yaml:"roles"`
	Tools       ToolRulesConfig   `yaml:"tools"`
	Prompts     PromptConfigYAML  `yaml:"prompts"`
	Hints       map[string]string `yaml:"hints,omitempty"`
	Priority    int               `yaml:"priority,omitempty"`
}

PersonaConfig is the configuration format for personas.

type PromptConfig

type PromptConfig struct {
	// SystemPrefix is prepended to system prompts.
	SystemPrefix string `json:"system_prefix,omitempty" yaml:"system_prefix,omitempty"`

	// SystemSuffix is appended to system prompts.
	SystemSuffix string `json:"system_suffix,omitempty" yaml:"system_suffix,omitempty"`

	// Instructions are additional instructions for this persona.
	Instructions string `json:"instructions,omitempty" yaml:"instructions,omitempty"`
}

PromptConfig defines prompt customizations for a persona.

type PromptConfigYAML

type PromptConfigYAML struct {
	SystemPrefix string `yaml:"system_prefix,omitempty"`
	SystemSuffix string `yaml:"system_suffix,omitempty"`
	Instructions string `yaml:"instructions,omitempty"`
}

PromptConfigYAML is the YAML configuration for prompts.

type Registry

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

Registry manages persona definitions.

func NewRegistry

func NewRegistry() *Registry

NewRegistry creates a new persona registry.

func (*Registry) All

func (r *Registry) All() []*Persona

All returns all registered personas.

func (*Registry) Get

func (r *Registry) Get(name string) (*Persona, bool)

Get retrieves a persona by name.

func (*Registry) GetDefault

func (r *Registry) GetDefault() (*Persona, bool)

GetDefault returns the default persona.

func (*Registry) GetForRoles

func (r *Registry) GetForRoles(roles []string) (*Persona, bool)

GetForRoles returns the best matching persona for the given roles.

func (*Registry) LoadFromConfig

func (r *Registry) LoadFromConfig(config map[string]*PersonaConfig) error

LoadFromConfig loads personas from a configuration map.

func (*Registry) Register

func (r *Registry) Register(p *Persona) error

Register adds a persona to the registry.

func (*Registry) SetDefault

func (r *Registry) SetDefault(name string)

SetDefault sets the default persona name.

type RoleMapper

type RoleMapper interface {
	// MapToRoles extracts roles from claims.
	MapToRoles(claims map[string]any) ([]string, error)

	// MapToPersona maps roles to a persona.
	MapToPersona(ctx context.Context, roles []string) (*Persona, error)
}

RoleMapper maps identity claims to platform roles and personas.

type StaticRoleMapper

type StaticRoleMapper struct {
	// UserPersonas maps user IDs/emails to persona names.
	UserPersonas map[string]string

	// GroupPersonas maps groups to persona names.
	GroupPersonas map[string]string

	// DefaultPersonaName is the fallback persona.
	DefaultPersonaName string

	// Registry is the persona registry.
	Registry *Registry
}

StaticRoleMapper uses static configuration for mapping.

func (*StaticRoleMapper) MapToPersona

func (m *StaticRoleMapper) MapToPersona(ctx context.Context, _ []string) (*Persona, error)

MapToPersona maps based on static configuration.

func (*StaticRoleMapper) MapToRoles

func (m *StaticRoleMapper) MapToRoles(_ map[string]any) ([]string, error)

MapToRoles returns static roles (not used for static mapping).

type ToolFilter

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

ToolFilter filters tools based on persona rules.

func NewToolFilter

func NewToolFilter(registry *Registry) *ToolFilter

NewToolFilter creates a new tool filter.

func (*ToolFilter) FilterTools

func (f *ToolFilter) FilterTools(persona *Persona, tools []string) []string

FilterTools filters a list of tools based on persona rules.

func (*ToolFilter) IsAllowed

func (f *ToolFilter) IsAllowed(persona *Persona, toolName string) bool

IsAllowed checks if a tool is allowed for a persona.

type ToolRules

type ToolRules struct {
	// Allow patterns for allowed tools (supports wildcards like "trino_*").
	Allow []string `json:"allow" yaml:"allow"`

	// Deny patterns for denied tools (takes precedence over Allow).
	Deny []string `json:"deny" yaml:"deny"`
}

ToolRules defines tool access rules for a persona.

type ToolRulesConfig

type ToolRulesConfig struct {
	Allow []string `yaml:"allow"`
	Deny  []string `yaml:"deny"`
}

ToolRulesConfig is the YAML configuration for tool rules.

Jump to

Keyboard shortcuts

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