From a2051acff1c97cdbf8302483f0c2859bc9e2a6d9 Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Sat, 20 Jan 2024 07:54:24 -0500 Subject: [PATCH] feat: add initial tusJwtResponseWriter bones so we can append the auth_token to tus urls --- api/middleware/s5.go | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/api/middleware/s5.go b/api/middleware/s5.go index 5c7881f..9ded1c5 100644 --- a/api/middleware/s5.go +++ b/api/middleware/s5.go @@ -101,6 +101,18 @@ func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler })(handler) } +type tusJwtResponseWriter struct { + http.ResponseWriter +} + +func (w *tusJwtResponseWriter) WriteHeader(statusCode int) { + // Check if this is the specific route and status + if statusCode == http.StatusCreated { + // Modify the response or add query arguments as needed + } + w.ResponseWriter.WriteHeader(statusCode) +} + func BuildS5TusApi(portal interfaces.Portal) jape.Handler { // Wrapper function for AuthMiddleware to fit the MiddlewareFunc signature @@ -127,8 +139,21 @@ func BuildS5TusApi(portal interfaces.Portal) jape.Handler { })(h) } + injectJwt := func(h jape.Handler) jape.Handler { + return jape.Adapt(func(h http.Handler) http.Handler { + return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + res := w + if r.Method == http.MethodPatch || r.Method == http.MethodPost { + res = &tusJwtResponseWriter{ResponseWriter: w} + } + + h.ServeHTTP(res, r) + }) + })(h) + } + // Apply the middlewares to the tusJapeHandler - tusHandler := ApplyMiddlewares(tusJapeHandler, stripPrefix, authMiddlewareFunc, protocolMiddleware) + tusHandler := ApplyMiddlewares(tusJapeHandler, injectJwt, stripPrefix, authMiddlewareFunc, protocolMiddleware) return tusHandler }