diff --git a/cmd/tulip/serve.go b/cmd/tulip/serve.go index 87f7a0f..78d494b 100644 --- a/cmd/tulip/serve.go +++ b/cmd/tulip/serve.go @@ -55,7 +55,7 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcomm return subcommands.ExitFailure } - var config startUpConfig + var config server.Conf err = json.NewDecoder(openConf).Decode(&config) if err != nil { log.Println("[Tulip] Error: invalid config file: ", err) @@ -71,7 +71,7 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...any) subcomm return subcommands.ExitSuccess } -func normalLoad(startUp startUpConfig, wd string) { +func normalLoad(startUp server.Conf, wd string) { key := genHmacKey() db, err := database.Open(filepath.Join(wd, "tulip.db.sqlite")) @@ -91,7 +91,7 @@ func normalLoad(startUp startUpConfig, wd string) { log.Fatal("[Tulip] Failed to load mail templates:", err) } - srv := server.NewHttpServer(startUp.Listen, startUp.BaseUrl, startUp.OtpIssuer, startUp.ServiceName, startUp.Mail, db, key) + srv := server.NewHttpServer(startUp, db, key) log.Printf("[Tulip] Starting HTTP server on '%s'\n", srv.Addr) go utils.RunBackgroundHttp("HTTP", srv) @@ -122,7 +122,7 @@ func checkDbHasUser(db *database.DB) error { defer tx.Rollback() if err := tx.HasUser(); err != nil { if errors.Is(err, sql.ErrNoRows) { - _, err := tx.InsertUser("Admin", "admin", "admin", "admin@localhost", database.RoleAdmin, false) + _, err := tx.InsertUser("Admin", "admin", "admin", "admin@localhost", false, database.RoleAdmin, false) if err != nil { return fmt.Errorf("failed to add user: %w", err) } diff --git a/database/init.sql b/database/init.sql index 4fb328e..ed54fae 100644 --- a/database/init.sql +++ b/database/init.sql @@ -14,6 +14,7 @@ CREATE TABLE IF NOT EXISTS users locale TEXT DEFAULT "en-US" NOT NULL, role INTEGER DEFAULT 0 NOT NULL, updated_at DATETIME, + registered INTEGER DEFAULT 0, active INTEGER DEFAULT 1 ); diff --git a/database/tx.go b/database/tx.go index 818fb52..2b005e6 100644 --- a/database/tx.go +++ b/database/tx.go @@ -37,13 +37,13 @@ func (t *Tx) HasUser() error { return nil } -func (t *Tx) InsertUser(name, un, pw, email string, role UserRole, active bool) (uuid.UUID, error) { +func (t *Tx) InsertUser(name, un, pw, email string, verifyEmail bool, role UserRole, active bool) (uuid.UUID, error) { pwHash, err := password.HashPassword(pw) if err != nil { return uuid.UUID{}, err } u := uuid.New() - _, err = t.tx.Exec(`INSERT INTO users (subject, name, username, password, email, role, updated_at, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?)`, u, name, un, pwHash, email, role, updatedAt(), active) + _, err = t.tx.Exec(`INSERT INTO users (subject, name, username, password, email, email_verified, role, updated_at, active) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`, u, name, un, pwHash, email, verifyEmail, role, updatedAt(), active) return u, err } @@ -235,18 +235,18 @@ func (t *Tx) InsertClientApp(name, domain string, sso, active bool, owner uuid.U return err } -func (t *Tx) UpdateClientApp(subject uuid.UUID, name, domain string, sso, active bool) error { - _, err := t.tx.Exec(`UPDATE client_store SET name = ?, domain = ?, sso = ?, active = ? WHERE subject = ?`, name, domain, sso, active, subject.String()) +func (t *Tx) UpdateClientApp(subject, owner uuid.UUID, name, domain string, sso, active bool) error { + _, err := t.tx.Exec(`UPDATE client_store SET name = ?, domain = ?, sso = ?, active = ? WHERE subject = ? AND owner = ?`, name, domain, sso, active, subject.String(), owner.String()) return err } -func (t *Tx) ResetClientAppSecret(subject uuid.UUID, secret string) error { +func (t *Tx) ResetClientAppSecret(subject, owner uuid.UUID) (string, error) { secret, err := password.GenerateApiSecret(70) if err != nil { - return err + return "", err } - _, err = t.tx.Exec(`UPDATE client_store SET secret = ? WHERE subject = ?`, secret, subject.String()) - return err + _, err = t.tx.Exec(`UPDATE client_store SET secret = ? WHERE subject = ? AND owner = ?`, secret, subject.String(), owner.String()) + return secret, err } func (t *Tx) GetUserList(offset int) ([]User, error) { diff --git a/pages/manage-apps.go.html b/pages/manage-apps.go.html index 8c544af..c173655 100644 --- a/pages/manage-apps.go.html +++ b/pages/manage-apps.go.html @@ -2,6 +2,30 @@
Using an `@{{.Namespace}}` email address will automatically verify as it is owned by this login + service.