Skip to content

🎧 Your Personal Streaming Service

feat: add option to auto-create admin user on first start-up

Deluan2c146ea1fe2b
diff --git a/conf/configuration.go b/conf/configuration.go
index f53a2f6f0..ca031e410 100644
--- a/conf/configuration.go
+++ b/conf/configuration.go
@@ -29,8 +29,9 @@ type nd struct {
ScanInterval string `default:"1m"`
// DevFlags. These are used to enable/disable debugging and incomplete features
- DevDisableBanner bool `default:"false"`
- DevLogSourceLine bool `default:"false"`
+ DevDisableBanner bool `default:"false"`
+ DevLogSourceLine bool `default:"false"`
+ DevAutoCreateAdminPassword string `default:""`
}
var Server = &nd{}
diff --git a/consts/consts.go b/consts/consts.go
index d1ff9dbe1..90ceb657f 100644
--- a/consts/consts.go
+++ b/consts/consts.go
@@ -13,4 +13,7 @@ const (
JWTTokenExpiration = 30 * time.Minute
UIAssetsLocalPath = "ui/build"
+
+ DevInitialUserName = "admin"
+ DevInitialName = "Dev Admin"
)
diff --git a/server/initial_setup.go b/server/initial_setup.go
index 437859871..6661cf07b 100644
--- a/server/initial_setup.go
+++ b/server/initial_setup.go
@@ -1,8 +1,11 @@
package server
import (
+ "context"
+ "fmt"
"time"
+ "github.com/deluan/navidrome/conf"
"github.com/deluan/navidrome/consts"
"github.com/deluan/navidrome/log"
"github.com/deluan/navidrome/model"
@@ -20,11 +23,47 @@ func initialSetup(ds model.DataStore) {
return err
}
+ if conf.Server.DevAutoCreateAdminPassword != "" {
+ if err = createInitialAdminUser(ds); err != nil {
+ return err
+ }
+ }
+
err = ds.Property(nil).Put(consts.InitialSetupFlagKey, time.Now().String())
return err
})
}
+func createInitialAdminUser(ds model.DataStore) error {
+ ctx := context.Background()
+ c, err := ds.User(ctx).CountAll()
+ if err != nil {
+ panic(fmt.Sprintf("Could not access User table: %s", err))
+ }
+ if c == 0 {
+ id, _ := uuid.NewRandom()
+ random, _ := uuid.NewRandom()
+ initialPassword := random.String()
+ if conf.Server.DevAutoCreateAdminPassword != "" {
+ initialPassword = conf.Server.DevAutoCreateAdminPassword
+ }
+ log.Warn("Creating initial admin user. This should only be used for development purposes!!", "user", consts.DevInitialUserName, "password", initialPassword)
+ initialUser := model.User{
+ ID: id.String(),
+ UserName: consts.DevInitialUserName,
+ Name: consts.DevInitialName,
+ Email: "",
+ Password: initialPassword,
+ IsAdmin: true,
+ }
+ err := ds.User(ctx).Put(&initialUser)
+ if err != nil {
+ log.Error("Could not create initial admin user", "user", initialUser, err)
+ }
+ }
+ return err
+}
+
func createJWTSecret(ds model.DataStore) error {
_, err := ds.Property(nil).Get(consts.JWTSecretKey)
if err == nil {