From e9db71f3b8f9195eea783ae6b2727b7c8d6473bc Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Fri, 19 Jan 2024 17:04:25 -0500 Subject: [PATCH] refactor: move middleware to its own package to prevent import cycles --- api/{s5/middleware.go => middleware/s5.go} | 14 +++++++------- api/s5/http.go | 19 ++++++++++--------- 2 files changed, 17 insertions(+), 16 deletions(-) rename api/{s5/middleware.go => middleware/s5.go} (88%) diff --git a/api/s5/middleware.go b/api/middleware/s5.go similarity index 88% rename from api/s5/middleware.go rename to api/middleware/s5.go index 3e24727..0402702 100644 --- a/api/s5/middleware.go +++ b/api/middleware/s5.go @@ -1,4 +1,4 @@ -package s5 +package middleware import ( "context" @@ -11,9 +11,9 @@ import ( ) const ( - AuthUserIDKey = "userID" - AuthCookieName = "s5-auth-token" - AuthQueryParam = "auth_token" + S5AuthUserIDKey = "userID" + S5AuthCookieName = "s5-auth-token" + S5AuthQueryParam = "auth_token" ) func findAuthToken(r *http.Request) string { @@ -29,12 +29,12 @@ func findAuthToken(r *http.Request) string { } for _, cookie := range r.Cookies() { - if cookie.Name == AuthCookieName { + if cookie.Name == S5AuthCookieName { return cookie.Value } } - return r.FormValue(AuthQueryParam) + return r.FormValue(S5AuthQueryParam) } func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler { @@ -90,7 +90,7 @@ func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler return } - ctx := context.WithValue(r.Context(), AuthUserIDKey, userID) + ctx := context.WithValue(r.Context(), S5AuthUserIDKey, userID) r = r.WithContext(ctx) h.ServeHTTP(w, r) diff --git a/api/s5/http.go b/api/s5/http.go index f756b78..d6536ef 100644 --- a/api/s5/http.go +++ b/api/s5/http.go @@ -15,6 +15,7 @@ import ( s5protocol "git.lumeweb.com/LumeWeb/libs5-go/protocol" s5storage "git.lumeweb.com/LumeWeb/libs5-go/storage" "git.lumeweb.com/LumeWeb/libs5-go/types" + "git.lumeweb.com/LumeWeb/portal/api/middleware" "git.lumeweb.com/LumeWeb/portal/db/models" "git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/protocols" @@ -160,7 +161,7 @@ func (h *HttpHandler) SmallFileUpload(jc jape.Context) { return } - err = h.portal.Accounts().PinByID(upload.ID, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64))) + err = h.portal.Accounts().PinByID(upload.ID, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64))) if err != nil { _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) h.portal.Logger().Error(errUploadingFile, zap.Error(err)) @@ -201,13 +202,13 @@ func (h *HttpHandler) SmallFileUpload(jc jape.Context) { h.portal.Logger().Info("CID", zap.String("cidStr", cidStr)) - upload, err := h.portal.Storage().CreateUpload(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(bufferSize), "s5") + upload, err := h.portal.Storage().CreateUpload(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(bufferSize), "s5") if err != nil { _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) h.portal.Logger().Error(errUploadingFile, zap.Error(err)) } - err = h.portal.Accounts().PinByID(upload.ID, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64))) + err = h.portal.Accounts().PinByID(upload.ID, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64))) if err != nil { _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) h.portal.Logger().Error(errUploadingFile, zap.Error(err)) @@ -547,7 +548,7 @@ func (h *HttpHandler) AccountLogin(jc jape.Context) { } func (h *HttpHandler) AccountInfo(jc jape.Context) { - _, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(AuthUserIDKey).(uint64)) + _, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)) info := &AccountInfoResponse{ Email: user.Email, @@ -567,7 +568,7 @@ func (h *HttpHandler) AccountInfo(jc jape.Context) { } func (h *HttpHandler) AccountStats(jc jape.Context) { - _, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(AuthUserIDKey).(uint64)) + _, user := h.portal.Accounts().AccountExists(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)) info := &AccountStatsResponse{ AccountInfoResponse: AccountInfoResponse{ @@ -605,7 +606,7 @@ func (h *HttpHandler) AccountPins(jc jape.Context) { h.portal.Logger().Error(errFailedToGetPins, zap.Error(err)) } - pins, err := h.portal.Accounts().AccountPins(jc.Request.Context().Value(AuthUserIDKey).(uint64), cursor) + pins, err := h.portal.Accounts().AccountPins(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64), cursor) if err != nil { errored(err) @@ -658,7 +659,7 @@ func (h *HttpHandler) AccountPinDelete(jc jape.Context) { hash := hex.EncodeToString(decodedCid.Hash.HashBytes()) - err = h.portal.Accounts().DeletePinByHash(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64))) + err = h.portal.Accounts().DeletePinByHash(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64))) if err != nil { errored(err) @@ -690,7 +691,7 @@ func (h *HttpHandler) AccountPin(jc jape.Context) { hash := hex.EncodeToString(decodedCid.Hash.HashBytes()) - err = h.portal.Accounts().PinByHash(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64))) + err = h.portal.Accounts().PinByHash(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64))) if err != nil { errored(err) @@ -778,7 +779,7 @@ func (h *HttpHandler) DirectoryUpload(jc jape.Context) { return } - upload, err := h.portal.Storage().CreateUpload(hash, uint(jc.Request.Context().Value(AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(fileHeader.Size), "s5") + upload, err := h.portal.Storage().CreateUpload(hash, uint(jc.Request.Context().Value(middleware.S5AuthUserIDKey).(uint64)), jc.Request.RemoteAddr, uint64(fileHeader.Size), "s5") if err != nil { errored(err)