Skip to content

Commit

Permalink
feat: Add configurable CORS Headers
Browse files Browse the repository at this point in the history
  • Loading branch information
josiah-lunit committed Sep 21, 2023
1 parent 70d7a2c commit bc8c846
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
1 change: 1 addition & 0 deletions cmd/dex/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ type Web struct {
TLSCert string `json:"tlsCert"`
TLSKey string `json:"tlsKey"`
AllowedOrigins []string `json:"allowedOrigins"`
AllowedHeaders []string `json:"allowedHeaders"`
}

// Telemetry is the config format for telemetry including the HTTP server config.
Expand Down
1 change: 1 addition & 0 deletions cmd/dex/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,7 @@ func runServe(options serveOptions) error {
AlwaysShowLoginScreen: c.OAuth2.AlwaysShowLoginScreen,
PasswordConnector: c.OAuth2.PasswordConnector,
AllowedOrigins: c.Web.AllowedOrigins,
AllowedHeaders: c.Web.AllowedHeaders,
Issuer: c.Issuer,
Storage: s,
Web: c.Frontend,
Expand Down
11 changes: 7 additions & 4 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ type Config struct {
// domain.
AllowedOrigins []string

// List of allowed headers for CORS requests on discovery, token, and keys endpoint.
AllowedHeaders []string

// If enabled, the server won't prompt the user to approve authorization requests.
// Logging in implies approval.
SkipApprovalScreen bool
Expand Down Expand Up @@ -214,6 +217,9 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
if len(c.SupportedResponseTypes) == 0 {
c.SupportedResponseTypes = []string{responseTypeCode}
}
if len(c.AllowedHeaders) == 0 {
c.AllowedHeaders = []string{"Authorization"}
}

allSupportedGrants := map[string]bool{
grantTypeAuthorizationCode: true,
Expand Down Expand Up @@ -353,12 +359,9 @@ func newServer(ctx context.Context, c Config, rotationStrategy rotationStrategy)
handleWithCORS := func(p string, h http.HandlerFunc) {
var handler http.Handler = h
if len(c.AllowedOrigins) > 0 {
allowedHeaders := []string{
"Authorization",
}
cors := handlers.CORS(
handlers.AllowedOrigins(c.AllowedOrigins),
handlers.AllowedHeaders(allowedHeaders),
handlers.AllowedHeaders(c.AllowedHeaders),
)
handler = cors(handler)
}
Expand Down

0 comments on commit bc8c846

Please sign in to comment.