From 0926bf93276341b130deadc933d8e7b33aa3e583 Mon Sep 17 00:00:00 2001 From: MrMelon54 Date: Mon, 29 Jan 2024 23:45:46 +0000 Subject: [PATCH] Start working on oauth controller --- .../purple-tulip}/pages/flow-callback.go.html | 0 .../pages/flow-popup-memory.go.html | 0 .../purple-tulip}/pages/flow-popup.go.html | 0 .../purple-tulip}/pages/pages.go | 0 cmd/purple-tulip/serve.go | 50 +++++++----- .../purple-tulip/server}/conf.go | 0 .../purple-tulip/server}/flow.go | 2 +- .../purple-tulip/server}/flow_test.go | 8 +- .../purple-tulip/server}/owners.go | 0 .../purple-tulip/server}/refresh.go | 4 +- .../purple-tulip/server}/server.go | 4 +- .../purple-tulip/server}/verify.go | 0 .../purple-tulip/server}/verify_test.go | 0 .../red-tulip/pages}/edit-otp.go.html | 0 .../red-tulip/pages}/edit-password.go.html | 0 .../red-tulip/pages}/edit.go.html | 0 .../red-tulip/pages}/index-guest.go.html | 0 .../red-tulip/pages}/index.go.html | 0 .../red-tulip/pages}/login-otp.go.html | 0 .../red-tulip/pages}/login.go.html | 0 .../red-tulip/pages}/manage-apps.go.html | 0 .../red-tulip/pages}/manage-users.go.html | 0 .../red-tulip/pages}/oauth-authorize.go.html | 0 {red-pages => cmd/red-tulip/pages}/pages.go | 4 +- .../red-tulip/pages}/pages_test.go | 2 +- .../red-tulip/pages}/remove-otp.go.html | 0 .../red-tulip/pages}/reset-password.go.html | 0 cmd/red-tulip/serve.go | 53 ++++++------- cmd/red-tulip/server/auth-source.go | 45 +++++++++++ {red-server => cmd/red-tulip/server}/auth.go | 2 +- .../red-tulip/server}/auth_test.go | 2 +- {red-server => cmd/red-tulip/server}/conf.go | 2 +- {red-server => cmd/red-tulip/server}/db.go | 2 +- {red-server => cmd/red-tulip/server}/edit.go | 6 +- {red-server => cmd/red-tulip/server}/home.go | 8 +- {red-server => cmd/red-tulip/server}/login.go | 8 +- {red-server => cmd/red-tulip/server}/mail.go | 6 +- .../red-tulip/server}/manage-apps.go | 6 +- .../red-tulip/server}/manage-users.go | 6 +- {red-server => cmd/red-tulip/server}/oauth.go | 37 +-------- {red-server => cmd/red-tulip/server}/otp.go | 10 +-- .../red-tulip/server}/server.go | 52 ++---------- .../red-tulip/server}/server_test.go | 2 +- mail/mail.go | 2 +- oauth/controller.go | 74 ++++++++++++++++++ oauth/jwt.go | 35 +++++++++ purple-test-client/index.html | 2 +- purple-tulip | Bin 0 -> 12233444 bytes red-tulip | Bin 0 -> 16487168 bytes 49 files changed, 264 insertions(+), 170 deletions(-) rename {purple-server => cmd/purple-tulip}/pages/flow-callback.go.html (100%) rename {purple-server => cmd/purple-tulip}/pages/flow-popup-memory.go.html (100%) rename {purple-server => cmd/purple-tulip}/pages/flow-popup.go.html (100%) rename {purple-server => cmd/purple-tulip}/pages/pages.go (100%) rename {purple-server => cmd/purple-tulip/server}/conf.go (100%) rename {purple-server => cmd/purple-tulip/server}/flow.go (98%) rename {purple-server => cmd/purple-tulip/server}/flow_test.go (98%) rename {purple-server => cmd/purple-tulip/server}/owners.go (100%) rename {purple-server => cmd/purple-tulip/server}/refresh.go (97%) rename {purple-server => cmd/purple-tulip/server}/server.go (94%) rename {purple-server => cmd/purple-tulip/server}/verify.go (100%) rename {purple-server => cmd/purple-tulip/server}/verify_test.go (100%) rename {red-pages => cmd/red-tulip/pages}/edit-otp.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/edit-password.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/edit.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/index-guest.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/index.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/login-otp.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/login.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/manage-apps.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/manage-users.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/oauth-authorize.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/pages.go (91%) rename {red-pages => cmd/red-tulip/pages}/pages_test.go (92%) rename {red-pages => cmd/red-tulip/pages}/remove-otp.go.html (100%) rename {red-pages => cmd/red-tulip/pages}/reset-password.go.html (100%) create mode 100644 cmd/red-tulip/server/auth-source.go rename {red-server => cmd/red-tulip/server}/auth.go (99%) rename {red-server => cmd/red-tulip/server}/auth_test.go (99%) rename {red-server => cmd/red-tulip/server}/conf.go (94%) rename {red-server => cmd/red-tulip/server}/db.go (97%) rename {red-server => cmd/red-tulip/server}/edit.go (94%) rename {red-server => cmd/red-tulip/server}/home.go (86%) rename {red-server => cmd/red-tulip/server}/login.go (96%) rename {red-server => cmd/red-tulip/server}/mail.go (97%) rename {red-server => cmd/red-tulip/server}/manage-apps.go (97%) rename {red-server => cmd/red-tulip/server}/manage-users.go (97%) rename {red-server => cmd/red-tulip/server}/oauth.go (80%) rename {red-server => cmd/red-tulip/server}/otp.go (94%) rename {red-server => cmd/red-tulip/server}/server.go (80%) rename {red-server => cmd/red-tulip/server}/server_test.go (98%) create mode 100644 oauth/controller.go create mode 100644 oauth/jwt.go create mode 100755 purple-tulip create mode 100755 red-tulip 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