Properly use .env for secure shit

This commit is contained in:
2025-10-10 19:57:00 +02:00
parent 683e6871fb
commit bae3254df8
4 changed files with 53 additions and 39 deletions

84
main.go
View File

@@ -3,58 +3,41 @@ package main
import (
"context"
"flag"
"fmt"
logger "git.site.quack-lab.dev/dave/cylogger"
)
import (
"context"
"flag"
"os"
"strings"
"github.com/joho/godotenv"
logger "git.site.quack-lab.dev/dave/cylogger"
"github.com/joho/godotenv"
)
type Options struct {
ClientID string
RedirectURI string
Scopes []string
}
var options Options
func main() {
flag.Parse()
logger.InitFlag()
logger.Info("Starting Eve PI")
// Load environment variables strictly from .env file (fail if there's an error loading)
if err := godotenv.Load(); err != nil {
logger.Error("Error loading .env file: %v", err)
var err error
options, err = LoadOptions()
if err != nil {
logger.Error("Failed to load options %v", err)
return
}
clientID := os.Getenv("ESI_CLIENT_ID")
if clientID == "" {
logger.Error("ESI_CLIENT_ID is required in .env file")
return
}
redirectURI := os.Getenv("ESI_REDIRECT_URI")
if redirectURI == "" {
logger.Error("ESI_REDIRECT_URI is required in .env file")
return
}
rawScopes := os.Getenv("ESI_SCOPES")
if rawScopes == "" {
logger.Error("ESI_SCOPES is required in .env file")
return
}
characterName := os.Getenv("ESI_CHARACTER_NAME")
if characterName == "" {
logger.Error("ESI_CHARACTER_NAME is required in .env file")
return
}
scopes := strings.Fields(rawScopes)
// Create SSO instance
sso, err := NewSSO(
clientID,
redirectURI,
scopes,
options.ClientID,
options.RedirectURI,
options.Scopes,
)
if err != nil {
logger.Error("Failed to create SSO instance %v", err)
@@ -62,7 +45,7 @@ func main() {
}
// Get token for character
token, err := sso.GetToken(context.Background(), characterName)
token, err := sso.GetToken(context.Background(), "PhatPhuckDave")
if err != nil {
logger.Error("Failed to get token %v", err)
return
@@ -71,4 +54,31 @@ func main() {
logger.Info("Got token %s", token)
// Use the token for ESI API calls
// The SSO handles all the complexity behind the scenes
}
}
func LoadOptions() (Options, error) {
// Load environment variables strictly from .env file (fail if there's an error loading)
if err := godotenv.Load(); err != nil {
return Options{}, fmt.Errorf("error loading .env file: %w", err)
}
clientID := os.Getenv("ESI_CLIENT_ID")
if clientID == "" {
return Options{}, fmt.Errorf("ESI_CLIENT_ID is required in .env file")
}
redirectURI := os.Getenv("ESI_REDIRECT_URI")
if redirectURI == "" {
return Options{}, fmt.Errorf("ESI_REDIRECT_URI is required in .env file")
}
rawScopes := os.Getenv("ESI_SCOPES")
if rawScopes == "" {
return Options{}, fmt.Errorf("ESI_SCOPES is required in .env file")
}
scopes := strings.Fields(rawScopes)
return Options{
ClientID: clientID,
RedirectURI: redirectURI,
Scopes: scopes,
}, nil
}