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 - Backport to v2 #485

Merged
merged 1 commit into from
Nov 16, 2023
Merged
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 @@ -90,9 +90,19 @@

type HttpInterceptor func(http.Handler) http.Handler

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

func CreateRouter(o OpenIDProvider, interceptors ...HttpInterceptor) *mux.Router {
router := mux.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 105 in pkg/op/op.go

View check run for this annotation

Codecov / codecov/patch

pkg/op/op.go#L103-L105

Added lines #L103 - L105 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 @@ -186,6 +196,7 @@
storage: storage,
endpoints: DefaultEndpoints,
timer: make(<-chan time.Time),
corsOpts: &defaultCORSOptions,
}

for _, optFunc := range opOpts {
Expand Down Expand Up @@ -229,6 +240,7 @@
timer <-chan time.Time
accessTokenVerifierOpts []AccessTokenVerifierOpt
idTokenHintVerifierOpts []IDTokenHintVerifierOpt
corsOpts *cors.Options
}

func (o *Provider) IssuerFromRequest(r *http.Request) string {
Expand Down Expand Up @@ -387,6 +399,10 @@
}
}

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

func (o *Provider) HttpHandler() http.Handler {
return o.httpHandler
}
Expand Down Expand Up @@ -534,12 +550,19 @@
}
}

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

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

View check run for this annotation

Codecov / codecov/patch

pkg/op/op.go#L553-L557

Added lines #L553 - L557 were not covered by tests
}

func intercept(i IssuerFromRequest, interceptors ...HttpInterceptor) func(handler http.Handler) http.Handler {
issuerInterceptor := NewIssuerInterceptor(i)
return func(handler http.Handler) http.Handler {
for i := len(interceptors) - 1; i >= 0; i-- {
handler = interceptors[i](handler)
}
return cors.New(defaultCORSOptions).Handler(issuerInterceptor.Handler(handler))
return issuerInterceptor.Handler(handler)
}
}