auth

package
v0.19.0 Latest Latest
Warning

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

Go to latest
Published: Nov 8, 2026 License: Apache-2.0 Imports: 45 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RoleOwner  = "owner"
	RoleAdmin  = "admin"
	RoleMember = "member"

	KindHuman   = "human"
	KindService = "service"
)
View Source
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.

View Source
const AuthAllRoutes = "/api/auth/*"

AuthAllRoutes is the chi pattern auth-all serves a person's own account on.

View Source
const WorkersAccount = "eval-workers"

WorkersAccount is the service account that every worker key belongs to.

Variables

View Source
var ErrClaimRequired = apierr.ErrForbidden.WithMessage("Enter the claim code from the server log before you create the owner.")

ErrClaimRequired refuses the first sign-up without the claim code.

Functions

func Allowed added in v0.16.0

func Allowed(ctx context.Context, scope string) bool

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 CheckAPIKey(storedHash, key string) bool

func Claimed added in v0.19.0

func Claimed(ctx context.Context) bool

Claimed reports whether ctx carries the verified claim code.

func ContextWithClaim added in v0.19.0

func ContextWithClaim(ctx context.Context) context.Context

ContextWithClaim marks ctx as carrying the verified claim code.

func ContextWithKey added in v0.16.0

func ContextWithKey(ctx context.Context, k *KeyInfo) context.Context

ContextWithKey attaches the authenticating key to ctx.

func ContextWithUser

func ContextWithUser(ctx context.Context, user *User) context.Context

func CreateWorkerKey added in v0.19.0

func CreateWorkerKey(ctx context.Context, pool *pgxpool.Pool, keys *KeyStore, keyName, actor string) (string, error)

CreateWorkerKey issues a key that can only claim and run eval jobs, under the eval-workers service account, which it creates on first use.

func GenerateAPIKey

func GenerateAPIKey() (fullKey, prefix string, err error)

func HasScope added in v0.16.0

func HasScope(ctx context.Context, scope string) bool

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 HashAPIKey(key string) (string, error)

func KindOf added in v0.17.0

func KindOf(u *store.User) string

KindOf returns a user's kind, human or service.

func Middleware

func Middleware(people *People, keyStore *KeyStore) func(http.Handler) http.Handler

Middleware enforces authentication on /mcp and all /api/ routes except exempt paths. auth-all resolves a session cookie or a bearer value, and Skael adds its key limits, which auth-all does not hold.

func NewSMTPSender added in v0.17.0

func NewSMTPSender(host string, port int, username, password, from string) email.Sender

NewSMTPSender returns the sender behind self-service password reset. On port 465 it speaks TLS from the first byte. On any other port net/smtp uses STARTTLS when the server offers it, and refuses to send auth in the clear. from may carry a display name, such as "Skael <skael@example.com>".

func PassXAPIKey added in v0.17.0

func PassXAPIKey(r *http.Request)

PassXAPIKey lets installed hooks keep sending X-API-Key.

func RegisterRoutes

func RegisterRoutes(api huma.API, people *People, userStore *UserStore, keyStore *KeyStore)

RegisterRoutes wires up the routes Skael keeps: account state, API keys, and owner administration. auth-all serves the rest of a person's account.

func RegisterServiceAccountRoutes added in v0.16.0

func RegisterServiceAccountRoutes(api huma.API, pool *pgxpool.Pool, keys *KeyStore)

RegisterServiceAccountRoutes wires service account and audit endpoints.

func SendTestSMTP added in v0.19.0

func SendTestSMTP(ctx context.Context, host string, port int, username, password, from, to string) error

SendTestSMTP sends one message to to, so an owner can check the settings.

func SignupAllowed added in v0.17.0

func SignupAllowed(provider, address string, disableSignup bool, domains []string) error

SignupAllowed decides whether one new account may exist. The company's identity provider is its own gate, so DISABLE_SIGNUP does not stop OIDC.

func ValidScope added in v0.16.0

func ValidScope(s string) bool

ValidScope reports whether s is a scope the server knows.

Types

type APIKeyInfo

type APIKeyInfo struct {
	ID         string     `json:"id"`
	Name       string     `json:"name"`
	Prefix     string     `json:"prefix"`
	LastUsedAt *time.Time `json:"last_used_at"`
	CreatedAt  time.Time  `json:"created_at"`
	Scopes     []string   `json:"scopes"`
	SetID      *string    `json:"set_id,omitempty"`
}

type APIKeyRow

type APIKeyRow struct {
	ID         string
	UserID     string
	Name       string
	KeyPrefix  string
	KeyHash    string
	LastUsedAt *time.Time
	CreatedAt  time.Time
	Scopes     []string
	SetID      *string
}

type KeyInfo added in v0.16.0

type KeyInfo struct {
	ID     string
	Scopes []string
	SetID  string
}

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

func KeyFromContext(ctx context.Context) *KeyInfo

KeyFromContext returns the authenticating key, or nil for a session.

