Files
nexarch/dms/internal/moduleauth/middleware.go
patrickandClaude Sonnet 5 403ceb6b61
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Canceled after 0s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Canceled after 0s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Canceled after 0s
CI / Web (dms-browser) (push) Canceled after 0s
CI / Web (pdf-viewer) (push) Canceled after 0s
CI / Web (shl) (push) Canceled after 0s
Supply-Chain-Scan (Go) / govulncheck (push) Canceled after 0s
DMS: Token-Rolle tenant_admin gilt im Guard als Admin (Core-Token-Modus); Smoke: Admin als Lebenszyklus-Akteur, Rollenpruefung R5
Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-22 00:21:35 +02:00

84 lines
2.3 KiB
Go

package moduleauth
import (
"context"
"errors"
"net/http"
"strings"
"gitea.perlbach24.de/scripte/nexarch/dms/internal/apierror"
)
type Identity struct {
UserID string
Tenant string
Role string
}
type identityKey struct{}
func WithIdentity(ctx context.Context, id Identity) context.Context {
return context.WithValue(ctx, identityKey{}, id)
}
func IdentityFrom(ctx context.Context) (Identity, bool) {
id, ok := ctx.Value(identityKey{}).(Identity)
return id, ok
}
type Config struct {
Verifier *Verifier
Prefix string
// AllowCaller laesst Anfragen ohne Bearer-Token mit der Interimsidentitaet
// (caller/X-User-Id) durch; ein vorhandenes Token wird immer geprueft.
AllowCaller bool
// Public sind Pfade unterhalb von Prefix ohne Nutzeridentitaet.
Public map[string]bool
}
func Authenticate(cfg Config, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
rest, underPrefix := strings.CutPrefix(r.URL.Path, cfg.Prefix)
if !underPrefix || cfg.Public[rest] {
next.ServeHTTP(w, r)
return
}
token, hasBearer := bearer(r)
if !hasBearer {
if cfg.AllowCaller && (r.URL.Query().Get("caller") != "" || r.Header.Get("X-User-Id") != "") {
next.ServeHTTP(w, r)
return
}
apierror.Write(w, http.StatusUnauthorized, "unauthenticated", "nicht angemeldet")
return
}
if cfg.Verifier == nil {
apierror.Write(w, http.StatusUnauthorized, "unauthenticated", "nicht angemeldet")
return
}
claims, err := cfg.Verifier.Verify(r.Context(), token)
if err != nil {
if errors.Is(err, ErrInvalidToken) {
apierror.Write(w, http.StatusUnauthorized, "unauthenticated", "nicht angemeldet")
} else {
apierror.Write(w, http.StatusServiceUnavailable, "unavailable", "identitaetsdienst nicht erreichbar")
}
return
}
q := r.URL.Query()
q.Set("caller", claims.Subject)
r2 := r.Clone(context.WithValue(r.Context(), identityKey{}, Identity{UserID: claims.Subject, Tenant: claims.Tenant, Role: claims.Role}))
r2.URL.RawQuery = q.Encode()
r2.Header.Set("X-User-Id", claims.Subject)
next.ServeHTTP(w, r2)
})
}
func bearer(r *http.Request) (string, bool) {
h := r.Header.Get("Authorization")
if len(h) > 7 && strings.EqualFold(h[:7], "bearer ") {
return strings.TrimSpace(h[7:]), true
}
return "", false
}