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:
parent
5f8e94c4a1
commit
071ee7c729
22
auth/url.go
Normal file
22
auth/url.go
Normal 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)
|
||||
}
|
||||
}
|
@ -17,8 +17,11 @@ import (
|
||||
func main() {
|
||||
var (
|
||||
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())
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user