diff --git a/purple-server/pages/flow-callback.go.html b/cmd/purple-tulip/pages/flow-callback.go.html similarity index 100% rename from purple-server/pages/flow-callback.go.html rename to cmd/purple-tulip/pages/flow-callback.go.html diff --git a/purple-server/pages/flow-popup-memory.go.html b/cmd/purple-tulip/pages/flow-popup-memory.go.html similarity index 100% rename from purple-server/pages/flow-popup-memory.go.html rename to cmd/purple-tulip/pages/flow-popup-memory.go.html diff --git a/purple-server/pages/flow-popup.go.html b/cmd/purple-tulip/pages/flow-popup.go.html similarity index 100% rename from purple-server/pages/flow-popup.go.html rename to cmd/purple-tulip/pages/flow-popup.go.html diff --git a/purple-server/pages/pages.go b/cmd/purple-tulip/pages/pages.go similarity index 100% rename from purple-server/pages/pages.go rename to cmd/purple-tulip/pages/pages.go diff --git a/cmd/purple-tulip/serve.go b/cmd/purple-tulip/serve.go index f6f74cf..bc3ab4b 100644 --- a/cmd/purple-tulip/serve.go +++ b/cmd/purple-tulip/serve.go @@ -11,8 +11,12 @@ import ( "errors" "flag" "github.com/1f349/mjwt" - "github.com/1f349/tulip/purple-server" - "github.com/1f349/tulip/purple-server/pages" + clientStore "github.com/1f349/tulip/client-store" + "github.com/1f349/tulip/cmd/purple-tulip/pages" + "github.com/1f349/tulip/cmd/purple-tulip/server" + "github.com/1f349/tulip/database" + "github.com/1f349/tulip/oauth" + "github.com/1f349/tulip/openid" "github.com/1f349/violet/utils" exitReload "github.com/MrMelon54/exit-reload" "github.com/google/subcommands" @@ -38,10 +42,10 @@ func (s *serveCmd) Usage() string { } func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) subcommands.ExitStatus { - log.Println("[Lavender] Starting...") + log.Println("[PurpleTulip] Starting...") if s.configPath == "" { - log.Println("[Lavender] Error: config flag is missing") + log.Println("[PurpleTulip] Error: config flag is missing") return subcommands.ExitUsageError } @@ -49,45 +53,53 @@ func (s *serveCmd) Execute(_ context.Context, _ *flag.FlagSet, _ ...interface{}) err := loadConfig(s.configPath, &conf) if err != nil { if os.IsNotExist(err) { - log.Println("[Lavender] Error: missing config file") + log.Println("[PurpleTulip] Error: missing config file") } else { - log.Println("[Lavender] Error: loading config file: ", err) + log.Println("[PurpleTulip] Error: loading config file: ", err) } return subcommands.ExitFailure } configPathAbs, err := filepath.Abs(s.configPath) if err != nil { - log.Fatal("[Lavender] Failed to get absolute config path") + log.Fatal("[PurpleTulip] Failed to get absolute config path") } wd := filepath.Dir(configPathAbs) - mSign, err := mjwt.NewMJwtSignerFromFileOrCreate(conf.Issuer, filepath.Join(wd, "lavender.private.key"), rand.Reader, 4096) + signer, err := mjwt.NewMJwtSignerFromFileOrCreate(conf.Issuer, filepath.Join(wd, "purple-tulip.private.key.pem"), rand.Reader, 4096) if err != nil { - log.Fatal("[Lavender] Failed to load or create MJWT signer:", err) + log.Fatal("[PurpleTulip] Failed to load or create MJWT signer:", err) + } + saveMjwtPubKey(signer, wd) + + db, err := database.Open(filepath.Join(wd, "purple-tulip.db.sqlite")) + if err != nil { + log.Fatal("[PurpleTulip] Failed to open database:", err) } - saveMjwtPubKey(mSign, wd) if err := pages.LoadPages(wd); err != nil { - log.Fatal("[Lavender] Failed to load page templates:", err) + log.Fatal("[PurpleTulip] Failed to load page templates:", err) } - srv := server.NewHttpServer(conf, mSign) - log.Printf("[Lavender] Starting HTTP red-server on '%s'\n", srv.Server.Addr) + openIdConf := openid.GenConfig(conf.BaseUrl, []string{"openid", "name", "username", "profile", "email", "birthdate", "age", "zoneinfo", "locale"}, []string{"sub", "name", "preferred_username", "profile", "picture", "website", "email", "email_verified", "gender", "birthdate", "zoneinfo", "locale", "updated_at"}) + controller := oauth.NewOAuthController(signer, &server.PurpleAuthSource{DB: db}, clientStore.New(db), openIdConf) + + srv := server.server.NewHttpServer(conf, db, controller, signer) + log.Printf("[PurpleTulip] Starting HTTP server on '%s'\n", srv.Server.Addr) go utils.RunBackgroundHttp("HTTP", srv.Server) - exitReload.ExitReload("Lavender", func() { + exitReload.ExitReload("PurpleTulip", func() { var conf server.Conf err := loadConfig(s.configPath, &conf) if err != nil { - log.Println("[Lavender] Failed to read config:", err) + log.Println("[PurpleTulip] Failed to read config:", err) } err = srv.UpdateConfig(conf) if err != nil { - log.Println("[Lavender] Failed to reload config:", err) + log.Println("[PurpleTulip] Failed to reload config:", err) } }, func() { - // stop http red-server + // stop http server _ = srv.Server.Close() }) @@ -108,10 +120,10 @@ func saveMjwtPubKey(mSign mjwt.Signer, wd string) { b := new(bytes.Buffer) err := pem.Encode(b, &pem.Block{Type: "RSA PUBLIC KEY", Bytes: pubKey}) if err != nil { - log.Fatal("[Lavender] Failed to encode MJWT public key:", err) + log.Fatal("[PurpleTulip] Failed to encode MJWT public key:", err) } err = os.WriteFile(filepath.Join(wd, "lavender.public.key"), b.Bytes(), 0600) if err != nil && !errors.Is(err, os.ErrExist) { - log.Fatal("[Lavender] Failed to save MJWT public key:", err) + log.Fatal("[PurpleTulip] Failed to save MJWT public key:", err) } } diff --git a/purple-server/conf.go b/cmd/purple-tulip/server/conf.go similarity index 100% rename from purple-server/conf.go rename to cmd/purple-tulip/server/conf.go diff --git a/purple-server/flow.go b/cmd/purple-tulip/server/flow.go similarity index 98% rename from purple-server/flow.go rename to cmd/purple-tulip/server/flow.go index 2e11587..643ea9e 100644 --- a/purple-server/flow.go +++ b/cmd/purple-tulip/server/flow.go @@ -4,8 +4,8 @@ import ( "context" _ "embed" "fmt" + "github.com/1f349/tulip/cmd/purple-tulip/pages" "github.com/1f349/tulip/issuer" - "github.com/1f349/tulip/purple-server/pages" "github.com/google/uuid" "github.com/julienschmidt/httprouter" "golang.org/x/oauth2" diff --git a/purple-server/flow_test.go b/cmd/purple-tulip/server/flow_test.go similarity index 98% rename from purple-server/flow_test.go rename to cmd/purple-tulip/server/flow_test.go index a9ba8b7..9c296aa 100644 --- a/purple-server/flow_test.go +++ b/cmd/purple-tulip/server/flow_test.go @@ -9,8 +9,8 @@ import ( "fmt" "github.com/1f349/cache" "github.com/1f349/mjwt" + "github.com/1f349/tulip/cmd/purple-tulip/pages" "github.com/1f349/tulip/issuer" - "github.com/1f349/tulip/purple-server/pages" "github.com/1f349/tulip/utils" "github.com/google/uuid" "github.com/julienschmidt/httprouter" @@ -72,7 +72,7 @@ var testHttpServer = HttpServer{ func init() { testHttpServer.conf.Store(&Conf{ BaseUrl: lavenderDomain, - ServiceName: "Test Lavender Service", + ServiceName: "Test Purple Tulip Service", }) testHttpServer.manager.Store(testManager) testHttpServer.services.Store(&map[string]AllowedClient{ @@ -353,7 +353,7 @@ func TestFlowCallback(t *testing.T) { const p1 = ` - Test Lavender Service + Test Purple Tulip Service