Skip to content

Commit

Permalink
Add cors handling to proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
arekkas authored and arekkas committed Nov 1, 2017
1 parent b776e05 commit 84cec15
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
8 changes: 7 additions & 1 deletion Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,7 @@
[[constraint]]
name = "gopkg.in/go-resty/resty.v0"
version = "0.13.0"

[[constraint]]
name = "github.com/rs/cors"
version = "1.2.0"
20 changes: 18 additions & 2 deletions cmd/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"net/http"
"net/http/httputil"
"net/url"

"os"
"strconv"
"strings"
"time"

"github.com/meatballhat/negroni-logrus"
Expand All @@ -14,6 +16,7 @@ import (
"github.com/ory/oathkeeper/director"
"github.com/ory/oathkeeper/evaluator"
"github.com/ory/oathkeeper/rule"
"github.com/rs/cors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/urfave/negroni"
Expand Down Expand Up @@ -63,10 +66,23 @@ var proxyCmd = &cobra.Command{
n.Use(negronilogrus.NewMiddlewareFromLogger(logger, "oahtkeeper-proxy"))
n.UseHandler(proxy)

allowCredentials, _ := strconv.ParseBool(os.Getenv("CORS_ALLOWED_CREDENTIALS"))
debug, _ := strconv.ParseBool(os.Getenv("CORS_DEBUG"))
maxAge, _ := strconv.Atoi(os.Getenv("CORS_MAX_AGE"))
ch := cors.New(cors.Options{
AllowedOrigins: strings.Split(os.Getenv("CORS_ALLOWED_ORIGINS"), ","),
AllowedMethods: strings.Split(os.Getenv("CORS_ALLOWED_METHODS"), ","),
AllowedHeaders: strings.Split(os.Getenv("CORS_ALLOWED_HEADERS"), ","),
ExposedHeaders: strings.Split(os.Getenv("CORS_EXPOSED_HEADERS"), ","),
AllowCredentials: allowCredentials,
MaxAge: maxAge,
Debug: debug,
}).Handler(n)

addr := fmt.Sprintf("%s:%s", viper.GetString("PROXY_HOST"), viper.GetString("PROXY_PORT"))
server := graceful.WithDefaults(&http.Server{
Addr: addr,
Handler: n,
Handler: ch,
})

logger.Printf("Listening on %s.\n", addr)
Expand Down

0 comments on commit 84cec15

Please sign in to comment.