tokidoki/auth/url.go
2024-02-21 17:07:08 +01:00

41 lines
850 B
Go

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
case "pam":
return NewPAM()
case "file":
path := u.Path
if u.Host != "" {
path = u.Host + path
}
return NewHtpasswd(path)
case "null":
return NewNull()
case "http", "https":
if u.User == nil {
return nil, fmt.Errorf("missing client ID for OAuth 2.0")
}
clientID := u.User.Username()
clientSecret, _ := u.User.Password()
u.User = nil
return NewOAuth2(u.String(), clientID, clientSecret)
default:
return nil, fmt.Errorf("no auth provider found for %s:// URL", u.Scheme)
}
}