Skip to content

Commit

Permalink
create admin user in cli
Browse files Browse the repository at this point in the history
  • Loading branch information
BuckarooBanzay committed Sep 14, 2023
1 parent 9e9a601 commit 7f44864
Showing 1 changed file with 55 additions and 3 deletions.
58 changes: 55 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,34 @@ import (
"flag"
"fmt"
"mtui/app"
"mtui/auth"
"mtui/events"
"mtui/jobs"
"mtui/web"
"net/http"
"os"
"os/signal"
"strings"
"syscall"

dbauth "github.com/minetest-go/mtdb/auth"

"github.com/sirupsen/logrus"
)

func main() {
var version_flag = flag.Bool("version", false, "show the current version and exit")
var help_flag = flag.Bool("help", false, "shows all options")
var create_admin = flag.String("create_admin", "", "creates a new admin-user (format: 'user:pass')")

version_ptr := flag.Bool("version", false, "show the current version and exit")
func main() {
flag.Parse()

if *version_ptr {
if *help_flag {
flag.PrintDefaults()
return
}

if *version_flag {
v := app.Version
if v == "" {
v = "DEV"
Expand Down Expand Up @@ -49,6 +60,47 @@ func main() {
panic(err)
}

if *create_admin != "" {
parts := strings.Split(*create_admin, ":")
if len(parts) != 2 {
panic("invalid format")
}
username := parts[0]
password := parts[1]
salt, verifier, err := auth.CreateAuth(username, password)
if err != nil {
panic(err)
}

str := auth.CreateDBPassword(salt, verifier)
entry, err := a.DBContext.Auth.GetByUsername(username)
if err != nil {
panic(err)
}

entry = &dbauth.AuthEntry{
Name: username,
Password: str,
}
err = a.DBContext.Auth.Create(entry)
if err != nil {
panic(err)
}

for _, priv := range []string{"server", "interact", "privs"} {
err = a.DBContext.Privs.Create(&dbauth.PrivilegeEntry{
ID: *entry.ID,
Privilege: priv,
})

if err != nil {
panic(err)
}
}
fmt.Printf("Created a new admin-user '%s'\n", username)
return
}

logrus.WithFields(logrus.Fields{
"version": app.Version,
}).Info("Starting mtui")
Expand Down

0 comments on commit 7f44864

Please sign in to comment.