Extract Grammaratur, Dregarnuhr, Geduldh, Mestionora, Ordoschnellic, Schutzaria, Kunstzeal and Seheweit behind compatible application contracts. Include accumulated editor resilience, navigation and theme UX, remote CLI, storage/schema and DR groundwork, tests and design/operations documentation. Verified: uncached full Go suite; go vet ./...; race tests for all eight components; 11 targeted Chromium tests; Node suites; 19 Python CLI tests; reproducible editor build. No push, release tag or deployment in this checkpoint.
112 lines
4.6 KiB
Go
112 lines
4.6 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"log"
|
|
"net/http"
|
|
"pageswiki/internal/store"
|
|
"strconv"
|
|
"time"
|
|
)
|
|
|
|
func ldapConfigFromForm(r *http.Request) store.AuthProviderConfig {
|
|
return store.AuthProviderConfig{
|
|
Kind: "ldap", Enabled: r.FormValue("enabled") == "on",
|
|
LDAPURL: r.FormValue("ldap_url"), LDAPBindDN: r.FormValue("ldap_bind_dn"), LDAPBindPassword: r.FormValue("ldap_bind_password"),
|
|
LDAPSearchBase: r.FormValue("ldap_search_base"), LDAPSearchFilter: r.FormValue("ldap_search_filter"),
|
|
LDAPStartTLS: r.FormValue("ldap_start_tls") == "on", LDAPSkipTLSVerify: r.FormValue("ldap_verify_tls") != "on", LDAPCAPEM: r.FormValue("ldap_ca_pem"),
|
|
LDAPUIDAttribute: r.FormValue("ldap_uid_attribute"), LDAPUsernameAttribute: r.FormValue("ldap_username_attribute"),
|
|
LDAPEmailAttribute: r.FormValue("ldap_email_attribute"), LDAPDisplayAttribute: r.FormValue("ldap_display_attribute"),
|
|
LDAPMatchBy: r.FormValue("ldap_match_by"), LDAPAutoProvision: r.FormValue("ldap_auto_provision") == "on",
|
|
}
|
|
}
|
|
|
|
func (s *Server) handleAuthAdmin(w http.ResponseWriter, r *http.Request) {
|
|
domainUUID := s.domainUUIDForRequest(r)
|
|
principal := s.requestPrincipal(r)
|
|
if principal == nil || !s.store.IsBuiltinGroupAdmin(domainUUID, principal.UUID, "domain_admin") {
|
|
http.Error(w, "domain admin required", http.StatusForbidden)
|
|
return
|
|
}
|
|
if r.Method == http.MethodPost {
|
|
if err := r.ParseForm(); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
panel := "auth"
|
|
switch r.FormValue("action") {
|
|
case "save_session_policy":
|
|
panel = "auth-sessions"
|
|
minutes, err := strconv.Atoi(r.FormValue("session_minutes"))
|
|
if err != nil || minutes < 5 || minutes > store.MaxAuthSessionMinutes {
|
|
http.Error(w, "session lifetime must be between 5 and 43200 minutes", http.StatusBadRequest)
|
|
return
|
|
}
|
|
if err := s.store.SetAuthSessionLifetimeMinutes(domainUUID, minutes); err != nil {
|
|
http.Error(w, "could not save session policy", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
case "revoke_session":
|
|
panel = "auth-sessions"
|
|
if err := s.store.RevokeAuthSessionByID(domainUUID, r.FormValue("session_uuid")); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
case "principal_state":
|
|
if err := s.store.SetPrincipalState(domainUUID, r.FormValue("principal_uuid"), r.FormValue("state")); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
case "create_account":
|
|
if _, err := s.store.CreatePasswordAccount(domainUUID, r.FormValue("username"), r.FormValue("password"), r.FormValue("display_name"), r.FormValue("email")); err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
case "save_oidc":
|
|
panel = "auth-providers"
|
|
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "oidc", Enabled: r.FormValue("enabled") == "on", Issuer: r.FormValue("issuer"), ClientID: r.FormValue("client_id"), ClientSecret: r.FormValue("client_secret"), OIDCMatchBy: r.FormValue("oidc_match_by")})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
case "save_smtp":
|
|
panel = "auth-providers"
|
|
err := s.store.SetAuthProviderConfig(domainUUID, store.AuthProviderConfig{Kind: "smtp", Enabled: r.FormValue("enabled") == "on", SMTPAddress: r.FormValue("smtp_address"), SMTPUsername: r.FormValue("smtp_username"), SMTPPassword: r.FormValue("smtp_password"), SMTPFrom: r.FormValue("smtp_from")})
|
|
if err != nil {
|
|
http.Error(w, err.Error(), 400)
|
|
return
|
|
}
|
|
case "save_ldap":
|
|
panel = "auth-providers"
|
|
if err := s.store.SetAuthProviderConfig(domainUUID, ldapConfigFromForm(r)); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
case "test_ldap":
|
|
panel = "auth-providers"
|
|
config, err := s.store.AuthProviderConfig(domainUUID, "ldap")
|
|
if err == nil && config.Enabled {
|
|
select {
|
|
case s.ldapSlots <- struct{}{}:
|
|
ctx, cancel := context.WithTimeout(r.Context(), 12*time.Second)
|
|
_, err = s.authenticateLDAPDirectory(ctx, config, r.FormValue("test_username"), r.FormValue("test_password"))
|
|
cancel()
|
|
<-s.ldapSlots
|
|
default:
|
|
err = context.DeadlineExceeded
|
|
}
|
|
}
|
|
if err != nil || !config.Enabled {
|
|
log.Printf("LDAP connection test failed domain=%s error=%v", domainUUID, err)
|
|
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=failed", http.StatusSeeOther)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/-/sys/config?panel=auth-providers&ldap_test=ok", http.StatusSeeOther)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/-/sys/config?panel="+panel+"&saved=1", http.StatusSeeOther)
|
|
return
|
|
}
|
|
http.Redirect(w, r, "/-/sys/config?panel=auth", http.StatusFound)
|
|
}
|