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

View File

@@ -1,3 +1,4 @@
DB_PATH=db.sqlite
CLIENT_ID=your-client-id
REDIRECT_URI=http://localhost:8080/callback
ESI_CLIENT_ID=your-client-id
ESI_REDIRECT_URI=http://localhost:8080/callback
ESI_SCOPES=esi-planets.manage_planets.v1

1
go.mod
View File

@@ -4,6 +4,7 @@ go 1.23.6
require (
git.site.quack-lab.dev/dave/cylogger v1.4.0
github.com/joho/godotenv v1.5.1
gorm.io/driver/sqlite v1.6.0
gorm.io/gorm v1.31.0
)

2
go.sum
View File

@@ -14,6 +14,8 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=

82
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
@@ -72,3 +55,30 @@ func main() {
// 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
}