mirror of
https://github.com/OpenFactorioServerManager/factorio-server-manager.git
synced 2025-03-17 21:07:54 +02:00
Add support for defining the cookie encryption key in the server config. Rename createInitialUser to CreateOrUpdateUser.
This commit is contained in:
parent
ffeeaa035b
commit
6c73277616
@ -1,5 +1,6 @@
|
||||
{
|
||||
"username": "admin",
|
||||
"password": "factorio",
|
||||
"database_file": "auth.leveldb"
|
||||
"database_file": "auth.leveldb",
|
||||
"cookie_encryption_key": "topsecretkey"
|
||||
}
|
||||
|
30
src/auth.go
30
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
|
||||
}
|
||||
|
35
src/main.go
35
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()
|
||||
|
Loading…
x
Reference in New Issue
Block a user