portal/api/s5/middleware.go

81 lines
1.8 KiB
Go
Raw Normal View History

2024-01-17 13:16:03 +00:00
package s5
import (
"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"
"strings"
)
const (
AuthUserIDKey = "userID"
)
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) {
authHeader := r.Header.Get("Authorization")
if authHeader == "" {
http.Error(w, "Authorization header is required", http.StatusBadRequest)
return
}
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
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
}
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)
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
}
exists, _ := portal.Accounts().AccountExists(userID)
if !exists {
http.Error(w, "Invalid User ID", http.StatusBadRequest)
return
}
2024-01-17 14:09:48 +00:00
ctx := context.WithValue(r.Context(), AuthUserIDKey, userID)
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)
}