package middleware import ( "context" "log" "net/http" "github.com/Wacky404/rpserver/internal/auth" ) type key string const claimsKey key = "claims" type cookies []string var AdmitCookies cookies = []string{"rp_clearance"} func JWT(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { claims, err := auth.VerifyRequest(r) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } ctx := context.WithValue(r.Context(), claimsKey, claims) next.ServeHTTP(w, r.WithContext(ctx)) }) } func Cookies(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { _, err := r.Cookie(AdmitCookies[0]) if err != nil { http.Error(w, "Unauthorized", http.StatusUnauthorized) return } // c1String := c1.Value // need db first to retrieve stored sessionid next.ServeHTTP(w, nil) }) } func Recover(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { defer func() { if err := recover(); err != nil { log.Printf("Recovered from panic: %v", err) http.Error(w, "Internal Server Error", http.StatusInternalServerError) } }() next.ServeHTTP(w, r) }) }