MakeFedAPI makes an http.Handler that checks matrix federation authentication.
( metricsName string, serverName gomatrixserverlib.ServerName, keyRing gomatrixserverlib.JSONVerifier, wakeup *FederationWakeups, f func(*http.Request, *gomatrixserverlib.FederationRequest, map[string]string) util.JSONResponse, )
| 525 | |
| 526 | // MakeFedAPI makes an http.Handler that checks matrix federation authentication. |
| 527 | func MakeFedAPI( |
| 528 | metricsName string, |
| 529 | serverName gomatrixserverlib.ServerName, |
| 530 | keyRing gomatrixserverlib.JSONVerifier, |
| 531 | wakeup *FederationWakeups, |
| 532 | f func(*http.Request, *gomatrixserverlib.FederationRequest, map[string]string) util.JSONResponse, |
| 533 | ) http.Handler { |
| 534 | h := func(req *http.Request) util.JSONResponse { |
| 535 | fedReq, errResp := gomatrixserverlib.VerifyHTTPRequest( |
| 536 | req, time.Now(), serverName, keyRing, |
| 537 | ) |
| 538 | if fedReq == nil { |
| 539 | return errResp |
| 540 | } |
| 541 | // add the user to Sentry, if enabled |
| 542 | hub := sentry.GetHubFromContext(req.Context()) |
| 543 | if hub != nil { |
| 544 | hub.Scope().SetTag("origin", string(fedReq.Origin())) |
| 545 | hub.Scope().SetTag("uri", fedReq.RequestURI()) |
| 546 | } |
| 547 | defer func() { |
| 548 | if r := recover(); r != nil { |
| 549 | if hub != nil { |
| 550 | hub.CaptureException(fmt.Errorf("%s panicked", req.URL.Path)) |
| 551 | } |
| 552 | // re-panic to return the 500 |
| 553 | panic(r) |
| 554 | } |
| 555 | }() |
| 556 | go wakeup.Wakeup(req.Context(), fedReq.Origin()) |
| 557 | vars, err := httputil.URLDecodeMapValues(mux.Vars(req)) |
| 558 | if err != nil { |
| 559 | return util.MatrixErrorResponse(400, "M_UNRECOGNISED", "badly encoded query params") |
| 560 | } |
| 561 | |
| 562 | jsonRes := f(req, fedReq, vars) |
| 563 | // do not log 4xx as errors as they are client fails, not server fails |
| 564 | if hub != nil && jsonRes.Code >= 500 { |
| 565 | hub.Scope().SetExtra("response", jsonRes) |
| 566 | hub.CaptureException(fmt.Errorf("%s returned HTTP %d", req.URL.Path, jsonRes.Code)) |
| 567 | } |
| 568 | return jsonRes |
| 569 | } |
| 570 | return httputil.MakeExternalAPI(metricsName, h) |
| 571 | } |
| 572 | |
| 573 | type FederationWakeups struct { |
| 574 | FsAPI *fedInternal.FederationInternalAPI |