Skip to content

Commit

Permalink
feat: Allow CORS policy to be configured (#485)
Browse files Browse the repository at this point in the history
  • Loading branch information
korylprince authored Nov 16, 2023
1 parent b0be181 commit dd5b1ca
Showing 1 changed file with 25 additions and 2 deletions.
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 OpenIDProvider interface {

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)
}
router.Use(intercept(o.IssuerFromRequest, interceptors...))
router.HandleFunc(healthEndpoint, healthHandler)
router.HandleFunc(readinessEndpoint, readyHandler(o.Probes()))
Expand Down Expand Up @@ -186,6 +196,7 @@ func newProvider(config *Config, storage Storage, issuer func(bool) (IssuerFromR
storage: storage,
endpoints: DefaultEndpoints,
timer: make(<-chan time.Time),
corsOpts: &defaultCORSOptions,
}

for _, optFunc := range opOpts {
Expand Down Expand Up @@ -229,6 +240,7 @@ type Provider struct {
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) Probes() []ProbesFn {
}
}

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 WithIDTokenHintVerifierOpts(opts ...IDTokenHintVerifierOpt) Option {
}
}

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

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)
}
}

0 comments on commit dd5b1ca

Please sign in to comment.