func (*KeyInfo) Restricted added in v0.16.0

func (k *KeyInfo) Restricted() bool

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 NewKeyStore(pool *pgxpool.Pool) *KeyStore

func (*KeyStore) Create

func (s *KeyStore) Create(ctx context.Context, userID, name, keyPrefix, keyHash string) (*APIKeyRow, error)

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) Delete

func (s *KeyStore) Delete(ctx context.Context, id, userID string) error

func (*KeyStore) GetByID added in v0.17.0

func (s *KeyStore) GetByID(ctx context.Context, id string) (*APIKeyRow, error)

GetByID returns one key with its limits.

func (*KeyStore) GetByPrefix

func (s *KeyStore) GetByPrefix(ctx context.Context, prefix string) (*APIKeyRow, error)

func (*KeyStore) ListByUser

func (s *KeyStore) ListByUser(ctx context.Context, userID string) ([]APIKeyInfo, error)

func (*KeyStore) UpdateLastUsed

func (s *KeyStore) UpdateLastUsed(ctx context.Context, id string)

type People added in v0.17.0

type People struct {
	Auth  *authall.Auth
	Admin *admin.Plugin
	// contains filtered or unexported fields
}

People is the auth-all instance behind every person's account.

func NewPeople added in v0.17.0

func NewPeople(pool *pgxpool.Pool, keys *KeyStore, cfg PeopleConfig) (*People, error)

NewPeople builds auth-all with Skael's roles, key resolver and signup rules.

func (*People) SignInMethods added in v0.17.0

func (p *People) SignInMethods() SignInMethods

SignInMethods reports what the sign-in page may show.

type PeopleConfig added in v0.17.0

type PeopleConfig struct {
	PublicURL      string
	CookieSecure   bool
	DisableSignup  bool
	AllowedDomains []string
	TrustedOrigins []string
	TrustedProxies []string

	OIDCIssuer, OIDCClientID, OIDCClientSecret string
	GoogleClientID, GoogleClientSecret         string
	GitHubClientID, GitHubClientSecret         string
	EmailSender                                email.Sender
	// Inspecting builds routes for the OpenAPI document with no database.
	Inspecting bool
	// FirstPerson, when set, must accept the creation of the first person.
	// The server uses it to require the claim code.
	FirstPerson func(ctx context.Context) error
	// Live, when set, returns the public URL, the signup rules, the sign-in
	// providers and the email sender on each use, so a settings change applies
	// without a restart. The other fields of its result are ignored.
	Live func() PeopleConfig
}

PeopleConfig is the part of the server configuration auth-all reads.

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.

func CreateServiceAccountTx added in v0.18.0

func CreateServiceAccountTx(ctx context.Context, tx pgx.Tx, name, actor string) (*ServiceAccount, error)

CreateServiceAccountTx inserts a service account inside tx, so a feature that owns one creates it with the rows that point at it.

type SignInMethods added in v0.17.0

type SignInMethods struct {
	Password       bool     `json:"password"`
	Signup         bool     `json:"signup"`
	ForgotPassword bool     `json:"forgot_password"`
	Providers      []string `json:"providers"`
}

SignInMethods says which sign-in methods an instance offers.

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 UserFromContext(ctx context.Context) *User

func (*User) IsOwner added in v0.9.0

func (u *User) IsOwner() bool

IsOwner reports whether the user is the instance owner.

func (*User) IsPrivileged added in v0.9.0

func (u *User) IsPrivileged() bool

IsPrivileged reports whether the user may perform privileged operations. Gate on this rather than comparing role strings at call sites.

type UserRow

type UserRow struct {
	ID                 string
	Email              string
	Name               string
	Role               string
	MustChangePassword bool
	DisabledAt         *time.Time
	CreatedAt          time.Time
	Kind               string
}

UserRow is one auth_users row. auth-all writes people, and Skael reads them.

type UserStore

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

func NewUserStore

func NewUserStore(pool *pgxpool.Pool) *UserStore

func (*UserStore) Count

func (s *UserStore) Count(ctx context.Context) (int, error)

func (*UserStore) Create

func (s *UserStore) Create(ctx context.Context, email, name string) (*UserRow, error)

Create inserts a member with no credential.

func (*UserStore) CreateWithRole

func (s *UserStore) CreateWithRole(ctx context.Context, email, name, role string) (*UserRow, error)

CreateWithRole inserts a user with no credential.

func (*UserStore) GetByEmail

func (s *UserStore) GetByEmail(ctx context.Context, email string) (*UserRow, error)

func (*UserStore) GetByID

func (s *UserStore) GetByID(ctx context.Context, id string) (*UserRow, error)

func (*UserStore) List added in v0.7.0

func (s *UserStore) List(ctx context.Context) ([]UserRow, error)

func (*UserStore) Search added in v0.10.0

func (s *UserStore) Search(ctx context.Context, q string, limit int) ([]UserRow, error)

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.

Jump to

Keyboard shortcuts

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