diff --git a/conf.json.example b/conf.json.example index 30a375d..ed24aef 100644 --- a/conf.json.example +++ b/conf.json.example @@ -1,5 +1,6 @@ { "username": "admin", "password": "factorio", - "database_file": "auth.leveldb" + "database_file": "auth.leveldb", + "cookie_encryption_key": "topsecretkey" } diff --git a/src/auth.go b/src/auth.go index 14dc6ab..43441eb 100644 --- a/src/auth.go +++ b/src/auth.go @@ -23,7 +23,7 @@ func initAuth() *AuthHTTP { return &AuthHTTP{} } -func (auth *AuthHTTP) createAuthDb(backendFile string) error { +func (auth *AuthHTTP) CreateAuth(backendFile string, cookieKey string) error { var err error os.Mkdir(backendFile, 0755) @@ -33,22 +33,20 @@ func (auth *AuthHTTP) createAuthDb(backendFile string) error { return err } + roles := make(map[string]httpauth.Role) + roles["user"] = 30 + roles["admin"] = 80 + + auth.aaa, err = httpauth.NewAuthorizer(auth.backend, []byte(cookieKey), "user", roles) + if err != nil { + log.Printf("Error creating authorizer: %s", err) + return err + } + return nil } -func (auth *AuthHTTP) createRoles() { - var err error - roles := make(map[string]httpauth.Role) - - roles["user"] = 30 - roles["admin"] = 80 - auth.aaa, err = httpauth.NewAuthorizer(auth.backend, []byte("topsecretkey"), "user", roles) - if err != nil { - log.Printf("Error creating roles: %s", err) - } -} - -func (auth *AuthHTTP) createInitialUser(username, password, role, email string) error { +func (auth *AuthHTTP) CreateOrUpdateUser(username, password, role, email string) error { user := httpauth.UserData{Username: username, Role: role, Email: email} err := auth.backend.SaveUser(user) if err != nil { @@ -58,11 +56,11 @@ func (auth *AuthHTTP) createInitialUser(username, password, role, email string) err = auth.aaa.Update(nil, nil, username, password, email) if err != nil { - log.Printf("Error saving user: %s", err) + log.Printf("Error updating user: %s", err) return err } - log.Printf("Created initial user: %s", user.Username) + log.Printf("Created user: %s", user.Username) return nil } diff --git a/src/main.go b/src/main.go index b15720c..a0581c3 100644 --- a/src/main.go +++ b/src/main.go @@ -11,19 +11,20 @@ import ( ) type Config struct { - FactorioDir string `json:"factorio_dir"` - FactorioSavesDir string `json:"saves_dir"` - FactorioModsDir string `json:"mods_dir"` - FactorioConfigFile string `json:"config_file"` - FactorioLog string `json:"logfile"` - FactorioBinary string `json:"factorio_binary"` - ServerIP string `json:"server_ip"` - ServerPort string `json:"server_port"` - MaxUploadSize int64 `json:"max_upload_size"` - Username string `json:"username"` - Password string `json:"password"` - DatabaseFile string `json:"database_file"` - ConfFile string + FactorioDir string `json:"factorio_dir"` + FactorioSavesDir string `json:"saves_dir"` + FactorioModsDir string `json:"mods_dir"` + FactorioConfigFile string `json:"config_file"` + FactorioLog string `json:"logfile"` + FactorioBinary string `json:"factorio_binary"` + ServerIP string `json:"server_ip"` + ServerPort string `json:"server_port"` + MaxUploadSize int64 `json:"max_upload_size"` + Username string `json:"username"` + Password string `json:"password"` + DatabaseFile string `json:"database_file"` + CookieEncryptionKey string `json:"cookie_encryption_key"` + ConfFile string } var ( @@ -82,12 +83,8 @@ func main() { // Initialize authentication system Auth = initAuth() - Auth.createAuthDb(config.DatabaseFile) - Auth.createRoles() - err := Auth.createInitialUser(config.Username, config.Password, "admin", "") - if err != nil { - log.Printf("Error creating user: %s", err) - } + Auth.CreateAuth(config.DatabaseFile, config.CookieEncryptionKey) + Auth.CreateOrUpdateUser(config.Username, config.Password, "admin", "") router := NewRouter() createModPackDir()