Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cors: add options cors middleware handler
Browse files Browse the repository at this point in the history
JiaLiPassion committed Oct 25, 2018
1 parent e876b28 commit f983afd
Showing 2 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jwk/handler.go
Original file line number Diff line number Diff line change
@@ -72,6 +72,7 @@ func (h *Handler) GetGenerators() map[string]KeyGenerator {
}

func (h *Handler) SetRoutes(frontend, backend *httprouter.Router, corsMiddleware func(http.Handler) http.Handler) {
frontend.Handler("OPTIONS", WellKnownKeysPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
frontend.Handler("GET", WellKnownKeysPath, corsMiddleware(http.HandlerFunc(h.WellKnown)))
backend.GET(KeyHandlerPath+"/:set/:key", h.GetKey)
backend.GET(KeyHandlerPath+"/:set", h.GetKeySet)
@@ -390,3 +391,7 @@ func (h *Handler) DeleteKey(w http.ResponseWriter, r *http.Request, ps httproute

w.WriteHeader(http.StatusNoContent)
}

// This function will not be called, OPTIONS request will be handled by cors
// this is just a placeholder.
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) {}
8 changes: 8 additions & 0 deletions oauth2/handler.go
Original file line number Diff line number Diff line change
@@ -157,14 +157,18 @@ type FlushInactiveOAuth2TokensRequest struct {
}

func (h *Handler) SetRoutes(frontend, backend *httprouter.Router, corsMiddleware func(http.Handler) http.Handler) {
frontend.Handler("OPTIONS", TokenPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
frontend.Handler("POST", TokenPath, corsMiddleware(http.HandlerFunc(h.TokenHandler)))
frontend.GET(AuthPath, h.AuthHandler)
frontend.POST(AuthPath, h.AuthHandler)
frontend.GET(DefaultConsentPath, h.DefaultConsentHandler)
frontend.GET(DefaultErrorPath, h.DefaultErrorHandler)
frontend.GET(DefaultLogoutPath, h.DefaultLogoutHandler)
frontend.Handler("OPTIONS", RevocationPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
frontend.Handler("POST", RevocationPath, corsMiddleware(http.HandlerFunc(h.RevocationHandler)))
frontend.Handler("OPTIONS", WellKnownPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
frontend.Handler("GET", WellKnownPath, corsMiddleware(http.HandlerFunc(h.WellKnownHandler)))
frontend.Handler("OPTIONS", UserinfoPath, corsMiddleware(http.HandlerFunc(h.handleOptions)))
frontend.Handler("GET", UserinfoPath, corsMiddleware(http.HandlerFunc(h.UserinfoHandler)))
frontend.Handler("POST", UserinfoPath, corsMiddleware(http.HandlerFunc(h.UserinfoHandler)))

@@ -675,3 +679,7 @@ func (h *Handler) writeAuthorizeError(w http.ResponseWriter, ar fosite.Authorize

h.OAuth2.WriteAuthorizeError(w, ar, err)
}

// This function will not be called, OPTIONS request will be handled by cors
// this is just a placeholder.
func (h *Handler) handleOptions(w http.ResponseWriter, r *http.Request) {}

0 comments on commit f983afd

Please sign in to comment.