Skip to content

Commit f4328b3

Browse files
committed
refactor: replace os.Getenv calls with viper configuration
- Add SMS, email, push notification, and encryption key config fields to Config struct - Add default values for all new configuration fields - Update DefaultServiceProviderOptions and SSOServerOptions to include AppConfig - Refactor SMS, email, and push service initialization to use viper config - Refactor encryption key initialization to use viper config instead of os.Getenv - Update test cases to include AppConfig in service provider options - Regenerate mocks to include ConfigurationRepository method - Remove unused os imports
1 parent 15d890c commit f4328b3

File tree

8 files changed

+1045
-12
lines changed

8 files changed

+1045
-12
lines changed

apps/ssso/config/config.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ type Config struct {
2929
DTSConnectTimeout time.Duration `mapstructure:"dts_connect_timeout"`
3030
DTSDefaultPKCETTL time.Duration `mapstructure:"dts_default_pkce_ttl"`
3131

32+
// SMS configuration
33+
TwilioAccountSID string `mapstructure:"twilio_account_sid"`
34+
TwilioAuthToken string `mapstructure:"twilio_auth_token"`
35+
TwilioPhoneNumber string `mapstructure:"twilio_phone_number"`
36+
37+
// Email service configuration
38+
ResendAPIKey string `mapstructure:"resend_api_key"`
39+
FromEmail string `mapstructure:"from_email"`
40+
NextPublicBaseURL string `mapstructure:"next_public_base_url"`
41+
42+
// Push notification service configuration
43+
FirebaseProjectID string `mapstructure:"firebase_project_id"`
44+
FirebaseCredentialsPath string `mapstructure:"firebase_credentials_path"`
45+
46+
// Configuration service encryption key
47+
ConfigEncryptionKey string `mapstructure:"config_encryption_key"`
48+
3249
// Token signing
3350
TokenSigningKey string `mapstructure:"token_signing_key"`
3451
TokenSigningKeyFile string `mapstructure:"token_signing_key_file"`
@@ -141,6 +158,23 @@ func LoadConfig() (config Config, err error) {
141158
viper.SetDefault("dts_connect_timeout", "5s") // Consistent with time.ParseDuration
142159
viper.SetDefault("dts_default_pkce_ttl", "10m") // Consistent with time.ParseDuration
143160

161+
// Default values for SMS configuration
162+
viper.SetDefault("twilio_account_sid", "")
163+
viper.SetDefault("twilio_auth_token", "")
164+
viper.SetDefault("twilio_phone_number", "")
165+
166+
// Default values for email service configuration
167+
viper.SetDefault("resend_api_key", "")
168+
viper.SetDefault("from_email", "")
169+
viper.SetDefault("next_public_base_url", "")
170+
171+
// Default values for push notification service configuration
172+
viper.SetDefault("firebase_project_id", "")
173+
viper.SetDefault("firebase_credentials_path", "")
174+
175+
// Default value for configuration service encryption key
176+
viper.SetDefault("config_encryption_key", "your-32-byte-encryption-key-here!!")
177+
144178
if errRead := viper.ReadInConfig(); errRead != nil {
145179
if _, ok := errRead.(viper.ConfigFileNotFoundError); ok {
146180
// Config file not found; ignore error if desired or load from env only

apps/ssso/ssso.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,17 @@ import (
1212
"github.com/pilab-dev/shadow-sso/mongodb"
1313
"github.com/pilab-dev/shadow-sso/services"
1414
"github.com/gin-gonic/gin"
15+
"github.com/joho/godotenv"
1516
"github.com/prometheus/client_golang/prometheus"
1617
"github.com/prometheus/client_golang/prometheus/promhttp"
1718
"github.com/rs/zerolog"
1819
"github.com/rs/zerolog/log"
1920
)
2021

2122
func main() {
23+
// Load .env file if it exists
24+
_ = godotenv.Load()
25+
2226
cfg, err := config.LoadConfig()
2327
if err != nil {
2428
log.Fatal().Err(err).Msg("Failed to load configuration")
@@ -77,6 +81,15 @@ func main() {
7781
}
7882
}
7983

84+
// Bootstrap default configurations from environment variables
85+
log.Info().Msg("Bootstrapping default configurations...")
86+
configRepo := repoProvider.ConfigurationRepository(context.Background())
87+
if err := configRepo.CreateDefaultConfigs(context.Background()); err != nil {
88+
log.Warn().Err(err).Msg("Failed to bootstrap default configurations, some features may not work correctly")
89+
} else {
90+
log.Info().Msg("Default configurations bootstrapped successfully")
91+
}
92+
8093
// Initialize TokenSigner (potentially from file/env)
8194
tokenSigner := services.NewTokenSigner()
8295
if cfg.TokenSigningKey != "" {
@@ -92,15 +105,25 @@ func main() {
92105
tokenSigner.AddKeySigner("temporary-secret-for-hs256-change-me") // Fallback for HS256
93106
}
94107

108+
// Get encryption key for configuration service from viper config
109+
encryptionKey := cfg.ConfigEncryptionKey
110+
if encryptionKey == "" {
111+
// Use a default key for development - in production, this should be set
112+
encryptionKey = "your-32-byte-encryption-key-here!!" // 32 bytes
113+
log.Warn().Msg("config_encryption_key not set, using default key. Set this configuration in production!")
114+
}
115+
95116
// Create SSOServerOptions
96117
opts := ssso.SSOServerOptions{
97118
Config: oidcConfig,
119+
AppConfig: &cfg,
98120
RepositoryProvider: repoProvider,
99121
TokenSigner: tokenSigner,
100122
TokenCache: cache.NewMemoryTokenStore(oidcConfig.AccessTokenTTL), // Use OIDC config's TTL
101123
PkceRepository: nil, // Let NewSSOServer default to in-memory if not provided by repoProvider
102124
FlowStore: nil, // Let NewSSOServer default to in-memory
103125
UserSessionStore: nil, // Let NewSSOServer default to in-memory
126+
EncryptionKey: encryptionKey,
104127
}
105128

106129
router, err := ssso.NewSSOServer(opts)

0 commit comments

Comments
 (0)