Add support for defining the cookie encryption key in the server config. Rename createInitialUser to CreateOrUpdateUser.

This commit is contained in:
Kyle Bostelmann 2016-09-29 18:35:12 -03:00
parent ffeeaa035b
commit 6c73277616
3 changed files with 32 additions and 36 deletions

View File

@ -1,5 +1,6 @@
{
"username": "admin",
"password": "factorio",
"database_file": "auth.leveldb"
"database_file": "auth.leveldb",
"cookie_encryption_key": "topsecretkey"
}

View File

@ -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
}

View File

@ -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()