Signing in now starts at the sign-in screen and one button that hands the browser to Go Loose, which returns to the welcome page. Nothing redirects to the identity provider on its own any more: that fired on every anonymous visit and read as a redirect loop rather than as a choice to sign in. The password form remains only where the API reports that Go Loose login is not configured for the host, because otherwise the application is unreachable on a bare localhost. A person's picture is carried through from the Go Loose identity and rendered in the account menu and on the profile page, with their initials as the fallback. Two things make that fallback necessary rather than decorative: Go Loose retires a picture key when a picture is replaced, so a link cached in a browser can be dead, and most people have not uploaded one. The URL is deliberately not persisted locally, since a stored key would keep failing after the picture changed. The account menu is a disclosure rather than a hover menu: a real button with aria-expanded, Escape and outside clicks closing it, and focus returning to the trigger. It is not an ARIA menu, because its actions are a link and a button and menuitem roles would strip the link semantics from one of them for no gain. The account URL of the Go Loose identity is resolved on each sign-in and is read from the live identity provider every time, which is also what makes it correct after a picture change. Depends on the Go Loose revision that serves avatar_url through /connect/userinfo, so the picture URL reaches this application at all.
131 lines
3.6 KiB
Go
131 lines
3.6 KiB
Go
package golooseauth
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"crypto/x509"
|
|
"fmt"
|
|
"net"
|
|
"net/http"
|
|
"os"
|
|
"strings"
|
|
"time"
|
|
|
|
goloose "github.com/danyel/go-loose/client"
|
|
|
|
"github.com/danyel/go-guess/backend/internal/config"
|
|
)
|
|
|
|
const SessionCookie = "go_loose_user_session"
|
|
|
|
type Registry struct {
|
|
appDomain string
|
|
bySlug map[string]*goloose.BrowserAuth
|
|
}
|
|
|
|
func New(cfg config.GoLooseConfig) (*Registry, error) {
|
|
registry := &Registry{
|
|
appDomain: domain(cfg.AppDomain),
|
|
bySlug: map[string]*goloose.BrowserAuth{},
|
|
}
|
|
if len(cfg.Tenants) == 0 {
|
|
return registry, nil
|
|
}
|
|
authDomain := domain(cfg.AuthDomain)
|
|
if authDomain == "" {
|
|
return nil, fmt.Errorf("GO_LOOSE_AUTH_DOMAIN is required when client credentials are configured")
|
|
}
|
|
httpClient, err := client(cfg.CAFile)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for slug, tenant := range cfg.Tenants {
|
|
slug = domain(slug)
|
|
if slug == "" || strings.Contains(slug, ".") {
|
|
return nil, fmt.Errorf("invalid Go Loose tenant slug %q", slug)
|
|
}
|
|
auth, err := goloose.NewBrowserAuth(goloose.BrowserAuthConfig{
|
|
BaseURL: "https://" + slug + "." + authDomain,
|
|
ClientID: tenant.ClientID,
|
|
ClientSecret: tenant.ClientSecret,
|
|
RedirectURL: "https://" + slug + "." + registry.appDomain + "/api/auth/callback",
|
|
AfterLoginURL: "/",
|
|
AfterLogoutURL: "/",
|
|
LoginPath: "/api/auth/login",
|
|
CookieName: SessionCookie,
|
|
CookieSecure: true,
|
|
HTTPClient: httpClient,
|
|
})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("configure %s Go Loose login: %w", slug, err)
|
|
}
|
|
registry.bySlug[slug] = auth
|
|
}
|
|
return registry, nil
|
|
}
|
|
|
|
func (r *Registry) AppDomain() string {
|
|
if r == nil || r.appDomain == "" {
|
|
return config.DefaultAppDomain
|
|
}
|
|
return r.appDomain
|
|
}
|
|
|
|
// ForHost resolves the Go Loose client of the tenant that owns host. Ingress
|
|
// proxies may replace the request Host, so X-Forwarded-Host wins when present.
|
|
func (r *Registry) ForHost(host string) (*goloose.BrowserAuth, string, bool) {
|
|
if r == nil {
|
|
return nil, "", false
|
|
}
|
|
slug := TenantFromHost(host, r.appDomain)
|
|
auth, ok := r.bySlug[slug]
|
|
return auth, slug, ok
|
|
}
|
|
|
|
// Host returns the host that identifies the tenant of a request, preferring the
|
|
// proxy-supplied X-Forwarded-Host over the request Host.
|
|
func Host(r *http.Request) string {
|
|
if forwarded := strings.TrimSpace(r.Header.Get("X-Forwarded-Host")); forwarded != "" {
|
|
return forwarded
|
|
}
|
|
return r.Host
|
|
}
|
|
|
|
func TenantFromHost(host, appDomain string) string {
|
|
hostname := strings.ToLower(strings.TrimSuffix(strings.TrimSpace(host), "."))
|
|
if parsed, _, err := net.SplitHostPort(host); err == nil {
|
|
hostname = strings.ToLower(strings.TrimSuffix(parsed, "."))
|
|
}
|
|
suffix := "." + domain(appDomain)
|
|
if !strings.HasSuffix(hostname, suffix) {
|
|
return ""
|
|
}
|
|
slug := strings.TrimSuffix(hostname, suffix)
|
|
if slug == "" || strings.Contains(slug, ".") {
|
|
return ""
|
|
}
|
|
return slug
|
|
}
|
|
|
|
// domain normalizes a configured host suffix or tenant slug.
|
|
func domain(value string) string {
|
|
return strings.Trim(strings.ToLower(strings.TrimSpace(value)), ".")
|
|
}
|
|
|
|
func client(caFile string) (*http.Client, error) {
|
|
transport := http.DefaultTransport.(*http.Transport).Clone()
|
|
if caFile != "" {
|
|
pem, err := os.ReadFile(caFile)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("read Go Loose CA: %w", err)
|
|
}
|
|
pool, err := x509.SystemCertPool()
|
|
if err != nil || pool == nil {
|
|
pool = x509.NewCertPool()
|
|
}
|
|
if !pool.AppendCertsFromPEM(pem) {
|
|
return nil, fmt.Errorf("parse Go Loose CA %s", caFile)
|
|
}
|
|
transport.TLSClientConfig = &tls.Config{RootCAs: pool, MinVersion: tls.VersionTLS12}
|
|
}
|
|
return &http.Client{Timeout: 5 * time.Second, Transport: transport}, nil
|
|
}
|