Files
patrickandClaude Sonnet 5.5 0df4696e8c
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (pull_request) Canceled after 0s
CI / Core-Modul (Build/Vet/Test) (push) Failing after 1m7s
CI / Web-Barrierefreiheitspruefung (axe, Tastatur, Reflow, Textabstaende) (push) Canceled after 0s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (push) Successful in 5s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (push) Successful in 5s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (push) Failing after 23s
CI / Web (dms-browser) (push) Failing after 1m34s
CI / Web (pdf-viewer) (push) Failing after 1m2s
CI / Web (shl) (push) Failing after 14s
CI / Core-Modul (Build/Vet/Test) (pull_request) Failing after 1m9s
CI / DMS Security-Test-Gate (AC5, Merge-Gate) (pull_request) Successful in 4s
CI / Connect Security-Test-Gate (QA-01 AC4, Merge-Gate) (pull_request) Successful in 4s
CI / DMS-Modul Supply-Chain-Scan (govulncheck) (pull_request) Failing after 21s
CI / Web (dms-browser) (pull_request) Failing after 1m30s
CI / Web (pdf-viewer) (pull_request) Failing after 1m2s
CI / Web (shl) (pull_request) Failing after 12s
Supply-Chain-Scan (Go) / govulncheck (push) Failing after 2m46s
Core-Schnittstellen-Vertragstests / contract-tests (pull_request) Successful in 2m49s
Supply-Chain-Scan (Go) / govulncheck (pull_request) Failing after 3m4s
Supply-Chain-Scan (npm) / npm-audit (pull_request) Failing after 2m6s
Core-Pflichttest-Gate / pflichttest-gate (pull_request) Successful in 2m39s
RBAC-Rollenhierarchie-Pflichtgate / rbac-policy-invariant (pull_request) Successful in 8m37s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (push) Failing after 3s
CI / DMS-Modul (Lint/Build/Test/Coverage/Artefakt) (pull_request) Failing after 2s
DB-Integrationstests / db-tests (pull_request) Canceled after 0s
dms: apierror, httpclient, tenantregistryclient aus common v0.1.0
Die common-Fassung von apierror enthaelt die 5xx-Behebung (Text verborgen,
Korrelations-ID). Der Waechtertest gegen http.DefaultClient liegt jetzt in
internal/archtest.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-27 23:23:56 +01:00

84 lines
2.3 KiB
Go

package moduleauth
import (
"context"
"errors"
"net/http"
"strings"
"gitea.perlbach24.de/scripte/nexarch/common/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
}