diff --git a/src/api/auth.go b/src/api/auth.go index 71ba4bd..95aac44 100644 --- a/src/api/auth.go +++ b/src/api/auth.go @@ -29,6 +29,7 @@ func SetupAuth() { cookieEncryptionKey, err := base64.StdEncoding.DecodeString(config.CookieEncryptionKey) if err != nil { + log.Printf("Error decoding base64 cookie encryption key: %s", err) panic(err) } sessionStore = sessions.NewCookieStore(cookieEncryptionKey) @@ -39,11 +40,13 @@ func SetupAuth() { auth.db, err = gorm.Open(sqlite.Open(config.SQLiteDatabaseFile), nil) if err != nil { + log.Printf("Error opening sqlite or goem database: %s", err) panic(err) } err = auth.db.AutoMigrate(&User{}) if err != nil { + log.Printf("Error AutoMigrating gorm database: %s", err) panic(err) } @@ -61,6 +64,7 @@ func SetupAuth() { err := auth.addUser(user) if err != nil { + log.Printf("Error adding admin user to db: %s", err) panic(err) } @@ -210,6 +214,7 @@ func AuthMiddleware(next http.Handler) http.Handler { username, ok := session.Values["username"] if !ok { + http.Error(w, "Could not read username from sessioncookie", http.StatusUnauthorized) w.WriteHeader(http.StatusUnauthorized) return } diff --git a/src/bootstrap/user.go b/src/bootstrap/user.go index 1afffa1..c342b83 100644 --- a/src/bootstrap/user.go +++ b/src/bootstrap/user.go @@ -23,12 +23,14 @@ type User struct { func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { oldDB, err := leveldb.OpenFile(oldDBFile, nil) if err != nil { + log.Printf("Error opening old leveldb: %s", err) panic(err) } defer oldDB.Close() newDB, err := gorm.Open(sqlite.Open(newDBFile), nil) if err != nil { + log.Printf("Error open sqlite and gorm: %s", err) panic(err) } defer func() { @@ -39,15 +41,20 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { if db != nil { err2 = db.Close() if err2 != nil { + log.Printf("Error closing real DB of gorm: %s", err2) panic(err2) } } }() - newDB.AutoMigrate(&User{}) + err = newDB.AutoMigrate(&User{}) + if err != nil { + log.Printf("Error autoMigrating sqlite database with user: %s", err) + } oldUserData, err := oldDB.Get([]byte("httpauth::userdata"), nil) if err != nil { + log.Printf("Error getting `httpauth::userdata` from leveldb: %s", err) panic(err) } @@ -59,6 +66,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { } err = json.Unmarshal(oldUserData, &migrationData) if err != nil { + log.Printf("Error unmarshalling old ") panic(err) } @@ -66,6 +74,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { // check if password is "factorio", which was the default password in the old system decodedHash, err := base64.StdEncoding.DecodeString(datum.Hash) if err != nil { + log.Printf("Error decoding base64 hash: %s", err) panic(err) } @@ -76,6 +85,7 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { bcryptPassword, err := bcrypt.GenerateFromPassword([]byte(newPassword), bcrypt.DefaultCost) if err != nil { + log.Printf("Error generating has from password: %s", err) panic(err) } @@ -99,8 +109,10 @@ func MigrateLevelDBToSqlite(oldDBFile, newDBFile string) { oldDB.Close() // delete oldDB + log.Println("Deleting old leveldb database.") err = os.RemoveAll(oldDBFile) if err != nil { + log.Printf("Error removing leveldb: %s", err) panic(err) } }