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
This commit is contained in:
Conrad Hoffmann 2022-02-22 11:44:37 +01:00
parent 5f8e94c4a1
commit 071ee7c729
2 changed files with 31 additions and 4 deletions

22
auth/url.go Normal file
View File

@ -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)
}
}

View File

@ -17,8 +17,11 @@ import (
func main() { func main() {
var ( var (
addr string addr string
authURL string
) )
flag.StringVar(&addr, "addr", ":8080", "listening address") 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() flag.Parse()
if len(flag.Args()) != 0 { if len(flag.Args()) != 0 {
@ -29,8 +32,10 @@ func main() {
mux := chi.NewRouter() mux := chi.NewRouter()
mux.Use(middleware.Logger) mux.Use(middleware.Logger)
// TODO: Configurable authProvider, err := auth.NewFromURL(authURL)
authProvider := auth.NewIMAP("imap.migadu.com:993", true) if err != nil {
log.Fatalf("failed to load auth provider: %s", err.Error())
}
mux.Use(authProvider.Middleware()) mux.Use(authProvider.Middleware())
backend := storage.NewPostgreSQL() backend := storage.NewPostgreSQL()
@ -41,7 +46,7 @@ func main() {
Handler: mux, Handler: mux,
} }
log.Printf("Server running on %s", addr) log.Printf("Server running on %s", addr)
err := server.ListenAndServe() err = server.ListenAndServe()
if err != http.ErrServerClosed { if err != http.ErrServerClosed {
log.Fatalf("ListenAndServe: %s", err.Error()) log.Fatalf("ListenAndServe: %s", err.Error())
} }