2024-01-19 22:04:25 +00:00
|
|
|
package middleware
|
2024-01-17 13:16:03 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-17 13:43:32 +00:00
|
|
|
"context"
|
2024-01-17 13:16:03 +00:00
|
|
|
"fmt"
|
|
|
|
"git.lumeweb.com/LumeWeb/portal/interfaces"
|
|
|
|
"github.com/golang-jwt/jwt/v5"
|
|
|
|
"go.sia.tech/jape"
|
|
|
|
"net/http"
|
2024-01-20 13:06:50 +00:00
|
|
|
"net/url"
|
2024-01-17 13:16:03 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2024-01-17 13:43:32 +00:00
|
|
|
const (
|
2024-01-19 22:04:25 +00:00
|
|
|
S5AuthUserIDKey = "userID"
|
|
|
|
S5AuthCookieName = "s5-auth-token"
|
|
|
|
S5AuthQueryParam = "auth_token"
|
2024-01-17 13:43:32 +00:00
|
|
|
)
|
|
|
|
|
2024-01-19 01:56:49 +00:00
|
|
|
func findAuthToken(r *http.Request) string {
|
2024-01-20 13:03:26 +00:00
|
|
|
authHeader := parseAuthTokenHeader(r.Header)
|
2024-01-19 01:56:49 +00:00
|
|
|
|
|
|
|
if authHeader != "" {
|
|
|
|
return authHeader
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, cookie := range r.Cookies() {
|
2024-01-19 22:04:25 +00:00
|
|
|
if cookie.Name == S5AuthCookieName {
|
2024-01-19 01:56:49 +00:00
|
|
|
return cookie.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:04:25 +00:00
|
|
|
return r.FormValue(S5AuthQueryParam)
|
2024-01-19 01:56:49 +00:00
|
|
|
}
|
|
|
|
|
2024-01-20 13:03:26 +00:00
|
|
|
func parseAuthTokenHeader(headers http.Header) string {
|
|
|
|
authHeader := headers.Get("Authorization")
|
|
|
|
if authHeader == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
authHeader = strings.TrimPrefix(authHeader, "Bearer ")
|
|
|
|
|
|
|
|
return authHeader
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:16:03 +00:00
|
|
|
func AuthMiddleware(handler jape.Handler, portal interfaces.Portal) jape.Handler {
|
|
|
|
return jape.Adapt(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2024-01-19 01:56:49 +00:00
|
|
|
authToken := findAuthToken(r)
|
|
|
|
|
|
|
|
if authToken == "" {
|
|
|
|
http.Error(w, "Invalid JWT", http.StatusUnauthorized)
|
2024-01-17 13:16:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-19 01:56:49 +00:00
|
|
|
token, err := jwt.Parse(authToken, func(token *jwt.Token) (interface{}, error) {
|
2024-01-17 13:16:03 +00:00
|
|
|
if _, ok := token.Method.(*jwt.SigningMethodEd25519); !ok {
|
|
|
|
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:53:10 +00:00
|
|
|
publicKey := portal.Identity().Public()
|
2024-01-17 13:16:03 +00:00
|
|
|
|
|
|
|
return publicKey, nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:43:32 +00:00
|
|
|
if claim, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
2024-01-17 14:02:13 +00:00
|
|
|
subject, ok := claim["sub"]
|
|
|
|
|
2024-01-17 13:57:45 +00:00
|
|
|
if !ok {
|
|
|
|
http.Error(w, "Invalid User ID", http.StatusBadRequest)
|
2024-01-17 13:43:32 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 14:02:13 +00:00
|
|
|
var userID uint64
|
|
|
|
|
|
|
|
switch v := subject.(type) {
|
|
|
|
case uint64:
|
|
|
|
userID = v
|
|
|
|
case float64:
|
|
|
|
userID = uint64(v)
|
|
|
|
default:
|
|
|
|
// Handle the case where userID is of an unexpected type
|
|
|
|
http.Error(w, "Invalid User ID", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-17 13:43:32 +00:00
|
|
|
exists, _ := portal.Accounts().AccountExists(userID)
|
|
|
|
|
|
|
|
if !exists {
|
|
|
|
http.Error(w, "Invalid User ID", http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:04:25 +00:00
|
|
|
ctx := context.WithValue(r.Context(), S5AuthUserIDKey, userID)
|
2024-01-17 13:43:32 +00:00
|
|
|
r = r.WithContext(ctx)
|
|
|
|
|
2024-01-17 13:16:03 +00:00
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
http.Error(w, "Invalid JWT", http.StatusUnauthorized)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})(handler)
|
|
|
|
}
|
2024-01-19 22:06:41 +00:00
|
|
|
|
2024-01-20 12:54:24 +00:00
|
|
|
type tusJwtResponseWriter struct {
|
|
|
|
http.ResponseWriter
|
2024-01-20 13:12:30 +00:00
|
|
|
req *http.Request
|
2024-01-20 12:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (w *tusJwtResponseWriter) WriteHeader(statusCode int) {
|
|
|
|
// Check if this is the specific route and status
|
|
|
|
if statusCode == http.StatusCreated {
|
2024-01-20 13:03:26 +00:00
|
|
|
location := w.Header().Get("Location")
|
2024-01-20 13:06:50 +00:00
|
|
|
authToken := parseAuthTokenHeader(w.Header())
|
|
|
|
|
|
|
|
if authToken != "" && location != "" {
|
|
|
|
|
|
|
|
parsedUrl, _ := url.Parse(location)
|
|
|
|
parsedUrl.Query().Set("auth_token", authToken)
|
|
|
|
|
|
|
|
w.Header().Set("Location", parsedUrl.String())
|
2024-01-20 13:03:26 +00:00
|
|
|
}
|
2024-01-20 12:54:24 +00:00
|
|
|
}
|
2024-01-20 13:06:50 +00:00
|
|
|
|
2024-01-20 12:54:24 +00:00
|
|
|
w.ResponseWriter.WriteHeader(statusCode)
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:06:41 +00:00
|
|
|
func BuildS5TusApi(portal interfaces.Portal) jape.Handler {
|
|
|
|
|
|
|
|
// Wrapper function for AuthMiddleware to fit the MiddlewareFunc signature
|
|
|
|
authMiddlewareFunc := func(h jape.Handler) jape.Handler {
|
|
|
|
return AuthMiddleware(h, portal)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a jape.Handler for your tusHandler
|
|
|
|
tusJapeHandler := func(c jape.Context) {
|
|
|
|
tusHandler := portal.Storage().Tus()
|
|
|
|
tusHandler.ServeHTTP(c.ResponseWriter, c.Request)
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolMiddleware := jape.Adapt(func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := context.WithValue(r.Context(), "protocol", "s5")
|
|
|
|
h.ServeHTTP(w, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2024-01-20 10:50:54 +00:00
|
|
|
stripPrefix := func(h jape.Handler) jape.Handler {
|
|
|
|
return jape.Adapt(func(h http.Handler) http.Handler {
|
2024-01-20 11:03:07 +00:00
|
|
|
return http.StripPrefix("/s5/upload/tus", h)
|
2024-01-20 10:50:54 +00:00
|
|
|
})(h)
|
|
|
|
}
|
|
|
|
|
2024-01-20 12:54:24 +00:00
|
|
|
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
|
2024-01-20 12:57:09 +00:00
|
|
|
if r.Method == http.MethodPost && r.URL.Path == "/s5/upload/tus" {
|
2024-01-20 13:12:30 +00:00
|
|
|
res = &tusJwtResponseWriter{ResponseWriter: w, req: r}
|
2024-01-20 12:54:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(res, r)
|
|
|
|
})
|
|
|
|
})(h)
|
|
|
|
}
|
|
|
|
|
2024-01-19 22:06:41 +00:00
|
|
|
// Apply the middlewares to the tusJapeHandler
|
2024-01-20 13:12:50 +00:00
|
|
|
tusHandler := ApplyMiddlewares(tusJapeHandler, stripPrefix, authMiddlewareFunc, injectJwt, protocolMiddleware)
|
2024-01-19 22:06:41 +00:00
|
|
|
|
|
|
|
return tusHandler
|
|
|
|
}
|