Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow CORS policy to be configured #484

Merged
merged 5 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions pkg/op/op.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,19 @@

type HttpInterceptor func(http.Handler) http.Handler

type corsOptioner interface {
CORSOptions() *cors.Options
}

func CreateRouter(o OpenIDProvider, interceptors ...HttpInterceptor) chi.Router {
router := chi.NewRouter()
router.Use(cors.New(defaultCORSOptions).Handler)
if co, ok := o.(corsOptioner); ok {
if opts := co.CORSOptions(); opts != nil {
router.Use(cors.New(*opts).Handler)
}
} else {
router.Use(cors.New(defaultCORSOptions).Handler)
}

Check warning on line 112 in pkg/op/op.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/op.go#L110-L112

Added lines #L110 - L112 were not covered by tests
router.Use(intercept(o.IssuerFromRequest, interceptors...))
router.HandleFunc(healthEndpoint, healthHandler)
router.HandleFunc(readinessEndpoint, readyHandler(o.Probes()))
Expand Down Expand Up @@ -224,6 +234,7 @@
storage: storage,
endpoints: DefaultEndpoints,
timer: make(<-chan time.Time),
corsOpts: &defaultCORSOptions,
logger: slog.Default(),
}

Expand Down Expand Up @@ -268,6 +279,7 @@
timer <-chan time.Time
accessTokenVerifierOpts []AccessTokenVerifierOpt
idTokenHintVerifierOpts []IDTokenHintVerifierOpt
corsOpts *cors.Options
logger *slog.Logger
}

Expand Down Expand Up @@ -427,6 +439,10 @@
}
}

func (o *Provider) CORSOptions() *cors.Options {
return o.corsOpts
}

func (o *Provider) Logger() *slog.Logger {
return o.logger
}
Expand Down Expand Up @@ -587,6 +603,13 @@
}
}

func WithCORSOptions(opts *cors.Options) Option {
return func(o *Provider) error {
o.corsOpts = opts
return nil
}

Check warning on line 610 in pkg/op/op.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/op.go#L606-L610

Added lines #L606 - L610 were not covered by tests
}

// WithLogger lets a logger other than slog.Default().
//
// EXPERIMENTAL: Will change to log/slog import after we drop support for Go 1.20
Expand All @@ -603,6 +626,6 @@
for i := len(interceptors) - 1; i >= 0; i-- {
handler = interceptors[i](handler)
}
return cors.New(defaultCORSOptions).Handler(issuerInterceptor.Handler(handler))
return issuerInterceptor.Handler(handler)
}
}
17 changes: 15 additions & 2 deletions pkg/op/server_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,19 @@
server: server,
endpoints: endpoints,
decoder: decoder,
corsOpts: &defaultCORSOptions,
logger: slog.Default(),
}
ws.router.Use(cors.New(defaultCORSOptions).Handler)

for _, option := range options {
option(ws)
}

ws.createRouter()
ws.handler = ws.router
if ws.corsOpts != nil {
ws.handler = cors.New(*ws.corsOpts).Handler(ws.router)
}
return ws
}

Expand Down Expand Up @@ -66,6 +70,13 @@
}
}

// WithServerCORSOptions sets the CORS policy for the Server's router.
func WithServerCORSOptions(opts *cors.Options) ServerOption {
return func(s *webServer) {
s.corsOpts = opts
}

Check warning on line 77 in pkg/op/server_http.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/server_http.go#L74-L77

Added lines #L74 - L77 were not covered by tests
}

// WithFallbackLogger overrides the fallback logger, which
// is used when no logger was found in the context.
// Defaults to [slog.Default].
Expand All @@ -78,13 +89,15 @@
type webServer struct {
server Server
router *chi.Mux
handler http.Handler
endpoints Endpoints
decoder httphelper.Decoder
corsOpts *cors.Options
logger *slog.Logger
}

func (s *webServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
s.router.ServeHTTP(w, r)
s.handler.ServeHTTP(w, r)
}

func (s *webServer) getLogger(ctx context.Context) *slog.Logger {
Expand Down
Loading