From 071ee7c729a3bb7d95c4e181a329ce1bd3dca319 Mon Sep 17 00:00:00 2001 From: Conrad Hoffmann Date: Tue, 22 Feb 2022 11:44:37 +0100 Subject: [PATCH] Implement configurable auth providers New providers need to "register" their URL scheme of choice in auth.NewFromURL(). Implements: https://todo.sr.ht/~sircmpwn/tokidoki/1 --- auth/url.go | 22 ++++++++++++++++++++++ cmd/tokidoki/main.go | 13 +++++++++---- 2 files changed, 31 insertions(+), 4 deletions(-) create mode 100644 auth/url.go diff --git a/auth/url.go b/auth/url.go new file mode 100644 index 0000000..1f0fc0f --- /dev/null +++ b/auth/url.go @@ -0,0 +1,22 @@ +package auth + +import ( + "fmt" + "net/url" +) + +func NewFromURL(authURL string) (AuthProvider, error) { + u, err := url.Parse(authURL) + if err != nil { + return nil, fmt.Errorf("error parsing auth URL: %s", err.Error()) + } + + switch u.Scheme { + case "imap": + return NewIMAP(u.Host, false), nil + case "imaps": + return NewIMAP(u.Host, true), nil + default: + return nil, fmt.Errorf("no auth provider found for %s:// URL", u.Scheme) + } +} diff --git a/cmd/tokidoki/main.go b/cmd/tokidoki/main.go index 9574bbc..aa5c560 100644 --- a/cmd/tokidoki/main.go +++ b/cmd/tokidoki/main.go @@ -16,9 +16,12 @@ import ( func main() { var ( - addr string + addr string + authURL string ) flag.StringVar(&addr, "addr", ":8080", "listening address") + // TODO: make flag mandatory instead of default value + flag.StringVar(&authURL, "auth.url", "imaps://imap.migadu.com:993", "auth backend URL") flag.Parse() if len(flag.Args()) != 0 { @@ -29,8 +32,10 @@ func main() { mux := chi.NewRouter() mux.Use(middleware.Logger) - // TODO: Configurable - authProvider := auth.NewIMAP("imap.migadu.com:993", true) + authProvider, err := auth.NewFromURL(authURL) + if err != nil { + log.Fatalf("failed to load auth provider: %s", err.Error()) + } mux.Use(authProvider.Middleware()) backend := storage.NewPostgreSQL() @@ -41,7 +46,7 @@ func main() { Handler: mux, } log.Printf("Server running on %s", addr) - err := server.ListenAndServe() + err = server.ListenAndServe() if err != http.ErrServerClosed { log.Fatalf("ListenAndServe: %s", err.Error()) }