NextWks/src/core/config/config.go

112 lines
2.7 KiB
Go

package config
import (
"fmt"
"os"
"gopkg.in/yaml.v3"
)
// Config represents the full NextWks configuration.
type Config struct {
Server ServerConfig `yaml:"server"`
Admin AdminConfig `yaml:"admin"`
Database DatabaseConfig `yaml:"database"`
Authelia AutheliaConfig `yaml:"authelia"`
OIDC OIDCConfig `yaml:"oidc"`
SMTP SMTPConfig `yaml:"smtp"`
IMAP IMAPConfig `yaml:"imap"`
Locale LocaleConfig `yaml:"locale"`
Session SessionConfig `yaml:"session"`
}
type ServerConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
type AdminConfig struct {
SecretToken string `yaml:"secret_token"`
}
type DatabaseConfig struct {
Type string `yaml:"type"`
Path string `yaml:"path"`
}
type AutheliaConfig struct {
Host string `yaml:"host"`
ConfigPath string `yaml:"config_path"`
UsersDBPath string `yaml:"users_db_path"`
}
// OIDCConfig holds the OIDC provider settings (Authelia).
type OIDCConfig struct {
IssuerURL string `yaml:"issuer_url"` // Public-facing URL users reach (e.g., https://auth.sechpoint.app)
ClientID string `yaml:"client_id"`
ClientSecret string `yaml:"client_secret"`
RedirectURL string `yaml:"redirect_url"`
Domain string `yaml:"domain"`
}
type SMTPConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
Username string `yaml:"username"`
Password string `yaml:"password"`
From string `yaml:"from"`
}
type IMAPConfig struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
}
type LocaleConfig struct {
Language string `yaml:"language"`
Timezone string `yaml:"timezone"`
}
type SessionConfig struct {
Secret string `yaml:"secret"`
ExpiryMinutes int `yaml:"expiry_minutes"`
}
// Load reads and parses the YAML configuration file.
func Load(path string) (*Config, error) {
data, err := os.ReadFile(path)
if err != nil {
return nil, fmt.Errorf("read config file: %w", err)
}
var cfg Config
if err := yaml.Unmarshal(data, &cfg); err != nil {
return nil, fmt.Errorf("parse config file: %w", err)
}
return &cfg, nil
}
// AutheliaSessionSecret extracts the session.secret from Authelia's configuration.
func AutheliaSessionSecret(cfgPath string) (string, error) {
data, err := os.ReadFile(cfgPath)
if err != nil {
return "", fmt.Errorf("read authelia config: %w", err)
}
var autheliaCfg struct {
Session struct {
Secret string `yaml:"secret"`
} `yaml:"session"`
}
if err := yaml.Unmarshal(data, &autheliaCfg); err != nil {
return "", fmt.Errorf("parse authelia config: %w", err)
}
if autheliaCfg.Session.Secret == "" {
return "", fmt.Errorf("authelia session.secret not found in %s", cfgPath)
}
return autheliaCfg.Session.Secret, nil
}