Fix linter issues, rename functions

This commit is contained in:
Till Faelligen 2024-08-16 12:14:23 +02:00
parent 919208be7a
commit 6f17931740
No known key found for this signature in database
GPG key ID: ACCDC9606D472758
7 changed files with 15 additions and 16 deletions

View file

@ -733,7 +733,7 @@ func Setup(
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions) ).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)
v3mux.Handle("/auth/{authType}/fallback/web", v3mux.Handle("/auth/{authType}/fallback/web",
httputil.MakeHTMLAPI("auth_fallback", userAPI, enableMetrics, func(w http.ResponseWriter, req *http.Request) { httputil.MakeHTTPAPI("auth_fallback", userAPI, enableMetrics, func(w http.ResponseWriter, req *http.Request) {
vars := mux.Vars(req) vars := mux.Vars(req)
AuthFallback(w, req, vars["authType"], cfg) AuthFallback(w, req, vars["authType"], cfg)
}), }),

View file

@ -679,8 +679,8 @@ func MakeFedAPI(
return httputil.MakeExternalAPI(metricsName, h) return httputil.MakeExternalAPI(metricsName, h)
} }
// MakeFedHTMLAPI makes an http.Handler that checks matrix federation authentication. // MakeFedHTTPAPI makes an http.Handler that checks matrix federation authentication.
func MakeFedHTMLAPI( func MakeFedHTTPAPI(
serverName spec.ServerName, serverName spec.ServerName,
isLocalServerName func(spec.ServerName) bool, isLocalServerName func(spec.ServerName) bool,
keyRing gomatrixserverlib.JSONVerifier, keyRing gomatrixserverlib.JSONVerifier,

View file

@ -59,7 +59,7 @@ func WithAllowGuests() AuthAPIOption {
} }
} }
// WithAuth is an option to MakeHTMLAPI to add authentication. // WithAuth is an option to MakeHTTPAPI to add authentication.
func WithAuth() AuthAPIOption { func WithAuth() AuthAPIOption {
return func(opts *AuthAPIOpts) { return func(opts *AuthAPIOpts) {
opts.WithAuth = true opts.WithAuth = true
@ -206,9 +206,9 @@ func MakeExternalAPI(metricsName string, f func(*http.Request) util.JSONResponse
return http.HandlerFunc(withSpan) return http.HandlerFunc(withSpan)
} }
// MakeHTMLAPI adds Span metrics to the HTML Handler function // MakeHTTPAPI adds Span metrics to the HTML Handler function
// This is used to serve HTML alongside JSON error messages // This is used to serve HTML alongside JSON error messages
func MakeHTMLAPI(metricsName string, userAPI userapi.QueryAcccessTokenAPI, enableMetrics bool, f func(http.ResponseWriter, *http.Request), checks ...AuthAPIOption) http.Handler { func MakeHTTPAPI(metricsName string, userAPI userapi.QueryAcccessTokenAPI, enableMetrics bool, f func(http.ResponseWriter, *http.Request), checks ...AuthAPIOption) http.Handler {
withSpan := func(w http.ResponseWriter, req *http.Request) { withSpan := func(w http.ResponseWriter, req *http.Request) {
trace, ctx := internal.StartTask(req.Context(), metricsName) trace, ctx := internal.StartTask(req.Context(), metricsName)
defer trace.EndTask() defer trace.EndTask()
@ -224,7 +224,6 @@ func MakeHTMLAPI(metricsName string, userAPI userapi.QueryAcccessTokenAPI, enabl
logger := util.GetLogger(req.Context()) logger := util.GetLogger(req.Context())
_, jsonErr := auth.VerifyUserFromRequest(req, userAPI) _, jsonErr := auth.VerifyUserFromRequest(req, userAPI)
if jsonErr != nil { if jsonErr != nil {
logger.Debugf("VerifyUserFromRequest %s -> HTTP %d", req.RemoteAddr, jsonErr.Code)
w.WriteHeader(jsonErr.Code) w.WriteHeader(jsonErr.Code)
if err := json.NewEncoder(w).Encode(jsonErr.JSON); err != nil { if err := json.NewEncoder(w).Encode(jsonErr.JSON); err != nil {
logger.WithError(err).Error("failed to encode JSON response") logger.WithError(err).Error("failed to encode JSON response")

View file

@ -218,5 +218,5 @@ func assertNoError(t *testing.T, err error, msg string) {
if err == nil { if err == nil {
return return
} }
t.Fatalf(msg) t.Fatal(msg)
} }

View file

@ -105,20 +105,20 @@ func Setup(
).Methods(http.MethodGet, http.MethodOptions) ).Methods(http.MethodGet, http.MethodOptions)
// v1 client endpoints requiring auth // v1 client endpoints requiring auth
downloadHandlerAuthed := httputil.MakeHTMLAPI("download", userAPI, cfg.Global.Metrics.Enabled, makeDownloadAPI("download_authed_client", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, false), httputil.WithAuth()) downloadHandlerAuthed := httputil.MakeHTTPAPI("download", userAPI, cfg.Global.Metrics.Enabled, makeDownloadAPI("download_authed_client", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, false), httputil.WithAuth())
v1mux.Handle("/config", configHandler).Methods(http.MethodGet, http.MethodOptions) v1mux.Handle("/config", configHandler).Methods(http.MethodGet, http.MethodOptions)
v1mux.Handle("/download/{serverName}/{mediaId}", downloadHandlerAuthed).Methods(http.MethodGet, http.MethodOptions) v1mux.Handle("/download/{serverName}/{mediaId}", downloadHandlerAuthed).Methods(http.MethodGet, http.MethodOptions)
v1mux.Handle("/download/{serverName}/{mediaId}/{downloadName}", downloadHandlerAuthed).Methods(http.MethodGet, http.MethodOptions) v1mux.Handle("/download/{serverName}/{mediaId}/{downloadName}", downloadHandlerAuthed).Methods(http.MethodGet, http.MethodOptions)
v1mux.Handle("/thumbnail/{serverName}/{mediaId}", v1mux.Handle("/thumbnail/{serverName}/{mediaId}",
httputil.MakeHTMLAPI("thumbnail", userAPI, cfg.Global.Metrics.Enabled, makeDownloadAPI("thumbnail_authed_client", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, false), httputil.WithAuth()), httputil.MakeHTTPAPI("thumbnail", userAPI, cfg.Global.Metrics.Enabled, makeDownloadAPI("thumbnail_authed_client", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, false), httputil.WithAuth()),
).Methods(http.MethodGet, http.MethodOptions) ).Methods(http.MethodGet, http.MethodOptions)
// same, but for federation // same, but for federation
v1fedMux.Handle("/download/{mediaId}", routing.MakeFedHTMLAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing, v1fedMux.Handle("/download/{mediaId}", routing.MakeFedHTTPAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
makeDownloadAPI("download_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true), makeDownloadAPI("download_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
)).Methods(http.MethodGet, http.MethodOptions) )).Methods(http.MethodGet, http.MethodOptions)
v1fedMux.Handle("/thumbnail/{mediaId}", routing.MakeFedHTMLAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing, v1fedMux.Handle("/thumbnail/{mediaId}", routing.MakeFedHTTPAPI(cfg.Global.ServerName, cfg.Global.IsLocalServerName, keyRing,
makeDownloadAPI("thumbnail_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true), makeDownloadAPI("thumbnail_authed_federation", &cfg.MediaAPI, rateLimits, db, client, federationClient, activeRemoteRequests, activeThumbnailGeneration, true),
)).Methods(http.MethodGet, http.MethodOptions) )).Methods(http.MethodGet, http.MethodOptions)
} }

View file

@ -29,11 +29,11 @@ func TestOpenACLsWithBlacklist(t *testing.T) {
roomID := "!test:test.com" roomID := "!test:test.com"
allowRegex, err := compileACLRegex("*") allowRegex, err := compileACLRegex("*")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal(err)
} }
denyRegex, err := compileACLRegex("foo.com") denyRegex, err := compileACLRegex("foo.com")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal(err)
} }
acls := ServerACLs{ acls := ServerACLs{
@ -72,7 +72,7 @@ func TestDefaultACLsWithWhitelist(t *testing.T) {
roomID := "!test:test.com" roomID := "!test:test.com"
allowRegex, err := compileACLRegex("foo.com") allowRegex, err := compileACLRegex("foo.com")
if err != nil { if err != nil {
t.Fatalf(err.Error()) t.Fatal(err)
} }
acls := ServerACLs{ acls := ServerACLs{

View file

@ -196,7 +196,7 @@ func (a *UserInternalAPI) QueryDeviceMessages(ctx context.Context, req *api.Quer
if m.StreamID > maxStreamID { if m.StreamID > maxStreamID {
maxStreamID = m.StreamID maxStreamID = m.StreamID
} }
if m.KeyJSON == nil || len(m.KeyJSON) == 0 { if len(m.KeyJSON) == 0 {
continue continue
} }
result = append(result, m) result = append(result, m)