Skip to content

Commit

Permalink
fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
garrettladley committed Jun 10, 2024
1 parent aa99835 commit ea57f51
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 78 deletions.
2 changes: 2 additions & 0 deletions backend/config/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package config
type Settings struct {
Application ApplicationSettings
Database DatabaseSettings
RedisSession RedisSettings
RedisLimiter RedisSettings
SuperUser SuperUserSettings
Calendar CalendarSettings
Expand All @@ -20,6 +21,7 @@ type Integrations struct {
type intermediateSettings struct {
Application ApplicationSettings `envPrefix:"SAC_APPLICATION_"`
Database intermediateDatabaseSettings `envPrefix:"SAC_DB_"`
RedisSession intermediateRedisSettings `envPrefix:"SAC_REDIS_SESSION_"`
RedisLimiter intermediateRedisSettings `envPrefix:"SAC_REDIS_LIMITER_"`
SuperUser intermediateSuperUserSettings `envPrefix:"SAC_SUDO_"`
AWS intermediateAWSSettings `envPrefix:"SAC_AWS_"`
Expand Down
79 changes: 4 additions & 75 deletions backend/integrations/oauth/soth/sothic/sothic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,14 @@ import (
"fmt"
"io"
"net/url"
"path/filepath"
"strings"

"github.com/GenerateNU/sac/backend/config"
"github.com/GenerateNU/sac/backend/database/store"
"github.com/caarlos0/env/v11"
"github.com/gofiber/fiber/v2"
"github.com/gofiber/fiber/v2/middleware/session"
"github.com/joho/godotenv"

"github.com/GenerateNU/sac/backend/integrations/oauth/soth"
m "github.com/garrettladley/mattress"
)

type key int
Expand All @@ -36,78 +33,10 @@ var (
SessionStore *session.Store
)

type storageSettings struct {
username string
password *m.Secret[string]
host string
port uint
db int
// TLSConfig *TLSConfig
}

func (s *storageSettings) Username() string {
return s.username
}

func (s *storageSettings) Password() *m.Secret[string] {
return s.password
}

func (s *storageSettings) Host() string {
return s.host
}

func (s *storageSettings) Port() uint {
return s.port
}

func (s *storageSettings) DB() int {
return s.db
}

type intermediateStorageSettings struct {
Username string `env:"SAC_REDIS_SESSION_USERNAME"`
Password string `env:"SAC_REDIS_SESSION_PASSWORD"`
Host string `env:"SAC_REDIS_SESSION_HOST"`
Port uint `env:"SAC_REDIS_SESSION_PORT"`
DB int `env:"SAC_REDIS_SESSION_DB"`
// TLSConfig *intermediateTLSConfig `env:"TLS_CONFIG"`
}

func (i *intermediateStorageSettings) into() (*storageSettings, error) {
password, err := m.NewSecret(i.Password)
if err != nil {
return nil, fmt.Errorf("failed to create secret from password: %s", err.Error())
}

return &storageSettings{
username: i.Username,
password: password,
host: i.Host,
port: i.Port,
db: i.DB,
// TLSConfig: i.TLSConfig.into(),
}, nil
}

func init() {
if err := godotenv.Load(filepath.Join("..", "config", ".env.dev")); err != nil {
panic(fmt.Errorf("failed to load environment variables: %s", err.Error()))
}

intSettings, err := env.ParseAs[intermediateStorageSettings]()
if err != nil {
panic(fmt.Errorf("failed to parse environment variables: %s", err.Error()))
}

settings, err := intSettings.into()
if err != nil {
panic(fmt.Errorf("failed to convert environment variables: %s", err.Error()))
}

// optional config
// MUST be called before using the package
func Init(sessionSettings config.RedisSettings) {
config := session.Config{
Storage: store.NewRedisClient(settings),
Storage: store.NewRedisClient(sessionSettings),
KeyLookup: fmt.Sprintf("cookie:%s", SessionName),
// for local
CookieHTTPOnly: true,
Expand Down
2 changes: 2 additions & 0 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/GenerateNU/sac/backend/integrations"
"github.com/GenerateNU/sac/backend/integrations/email"
"github.com/GenerateNU/sac/backend/integrations/file"
"github.com/GenerateNU/sac/backend/integrations/oauth/soth/sothic"
"github.com/GenerateNU/sac/backend/server"
"github.com/GenerateNU/sac/backend/telemetry"
"github.com/GenerateNU/sac/backend/utilities"
Expand Down Expand Up @@ -66,6 +67,7 @@ func main() {
startBackgroundJobs(ctx, db)

stores := store.ConfigureStores(config.RedisLimiter)
sothic.Init(config.RedisLimiter)
integrations := configureIntegrations(&config.Integrations)

tp := telemetry.InitTracer()
Expand Down
3 changes: 0 additions & 3 deletions config/.env.template
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ SAC_DB_HOST="127.0.0.1"
SAC_DB_NAME="sac"
SAC_DB_REQUIRE_SSL="false"

SAC_SESSION_SECRET="this is a super duper long secret 1234567890!@#$%^&*()"
SESSION_SECRET="this is a super duper long secret 1234567890!@#$%^&*()"

SAC_REDIS_SESSION_USERNAME="redis_session"
SAC_REDIS_SESSION_PASSWORD="redis_session!#1"
SAC_REDIS_SESSION_HOST="127.0.0.1"
Expand Down

0 comments on commit ea57f51

Please sign in to comment.