refactor: move middleware to its own package to prevent import cycles

This commit is contained in:
Derrick Hammer 2024-01-19 17:04:25 -05:00
parent 4c92750dd0
commit e9db71f3b8
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
2 changed files with 17 additions and 16 deletions

View File

@ -1,4 +1,4 @@
package s5 package middleware
import ( import (
"context" "context"
@ -11,9 +11,9 @@ import (
) )
const ( const (
AuthUserIDKey = "userID" S5AuthUserIDKey = "userID"
AuthCookieName = "s5-auth-token" S5AuthCookieName = "s5-auth-token"
AuthQueryParam = "auth_token" S5AuthQueryParam = "auth_token"
) )
func findAuthToken(r *http.Request) string { func findAuthToken(r *http.Request) string {
@ -29,12 +29,12 @@ func findAuthToken(r *http.Request) string {
} }
for _, cookie := range r.Cookies() { for _, cookie := range r.Cookies() {
if cookie.Name == AuthCookieName { if cookie.Name == S5AuthCookieName {
return cookie.Value return cookie.Value
} }
} }
return r.FormValue(AuthQueryParam) return r.FormValue(S5AuthQueryParam)
} }
func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler { 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 return
} }
ctx := context.WithValue(r.Context(), AuthUserIDKey, userID) ctx := context.WithValue(r.Context(), S5AuthUserIDKey, userID)
r = r.WithContext(ctx) r = r.WithContext(ctx)
h.ServeHTTP(w, r) h.ServeHTTP(w, r)

View File

@ -15,6 +15,7 @@ import (
s5protocol "git.lumeweb.com/LumeWeb/libs5-go/protocol" s5protocol "git.lumeweb.com/LumeWeb/libs5-go/protocol"
s5storage "git.lumeweb.com/LumeWeb/libs5-go/storage" s5storage "git.lumeweb.com/LumeWeb/libs5-go/storage"
"git.lumeweb.com/LumeWeb/libs5-go/types" "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/db/models"
"git.lumeweb.com/LumeWeb/portal/interfaces" "git.lumeweb.com/LumeWeb/portal/interfaces"
"git.lumeweb.com/LumeWeb/portal/protocols" "git.lumeweb.com/LumeWeb/portal/protocols"
@ -160,7 +161,7 @@ func (h *HttpHandler) SmallFileUpload(jc jape.Context) {
return 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 { if err != nil {
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
h.portal.Logger().Error(errUploadingFile, zap.Error(err)) 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)) 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 { if err != nil {
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
h.portal.Logger().Error(errUploadingFile, zap.Error(err)) 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 { if err != nil {
_ = jc.Error(errUploadingFileErr, http.StatusInternalServerError) _ = jc.Error(errUploadingFileErr, http.StatusInternalServerError)
h.portal.Logger().Error(errUploadingFile, zap.Error(err)) 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) { 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{ info := &AccountInfoResponse{
Email: user.Email, Email: user.Email,
@ -567,7 +568,7 @@ func (h *HttpHandler) AccountInfo(jc jape.Context) {
} }
func (h *HttpHandler) AccountStats(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{ info := &AccountStatsResponse{
AccountInfoResponse: AccountInfoResponse{ AccountInfoResponse: AccountInfoResponse{
@ -605,7 +606,7 @@ func (h *HttpHandler) AccountPins(jc jape.Context) {
h.portal.Logger().Error(errFailedToGetPins, zap.Error(err)) 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 { if err != nil {
errored(err) errored(err)
@ -658,7 +659,7 @@ func (h *HttpHandler) AccountPinDelete(jc jape.Context) {
hash := hex.EncodeToString(decodedCid.Hash.HashBytes()) 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 { if err != nil {
errored(err) errored(err)
@ -690,7 +691,7 @@ func (h *HttpHandler) AccountPin(jc jape.Context) {
hash := hex.EncodeToString(decodedCid.Hash.HashBytes()) 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 { if err != nil {
errored(err) errored(err)
@ -778,7 +779,7 @@ func (h *HttpHandler) DirectoryUpload(jc jape.Context) {
return 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 { if err != nil {
errored(err) errored(err)