Documentation
¶
Index ¶
- Constants
- func Allowed(ctx context.Context, scope string) bool
- func CheckAPIKey(storedHash, key string) bool
- func CheckPassword(hash, password string) bool
- func ContextWithKey(ctx context.Context, k *KeyInfo) context.Context
- func ContextWithUser(ctx context.Context, user *User) context.Context
- func GenerateAPIKey() (fullKey, prefix string, err error)
- func GenerateTemporaryPassword() (string, error)
- func HasScope(ctx context.Context, scope string) bool
- func HashAPIKey(key string) (string, error)
- func HashPassword(password string) (string, error)
- func Middleware(sessionManager *scs.SessionManager, userStore *UserStore, keyStore *KeyStore) func(http.Handler) http.Handler
- func RegisterRoutes(api huma.API, sessionManager *scs.SessionManager, userStore *UserStore, ...)
- func RegisterServiceAccountRoutes(api huma.API, pool *pgxpool.Pool, keys *KeyStore)
- func ValidScope(s string) bool
- type APIKeyInfo
- type APIKeyRow
- type KeyInfo
- type KeyStore
- func (s *KeyStore) Create(ctx context.Context, userID, name, keyPrefix, keyHash string) (*APIKeyRow, error)
- func (s *KeyStore) CreateLimited(ctx context.Context, userID, name, keyPrefix, keyHash string, scopes []string, ...) (*APIKeyRow, error)
- func (s *KeyStore) Delete(ctx context.Context, id, userID string) error
- func (s *KeyStore) GetByPrefix(ctx context.Context, prefix string) (*APIKeyRow, error)
- func (s *KeyStore) ListByUser(ctx context.Context, userID string) ([]APIKeyInfo, error)
- func (s *KeyStore) UpdateLastUsed(ctx context.Context, id string)
- type RouteRule
- type ServiceAccount
- type User
- type UserRow
- type UserStore
- func (s *UserStore) Count(ctx context.Context) (int, error)
- func (s *UserStore) Create(ctx context.Context, email, name, passwordHash string) (*UserRow, error)
- func (s *UserStore) CreateWithRole(ctx context.Context, email, name, passwordHash, role string) (*UserRow, error)
- func (s *UserStore) GetByEmail(ctx context.Context, email string) (*UserRow, error)
- func (s *UserStore) GetByID(ctx context.Context, id string) (*UserRow, error)
- func (s *UserStore) List(ctx context.Context) ([]UserRow, error)
- func (s *UserStore) Search(ctx context.Context, q string, limit int) ([]UserRow, error)
- func (s *UserStore) SetResetRequired(ctx context.Context, id string, required bool) error
- func (s *UserStore) UpdatePassword(ctx context.Context, id, passwordHash string) error
- func (s *UserStore) UpdateRole(ctx context.Context, id, role string) (bool, error)
Constants ¶
const ( RoleOwner = "owner" RoleAdmin = "admin" RoleMember = "member" KindHuman = "human" KindService = "service" )
const ( ScopeSkillsRead = "skills:read" ScopeSetsRead = "sets:read" ScopeEvalWork = "eval:work" ScopeEventsWrite = "events:write" ScopeOutcomesWrite = "outcomes:write" ScopeSubmissionsWrite = "submissions:write" )
The scopes a key may hold.
Variables ¶
This section is empty.
Functions ¶
func Allowed ¶ added in v0.16.0
Allowed reports whether a tool or handler that needs scope may run for the request: an unrestricted caller may, and a restricted key needs the scope.
func CheckAPIKey ¶
func CheckPassword ¶
func ContextWithKey ¶ added in v0.16.0
ContextWithKey attaches the authenticating key to ctx.
func GenerateAPIKey ¶
func GenerateTemporaryPassword ¶ added in v0.7.0
func HasScope ¶ added in v0.16.0
HasScope reports whether the request's key holds scope explicitly. An unrestricted key holds no scopes: its rights come from its owner's role.
func HashAPIKey ¶
func HashPassword ¶
func Middleware ¶
func Middleware(sessionManager *scs.SessionManager, userStore *UserStore, keyStore *KeyStore) func(http.Handler) http.Handler
Middleware returns a chi-compatible middleware that enforces authentication on /mcp and all /api/ routes except explicitly exempt paths. It checks two auth methods in order:
- Session cookie (via scs session manager)
- API key (X-API-Key header with "sk-" prefix, SHA-256 hashed)
On success, the authenticated User is attached to the request context via ContextWithUser.
func RegisterRoutes ¶
func RegisterRoutes(api huma.API, sessionManager *scs.SessionManager, userStore *UserStore, keyStore *KeyStore, disableSignup bool)
RegisterRoutes wires up all authentication-related HTTP endpoints onto the provided Huma API: signup, login, logout, me, and API key management.
func RegisterServiceAccountRoutes ¶ added in v0.16.0
RegisterServiceAccountRoutes wires service account and audit endpoints.
func ValidScope ¶ added in v0.16.0
ValidScope reports whether s is a scope the server knows.
Types ¶
type APIKeyInfo ¶
type KeyInfo ¶ added in v0.16.0
KeyInfo describes the API key that authenticated a request. A session or an unrestricted personal key has none of these limits.
func KeyFromContext ¶ added in v0.16.0
KeyFromContext returns the authenticating key, or nil for a session.
func (*KeyInfo) Restricted ¶ added in v0.16.0
Restricted reports whether the deny-by-default table applies to the key.
type KeyStore ¶
type KeyStore struct {
// contains filtered or unexported fields
}
func NewKeyStore ¶
func (*KeyStore) CreateLimited ¶ added in v0.16.0
func (s *KeyStore) CreateLimited(ctx context.Context, userID, name, keyPrefix, keyHash string, scopes []string, setID *string) (*APIKeyRow, error)
CreateLimited inserts a key with scopes and an optional set limit.
func (*KeyStore) GetByPrefix ¶
func (*KeyStore) ListByUser ¶
type RouteRule ¶ added in v0.16.0
type RouteRule struct {
Method string
Pattern string
Scopes []string
// SetLimited marks a route a set-limited key may reach. The handler then
// checks that the set is the key's own.
SetLimited bool
}
RouteRule says which scopes reach one route. A rule with no scopes is listed so that the route is a decision, and a restricted key still never reaches it.
func RouteRules ¶ added in v0.16.0
func RouteRules() []RouteRule
RouteRules returns the table for the coverage test.
type ServiceAccount ¶ added in v0.16.0
type ServiceAccount struct {
ID string `json:"id"`
Name string `json:"name"`
KeyCount int `json:"key_count"`
LastUsedAt *time.Time `json:"last_used_at"`
CreatedAt time.Time `json:"created_at"`
}
ServiceAccount is a machine identity. Its name is the consumer it acts for.
type User ¶
type User struct {
ID string `json:"id"`
Email string `json:"email"`
Name string `json:"name"`
Role string `json:"role"`
Kind string `json:"-"`
}
func UserFromContext ¶
func (*User) IsPrivileged ¶ added in v0.9.0
IsPrivileged reports whether the user may perform privileged operations. Gate on this rather than comparing role strings at call sites.
type UserStore ¶
type UserStore struct {
// contains filtered or unexported fields
}
func NewUserStore ¶
func (*UserStore) CreateWithRole ¶
func (*UserStore) GetByEmail ¶
func (*UserStore) Search ¶ added in v0.10.0
Search finds users by name or email prefix, case-insensitively. It is deliberately capped and requires a minimum query length: the endpoint on top of it is open to every authenticated member, so it must be a lookup rather than a way to dump the directory.
func (*UserStore) SetResetRequired ¶ added in v0.7.0
func (*UserStore) UpdatePassword ¶ added in v0.7.0
func (*UserStore) UpdateRole ¶ added in v0.9.0
UpdateRole sets a user's role. It reports whether a row was updated so the caller can distinguish "no such user" from a successful change. Callers are responsible for authorising the change and for validating the role value.