3
0

Moved CORS handler 1 level lower to catch all requests

Prior this, we failed to set CORS on invalid and expired JWT
This commit is contained in:
Denis Arh
2018-09-02 15:42:32 +02:00
parent 9707da5799
commit 2a55cc54c5
4 changed files with 27 additions and 21 deletions

View File

@@ -7,20 +7,9 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router)) {
// CORS for local development...
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
r.Use(cors.Handler)
if opts.http.logging {
r.Use(middleware.Logger)
}

View File

@@ -13,6 +13,7 @@ import (
"github.com/crusttech/crust/auth"
"github.com/crusttech/crust/crm/rest"
"github.com/go-chi/cors"
"github.com/titpetric/factory"
"github.com/titpetric/factory/resputil"
)
@@ -65,6 +66,7 @@ func Start() error {
}
r := chi.NewRouter()
r.Use(handleCORS)
// Only protect application routes with JWT
r.Group(func(r chi.Router) {
@@ -80,3 +82,14 @@ func Start() error {
return nil
}
// Sets up default CORS rules to use as a middleware
func handleCORS(next http.Handler) http.Handler {
return cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}).Handler(next)
}

View File

@@ -7,19 +7,10 @@ import (
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/go-chi/cors"
)
func mountRoutes(r chi.Router, opts *configuration, mounts ...func(r chi.Router)) {
// CORS for local development...
cors := cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
})
r.Use(cors.Handler)
r.Use(handleCORS)
if opts.http.logging {
r.Use(middleware.Logger)

View File

@@ -14,6 +14,7 @@ import (
"github.com/crusttech/crust/sam/rest"
"github.com/crusttech/crust/sam/websocket"
"github.com/go-chi/cors"
"github.com/titpetric/factory"
"github.com/titpetric/factory/resputil"
)
@@ -66,6 +67,7 @@ func Start() error {
}
r := chi.NewRouter()
r.Use(handleCORS)
// Only protect application routes with JWT
r.Group(func(r chi.Router) {
@@ -81,3 +83,14 @@ func Start() error {
return nil
}
// Sets up default CORS rules to use as a middleware
func handleCORS(next http.Handler) http.Handler {
return cors.New(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"},
AllowedHeaders: []string{"Accept", "Authorization", "Content-Type", "X-CSRF-Token"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}).Handler(next)
}