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
149 lines
3.8 KiB
Go
149 lines
3.8 KiB
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
|
"golang.org/x/oauth2"
|
|
)
|
|
|
|
// OIDCConfig holds settings for the OIDC backend.
|
|
type OIDCConfig struct {
|
|
IssuerURL string
|
|
ClientID string
|
|
ClientSecret string
|
|
RedirectURL string
|
|
Scopes []string
|
|
AdminRole string
|
|
EditorRole string
|
|
DefaultRole string
|
|
}
|
|
|
|
// OIDCBackend handles OIDC redirect-based authentication (e.g., Keycloak).
|
|
type OIDCBackend struct {
|
|
cfg OIDCConfig
|
|
provider *oidc.Provider
|
|
verifier *oidc.IDTokenVerifier
|
|
oauth oauth2.Config
|
|
}
|
|
|
|
// NewOIDCBackend creates and initializes an OIDC backend.
|
|
// Contacts the issuer URL to discover endpoints, so requires network access.
|
|
func NewOIDCBackend(ctx context.Context, cfg OIDCConfig) (*OIDCBackend, error) {
|
|
provider, err := oidc.NewProvider(ctx, cfg.IssuerURL)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("oidc provider discovery: %w", err)
|
|
}
|
|
|
|
scopes := cfg.Scopes
|
|
if len(scopes) == 0 {
|
|
scopes = []string{oidc.ScopeOpenID, "profile", "email"}
|
|
}
|
|
|
|
oauthConfig := oauth2.Config{
|
|
ClientID: cfg.ClientID,
|
|
ClientSecret: cfg.ClientSecret,
|
|
RedirectURL: cfg.RedirectURL,
|
|
Endpoint: provider.Endpoint(),
|
|
Scopes: scopes,
|
|
}
|
|
|
|
return &OIDCBackend{
|
|
cfg: cfg,
|
|
provider: provider,
|
|
verifier: provider.Verifier(&oidc.Config{ClientID: cfg.ClientID}),
|
|
oauth: oauthConfig,
|
|
}, nil
|
|
}
|
|
|
|
// Name returns "oidc".
|
|
func (b *OIDCBackend) Name() string { return "oidc" }
|
|
|
|
// Authenticate is not used for OIDC — the redirect flow is handled by
|
|
// AuthCodeURL and Exchange instead.
|
|
func (b *OIDCBackend) Authenticate(_ context.Context, _, _ string) (*User, error) {
|
|
return nil, fmt.Errorf("OIDC requires redirect flow, not direct authentication")
|
|
}
|
|
|
|
// AuthCodeURL generates the OIDC authorization URL with the given state parameter.
|
|
func (b *OIDCBackend) AuthCodeURL(state string) string {
|
|
return b.oauth.AuthCodeURL(state)
|
|
}
|
|
|
|
// Exchange handles the OIDC callback: exchanges the authorization code for tokens,
|
|
// verifies the ID token, and extracts user claims.
|
|
func (b *OIDCBackend) Exchange(ctx context.Context, code string) (*User, error) {
|
|
token, err := b.oauth.Exchange(ctx, code)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("oidc code exchange: %w", err)
|
|
}
|
|
|
|
rawIDToken, ok := token.Extra("id_token").(string)
|
|
if !ok {
|
|
return nil, fmt.Errorf("no id_token in oidc response")
|
|
}
|
|
|
|
idToken, err := b.verifier.Verify(ctx, rawIDToken)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("oidc token verification: %w", err)
|
|
}
|
|
|
|
var claims struct {
|
|
Subject string `json:"sub"`
|
|
PreferredUsername string `json:"preferred_username"`
|
|
Email string `json:"email"`
|
|
Name string `json:"name"`
|
|
RealmAccess struct {
|
|
Roles []string `json:"roles"`
|
|
} `json:"realm_access"`
|
|
}
|
|
if err := idToken.Claims(&claims); err != nil {
|
|
return nil, fmt.Errorf("parsing oidc claims: %w", err)
|
|
}
|
|
|
|
username := claims.PreferredUsername
|
|
if username == "" {
|
|
username = claims.Subject
|
|
}
|
|
displayName := claims.Name
|
|
if displayName == "" {
|
|
displayName = username
|
|
}
|
|
|
|
role := b.resolveRole(claims.RealmAccess.Roles)
|
|
|
|
return &User{
|
|
ID: claims.Subject, // Temporarily holds OIDC subject; replaced by DB ID after upsert
|
|
Username: username,
|
|
DisplayName: displayName,
|
|
Email: claims.Email,
|
|
Role: role,
|
|
AuthSource: "oidc",
|
|
}, nil
|
|
}
|
|
|
|
// resolveRole maps Keycloak realm roles to a Silo role.
|
|
func (b *OIDCBackend) resolveRole(roles []string) string {
|
|
roleSet := make(map[string]struct{}, len(roles))
|
|
for _, r := range roles {
|
|
roleSet[r] = struct{}{}
|
|
}
|
|
|
|
if b.cfg.AdminRole != "" {
|
|
if _, ok := roleSet[b.cfg.AdminRole]; ok {
|
|
return RoleAdmin
|
|
}
|
|
}
|
|
if b.cfg.EditorRole != "" {
|
|
if _, ok := roleSet[b.cfg.EditorRole]; ok {
|
|
return RoleEditor
|
|
}
|
|
}
|
|
|
|
if b.cfg.DefaultRole != "" {
|
|
return b.cfg.DefaultRole
|
|
}
|
|
return RoleViewer
|
|
}
|