diff options
author | Till <2353100+S7evinK@users.noreply.github.com> | 2024-08-16 12:37:59 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-08-16 12:37:59 +0200 |
commit | 7a4ef240fc8ec97ba957933de3a80e611ad7d1f5 (patch) | |
tree | c8946995640907a3ea6e64a8a0509a23b696c69e /federationapi/routing/routing.go | |
parent | 8c6cf51b8f6dd0f34ecc0f0b38d5475e2055a297 (diff) |
Implement MSC3916 (#3397)
Needs https://github.com/matrix-org/gomatrixserverlib/pull/437
Diffstat (limited to 'federationapi/routing/routing.go')
-rw-r--r-- | federationapi/routing/routing.go | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/federationapi/routing/routing.go b/federationapi/routing/routing.go index 6328d165..91718efd 100644 --- a/federationapi/routing/routing.go +++ b/federationapi/routing/routing.go @@ -16,6 +16,7 @@ package routing import ( "context" + "encoding/json" "fmt" "net/http" "sync" @@ -678,6 +679,53 @@ func MakeFedAPI( return httputil.MakeExternalAPI(metricsName, h) } +// MakeFedHTTPAPI makes an http.Handler that checks matrix federation authentication. +func MakeFedHTTPAPI( + serverName spec.ServerName, + isLocalServerName func(spec.ServerName) bool, + keyRing gomatrixserverlib.JSONVerifier, + f func(http.ResponseWriter, *http.Request), +) http.Handler { + h := func(w http.ResponseWriter, req *http.Request) { + fedReq, errResp := fclient.VerifyHTTPRequest( + req, time.Now(), serverName, isLocalServerName, keyRing, + ) + + enc := json.NewEncoder(w) + logger := util.GetLogger(req.Context()) + if fedReq == nil { + + logger.Debugf("VerifyUserFromRequest %s -> HTTP %d", req.RemoteAddr, errResp.Code) + w.WriteHeader(errResp.Code) + if err := enc.Encode(errResp); err != nil { + logger.WithError(err).Error("failed to encode JSON response") + } + return + } + // add the user to Sentry, if enabled + hub := sentry.GetHubFromContext(req.Context()) + if hub != nil { + // clone the hub, so we don't send garbage events with e.g. mismatching rooms/event_ids + hub = hub.Clone() + hub.Scope().SetTag("origin", string(fedReq.Origin())) + hub.Scope().SetTag("uri", fedReq.RequestURI()) + } + defer func() { + if r := recover(); r != nil { + if hub != nil { + hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path)) + } + // re-panic to return the 500 + panic(r) + } + }() + + f(w, req) + } + + return http.HandlerFunc(h) +} + type FederationWakeups struct { FsAPI *fedInternal.FederationInternalAPI origins sync.Map |