1
0
mirror of https://github.com/muety/wakapi.git synced 2023-08-10 21:12:56 +03:00

feat: ability to set default username and password

This commit is contained in:
Ferdinand Mütsch 2020-04-26 14:00:53 +02:00
parent 91a4bb2db3
commit 2b47a7d73f
5 changed files with 40 additions and 37 deletions

View File

@ -5,3 +5,5 @@ WAKAPI_DB_USER=myuser
WAKAPI_DB_PASSWORD=shhh WAKAPI_DB_PASSWORD=shhh
WAKAPI_DB_HOST=localhost WAKAPI_DB_HOST=localhost
WAKAPI_DB_PORT=3306 WAKAPI_DB_PORT=3306
WAKAPI_DEFAULT_USER_NAME=admin
WAKAPI_DEFAULT_USER_PASSWORD=admin # CHANGE!

10
main.go
View File

@ -47,6 +47,8 @@ func readConfig() *models.Config {
dbHost := utils.LookupFatal("WAKAPI_DB_HOST") dbHost := utils.LookupFatal("WAKAPI_DB_HOST")
dbName := utils.LookupFatal("WAKAPI_DB_NAME") dbName := utils.LookupFatal("WAKAPI_DB_NAME")
dbPortStr := utils.LookupFatal("WAKAPI_DB_PORT") dbPortStr := utils.LookupFatal("WAKAPI_DB_PORT")
defaultUserName := utils.LookupFatal("WAKAPI_DEFAULT_USER_NAME")
defaultUserPassword := utils.LookupFatal("WAKAPI_DEFAULT_USER_PASSWORD")
dbPort, err := strconv.Atoi(dbPortStr) dbPort, err := strconv.Atoi(dbPortStr)
cfg, err := ini.Load("config.ini") cfg, err := ini.Load("config.ini")
@ -107,6 +109,8 @@ func readConfig() *models.Config {
DbDialect: dbType, DbDialect: dbType,
DbMaxConn: dbMaxConn, DbMaxConn: dbMaxConn,
CleanUp: cleanUp, CleanUp: cleanUp,
DefaultUserName: defaultUserName,
DefaultUserPassword: defaultUserPassword,
CustomLanguages: customLangs, CustomLanguages: customLangs,
LanguageColors: colors, LanguageColors: colors,
} }
@ -263,16 +267,16 @@ func migrateLanguages(db *gorm.DB, cfg *models.Config) {
} }
func addDefaultUser(db *gorm.DB, cfg *models.Config) { func addDefaultUser(db *gorm.DB, cfg *models.Config) {
pw := md5.Sum([]byte(models.DefaultPassword)) pw := md5.Sum([]byte(cfg.DefaultUserPassword))
pwString := hex.EncodeToString(pw[:]) pwString := hex.EncodeToString(pw[:])
apiKey := uuid.NewV4().String() apiKey := uuid.NewV4().String()
u := &models.User{ID: models.DefaultUser, Password: pwString, ApiKey: apiKey} u := &models.User{ID: cfg.DefaultUserName, Password: pwString, ApiKey: apiKey}
result := db.FirstOrCreate(u, &models.User{ID: u.ID}) result := db.FirstOrCreate(u, &models.User{ID: u.ID})
if result.Error != nil { if result.Error != nil {
log.Println("Unable to create default user.") log.Println("Unable to create default user.")
log.Fatal(result.Error) log.Fatal(result.Error)
} }
if result.RowsAffected > 0 { if result.RowsAffected > 0 {
log.Printf("Created default user '%s' with password '%s' and API key '%s'.\n", u.ID, models.DefaultPassword, u.ApiKey) log.Printf("Created default user '%s' with password '%s' and API key '%s'.\n", u.ID, cfg.DefaultUserPassword, u.ApiKey)
} }
} }

View File

@ -12,6 +12,8 @@ type Config struct {
DbDialect string DbDialect string
DbMaxConn uint DbMaxConn uint
CleanUp bool CleanUp bool
DefaultUserName string
DefaultUserPassword string
CustomLanguages map[string]string CustomLanguages map[string]string
LanguageColors map[string]string LanguageColors map[string]string
} }

View File

@ -1,10 +1,5 @@
package models package models
const (
DefaultUser string = "admin"
DefaultPassword string = "admin"
)
type User struct { type User struct {
ID string `json:"id" gorm:"primary_key"` ID string `json:"id" gorm:"primary_key"`
ApiKey string `json:"api_key" gorm:"unique"` ApiKey string `json:"api_key" gorm:"unique"`

View File

@ -1 +1 @@
1.2.1 1.2.2