Files
silo-mod/internal/auth/auth.go
forbes e92c022e80 feat(auth): add authentication, RBAC, API tokens, and default admin
Add a complete authentication and authorization system to Silo with
three pluggable backends (local bcrypt, LDAP/FreeIPA, OIDC/Keycloak),
session management, API token support, and role-based access control.

Authentication backends:
- Local: bcrypt (cost 12) password verification against users table
- LDAP: FreeIPA simple bind with group-to-role mapping
- OIDC: Keycloak redirect flow with realm role mapping
- Backends are tried in order; users upserted to DB on first login

Session and token management:
- PostgreSQL-backed sessions via alexedwards/scs + pgxstore
- Opaque API tokens (silo_ prefix, SHA-256 hashed, shown once)
- 24h session lifetime, HttpOnly/SameSite=Lax/Secure cookies

Role-based access control (admin > editor > viewer):
- RequireAuth middleware: Bearer token -> session -> redirect/401
- RequireRole middleware: per-route-group minimum role enforcement
- CSRF protection via justinas/nosurf on web forms, API exempt
- CORS locked to configured origins when auth enabled

Route restructuring:
- Public: /health, /ready, /login, /auth/oidc, /auth/callback
- Web (auth + CSRF): /, /projects, /schemas, /settings
- API read (viewer): GET /api/**
- API write (editor): POST/PUT/PATCH/DELETE /api/**

User context wiring:
- created_by/updated_by columns on items, projects, relationships
- All create/update handlers populate tracking fields from context
- CSV and BOM import handlers pass authenticated username
- Revision creation tracks user across all code paths

Default admin account:
- Configurable via auth.local.default_admin_username/password
- Env var overrides: SILO_ADMIN_USERNAME, SILO_ADMIN_PASSWORD
- Idempotent: created on first startup, skipped if exists

CLI and FreeCAD plugin:
- silo token create/list/revoke subcommands (HTTP API client)
- FreeCAD SiloClient sends Bearer token on all requests
- Token read from ApiToken preference or SILO_API_TOKEN env var

Web UI:
- Login page (Catppuccin Mocha themed, OIDC button conditional)
- Settings page with account info and API token management
- User display name, role badge, and logout button in header
- One-time token display banner with copy-to-clipboard

Database (migration 009):
- users table with role, auth_source, oidc_subject, password_hash
- api_tokens table with SHA-256 hash, prefix, expiry, scopes
- sessions table (scs pgxstore schema)
- audit_log table (schema ready for future use)
- created_by/updated_by ALTER on items, relationships, projects

New dependencies: scs/v2, scs/pgxstore, go-oidc/v3, go-ldap/v3,
justinas/nosurf, golang.org/x/oauth2
2026-01-31 11:20:12 -06:00

189 lines
5.2 KiB
Go

// Package auth provides authentication and authorization for Silo.
package auth
import (
"context"
"fmt"
"github.com/rs/zerolog"
"github.com/kindredsystems/silo/internal/db"
)
// Role constants.
const (
RoleAdmin = "admin"
RoleEditor = "editor"
RoleViewer = "viewer"
)
// roleRank maps roles to their privilege level for comparison.
var roleRank = map[string]int{
RoleViewer: 1,
RoleEditor: 2,
RoleAdmin: 3,
}
// RoleSatisfies returns true if the user's role meets or exceeds the minimum required role.
func RoleSatisfies(userRole, minimumRole string) bool {
return roleRank[userRole] >= roleRank[minimumRole]
}
// User represents an authenticated user in the system.
type User struct {
ID string
Username string
DisplayName string
Email string
Role string // "admin", "editor", "viewer"
AuthSource string // "local", "ldap", "oidc"
}
// contextKey is a private type for context keys in this package.
type contextKey int
const userContextKey contextKey = iota
// UserFromContext extracts the authenticated user from the request context.
// Returns nil if no user is present (unauthenticated request).
func UserFromContext(ctx context.Context) *User {
u, _ := ctx.Value(userContextKey).(*User)
return u
}
// ContextWithUser returns a new context carrying the given user.
func ContextWithUser(ctx context.Context, u *User) context.Context {
return context.WithValue(ctx, userContextKey, u)
}
// Backend is the interface every auth provider must implement.
type Backend interface {
// Name returns the backend identifier ("local", "ldap").
Name() string
// Authenticate validates credentials and returns the authenticated user.
Authenticate(ctx context.Context, username, password string) (*User, error)
}
// Service orchestrates authentication across all configured backends.
type Service struct {
users *db.UserRepository
tokens *db.TokenRepository
backends []Backend
logger zerolog.Logger
}
// NewService creates the auth service with the given backends.
func NewService(logger zerolog.Logger, users *db.UserRepository, tokens *db.TokenRepository, backends ...Backend) *Service {
return &Service{
users: users,
tokens: tokens,
backends: backends,
logger: logger,
}
}
// Authenticate tries each backend in order until one succeeds.
// On success, upserts the user into the local database and updates last_login_at.
func (s *Service) Authenticate(ctx context.Context, username, password string) (*User, error) {
for _, b := range s.backends {
user, err := b.Authenticate(ctx, username, password)
if err != nil {
s.logger.Debug().Str("backend", b.Name()).Str("username", username).Err(err).Msg("auth attempt failed")
continue
}
// Upsert user into local database
dbUser := &db.User{
Username: user.Username,
DisplayName: user.DisplayName,
Email: user.Email,
AuthSource: user.AuthSource,
Role: user.Role,
}
if err := s.users.Upsert(ctx, dbUser); err != nil {
return nil, fmt.Errorf("upserting user: %w", err)
}
user.ID = dbUser.ID
s.logger.Info().Str("backend", b.Name()).Str("username", username).Msg("user authenticated")
return user, nil
}
return nil, fmt.Errorf("invalid credentials")
}
// UpsertOIDCUser upserts a user from OIDC claims into the local database.
func (s *Service) UpsertOIDCUser(ctx context.Context, user *User) error {
dbUser := &db.User{
Username: user.Username,
DisplayName: user.DisplayName,
Email: user.Email,
AuthSource: "oidc",
OIDCSubject: &user.ID, // ID carries the OIDC subject before DB upsert
Role: user.Role,
}
if err := s.users.Upsert(ctx, dbUser); err != nil {
return fmt.Errorf("upserting oidc user: %w", err)
}
user.ID = dbUser.ID
return nil
}
// ValidateToken checks a raw API token and returns the owning user.
func (s *Service) ValidateToken(ctx context.Context, rawToken string) (*User, error) {
tokenInfo, err := s.tokens.ValidateToken(ctx, rawToken)
if err != nil {
return nil, err
}
dbUser, err := s.users.GetByID(ctx, tokenInfo.UserID)
if err != nil {
return nil, fmt.Errorf("looking up token user: %w", err)
}
if dbUser == nil || !dbUser.IsActive {
return nil, fmt.Errorf("token user not found or inactive")
}
// Update last_used_at asynchronously
go func() {
_ = s.tokens.TouchLastUsed(context.Background(), tokenInfo.ID)
}()
return &User{
ID: dbUser.ID,
Username: dbUser.Username,
DisplayName: dbUser.DisplayName,
Email: dbUser.Email,
Role: dbUser.Role,
AuthSource: dbUser.AuthSource,
}, nil
}
// GetUserByID retrieves a user by their database ID.
func (s *Service) GetUserByID(ctx context.Context, id string) (*User, error) {
dbUser, err := s.users.GetByID(ctx, id)
if err != nil {
return nil, err
}
if dbUser == nil {
return nil, nil
}
return &User{
ID: dbUser.ID,
Username: dbUser.Username,
DisplayName: dbUser.DisplayName,
Email: dbUser.Email,
Role: dbUser.Role,
AuthSource: dbUser.AuthSource,
}, nil
}
// Users returns the underlying user repository for direct access.
func (s *Service) Users() *db.UserRepository {
return s.users
}
// Tokens returns the underlying token repository for direct access.
func (s *Service) Tokens() *db.TokenRepository {
return s.tokens
}